Google Optimization tools for machining workshop task Planning "Python Edition"

Source: Internet
Author: User

In the previous article, "Using. NET core and Google Optimization tools to achieve workshop task planning", this time the official Google document Python implementation of the version of the full source to meet the love of Python friends.

 from __future__ Importprint_function#Import Python wrapper for Or-tools constraint solver. fromOrtools.constraint_solverImportPYWRAPCPdefMain ():#Create the solver.Solver = PYWRAPCP. Solver ('Jobshop') Machines_count= 3Jobs_count= 3All_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 forIinchAll_jobs:horizon+=sum (processing_times[i])#creates jobs.All_tasks = {}   forIinchAll_jobs: forJinchRange (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= []   forIinchAll_machines:machines_jobs= []     forJinchAll_jobs: forKinchRange (0, Len (machines[j)):ifMachines[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.   forIinchAll_jobs: forJinchRange (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 () forIinchAll_jobs]) Objective_monitor= Solver. Minimize (Obj_var, 1)  #Create search phases.Sequence_phase = Solver. Phase ([All_sequences[i] forIinchAll_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) forIinchall_machines:sequence=All_sequences[i]; Sequence_count=sequence.    Size ();  forJinchRange (0, sequence_count): t=sequence. Interval (j) collector. ADD (t.startexpr (). Var ()) collector. ADD (t.endexpr (). Var ())#Solve the problem.Disp_col_width = 10ifSolver. Solve (Main_phase, [Objective_monitor, Collector]):Print("\noptimal Schedule Length:", collector. Objectivevalue (0),"\ n") Sol_line=""Sol_line_tasks=""    Print("Optimal Schedule","\ n")     forIinchAll_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) forJinchRange (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 ())) forJinchRange (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 for machining workshop task Planning "Python Edition"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.