Google Optimization Tools實現加工車間任務規劃【Python版】

來源:互聯網
上載者:User

標籤:constrain   官方文檔   sum   duration   creates   nes   put   space   ali   

上一篇介紹了《使用.NET Core與Google Optimization Tools實現加工車間任務規劃》,這次將Google官方文檔python實現的版本的完整源碼獻出來,以滿足喜愛python的朋友。

from __future__ import print_function# Import Python wrapper for or-tools constraint solver.from ortools.constraint_solver import pywrapcpdef main():  # Create the solver.  solver = pywrapcp.Solver(‘jobshop‘)  machines_count = 3  jobs_count = 3  all_machines = range(0, machines_count)  all_jobs = range(0, jobs_count)  # Define data.  machines = [[0, 1, 2],              [0, 2, 1],              [1, 2]]  processing_times = [[3, 2, 2],                      [2, 1, 4],                      [4, 3]]  # Computes horizon.  horizon = 0  for i in all_jobs:    horizon += sum(processing_times[i])  # Creates jobs.  all_tasks = {}  for i in all_jobs:    for j in range(0, len(machines[i])):      all_tasks[(i, j)] = solver.FixedDurationIntervalVar(0,                                                          horizon,                                                          processing_times[i][j],                                                          False,                                                          ‘Job_%i_%i‘ % (i, j))  # Creates sequence variables and add disjunctive constraints.  all_sequences = []  all_machines_jobs = []  for i in all_machines:    machines_jobs = []    for j in all_jobs:      for k in range(0, len(machines[j])):        if machines[j][k] == i:          machines_jobs.append(all_tasks[(j, k)])    disj = solver.DisjunctiveConstraint(machines_jobs, ‘machine %i‘ % i)    all_sequences.append(disj.SequenceVar())    solver.Add(disj)  # Add conjunctive contraints.  for i in all_jobs:    for j in range(0, len(machines[i]) - 1):      solver.Add(all_tasks[(i, j + 1)].StartsAfterEnd(all_tasks[(i, j)]))  # Set the objective.  obj_var = solver.Max([all_tasks[(i, len(machines[i])-1)].EndExpr()                        for i in all_jobs])  objective_monitor = solver.Minimize(obj_var, 1)  # Create search phases.  sequence_phase = solver.Phase([all_sequences[i] for i in all_machines],                                solver.SEQUENCE_DEFAULT)  vars_phase = solver.Phase([obj_var],                            solver.CHOOSE_FIRST_UNBOUND,                            solver.ASSIGN_MIN_VALUE)  main_phase = solver.Compose([sequence_phase, vars_phase])  # Create the solution collector.  collector = solver.LastSolutionCollector()  # Add the interesting variables to the SolutionCollector.  collector.Add(all_sequences)  collector.AddObjective(obj_var)  for i in all_machines:    sequence = all_sequences[i];    sequence_count = sequence.Size();    for j in range(0, sequence_count):      t = sequence.Interval(j)      collector.Add(t.StartExpr().Var())      collector.Add(t.EndExpr().Var())  # Solve the problem.  disp_col_width = 10  if solver.Solve(main_phase, [objective_monitor, collector]):    print("\nOptimal Schedule Length:", collector.ObjectiveValue(0), "\n")    sol_line = ""    sol_line_tasks = ""    print("Optimal Schedule", "\n")    for i in all_machines:      seq = all_sequences[i]      sol_line += "Machine " + str(i) + ": "      sol_line_tasks += "Machine " + str(i) + ": "      sequence = collector.ForwardSequence(0, seq)      seq_size = len(sequence)      for j in range(0, seq_size):        t = seq.Interval(sequence[j]);         # Add spaces to output to align columns.        sol_line_tasks +=  t.Name() + " " * (disp_col_width - len(t.Name()))      for j in range(0, seq_size):        t = seq.Interval(sequence[j]);        sol_tmp = "[" + str(collector.Value(0, t.StartExpr().Var())) + ","        sol_tmp += str(collector.Value(0, t.EndExpr().Var())) + "] "        # Add spaces to output to align columns.        sol_line += sol_tmp + " " * (disp_col_width - len(sol_tmp))      sol_line += "\n"      sol_line_tasks += "\n"    print(sol_line_tasks)    print("Time Intervals for Tasks\n")    print(sol_line)if __name__ == ‘__main__‘:  main()

 

Google Optimization Tools實現加工車間任務規劃【Python版】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.