polish output
This commit is contained in:
@@ -20,7 +20,7 @@ import collections
|
||||
from ortools.sat.python import cp_model
|
||||
|
||||
|
||||
def main():
|
||||
def jobshop_with_maintenance():
|
||||
"""Solves a jobshop with maintenance on one machine."""
|
||||
model = cp_model.CpModel()
|
||||
|
||||
@@ -110,7 +110,7 @@ def main():
|
||||
# Add spaces to output to align columns.
|
||||
sol_line_tasks += '%-10s' % name
|
||||
start = assigned_task.start
|
||||
duration = jobs_data[assigned_task.job][assigned_task.index][1]
|
||||
duration = assigned_task.duration
|
||||
|
||||
sol_tmp = '[%i,%i]' % (start, start + duration)
|
||||
# Add spaces to output to align columns.
|
||||
@@ -126,5 +126,4 @@ def main():
|
||||
print(output)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
jobshop_with_maintenance()
|
||||
|
||||
@@ -22,7 +22,7 @@ import collections
|
||||
from ortools.sat.python import cp_model
|
||||
|
||||
|
||||
def MinimalJobshopSat():
|
||||
def minimal_jobshop_sat():
|
||||
"""Minimal jobshop problem."""
|
||||
# Create the model.
|
||||
model = cp_model.CpModel()
|
||||
@@ -47,10 +47,12 @@ def MinimalJobshopSat():
|
||||
# [START variables]
|
||||
task_type = collections.namedtuple('task_type', 'start end interval')
|
||||
assigned_task_type = collections.namedtuple('assigned_task_type',
|
||||
'start job index')
|
||||
'start job index duration')
|
||||
|
||||
# Create jobs.
|
||||
# Creates job intervals and add to the corresponding machine lists.
|
||||
all_tasks = {}
|
||||
machine_to_intervals = collections.defaultdict(list)
|
||||
|
||||
for job in all_jobs:
|
||||
for task_id, task in enumerate(jobs_data[job]):
|
||||
start_var = model.NewIntVar(0, horizon,
|
||||
@@ -61,23 +63,18 @@ def MinimalJobshopSat():
|
||||
start_var, duration, end_var, 'interval_%i_%i' % (job, task_id))
|
||||
all_tasks[job, task_id] = task_type(
|
||||
start=start_var, end=end_var, interval=interval_var)
|
||||
machine_to_intervals[task[0]].append(interval_var)
|
||||
# [END variables]
|
||||
|
||||
# [START constraints]
|
||||
# Create and add disjunctive constraints.
|
||||
for machine in all_machines:
|
||||
intervals = []
|
||||
for job in all_jobs:
|
||||
for task_id, task in enumerate(jobs_data[job]):
|
||||
if task[0] == machine:
|
||||
intervals.append(all_tasks[job, task_id].interval)
|
||||
model.AddNoOverlap(intervals)
|
||||
for m in all_machines:
|
||||
model.AddNoOverlap(machine_to_intervals[m])
|
||||
|
||||
# Add precedence contraints.
|
||||
for job in all_jobs:
|
||||
for task_id in range(0, len(jobs_data[job]) - 1):
|
||||
model.Add(all_tasks[job, task_id +
|
||||
1].start >= all_tasks[job, task_id].end)
|
||||
# Precedences inside a job.
|
||||
for j, job in enumerate(jobs_data):
|
||||
for t in range(len(job) - 1):
|
||||
model.Add(all_tasks[j, t + 1].start >= all_tasks[j, t].end)
|
||||
# [END constraints]
|
||||
|
||||
# [START objective]
|
||||
@@ -110,7 +107,8 @@ def MinimalJobshopSat():
|
||||
assigned_task_type(
|
||||
start=solver.Value(all_tasks[job, task_id].start),
|
||||
job=job,
|
||||
index=task_id))
|
||||
index=task_id,
|
||||
duration=task[1]))
|
||||
|
||||
|
||||
# Create per machine output lines.
|
||||
@@ -125,9 +123,9 @@ def MinimalJobshopSat():
|
||||
name = 'job_%i_%i' % (assigned_task.job, assigned_task.index)
|
||||
# Add spaces to output to align columns.
|
||||
sol_line_tasks += '%-10s' % name
|
||||
start = assigned_task.start
|
||||
duration = jobs_data[assigned_task.job][assigned_task.index][1]
|
||||
|
||||
start = assigned_task.start
|
||||
duration = assigned_task.duration
|
||||
sol_tmp = '[%i,%i]' % (start, start + duration)
|
||||
# Add spaces to output to align columns.
|
||||
sol_line += '%-10s' % sol_tmp
|
||||
@@ -143,5 +141,5 @@ def MinimalJobshopSat():
|
||||
# [END solution_printing]
|
||||
|
||||
|
||||
MinimalJobshopSat()
|
||||
minimal_jobshop_sat()
|
||||
# [END program]
|
||||
|
||||
Reference in New Issue
Block a user