上一篇介绍了《使用.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 pywrapcp def 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版】的更多相关文章

  1. 使用.NET Core与Google Optimization Tools实现加工车间任务规划

    前一篇文章<使用.NET Core与Google Optimization Tools实现员工排班计划Scheduling>算是一种针对内容的规划,而针对时间顺序任务规划,加工车间的工活儿 ...

  2. Google Optimization Tools实现员工排班计划Scheduling【Python版】

    上一篇介绍了<使用.Net Core与Google Optimization Tools实现员工排班计划Scheduling>,这次将Google官方文档python实现的版本的完整源码献 ...

  3. Google Optimization Tools介绍

    Google Optimization Tools(OR-Tools)是一款专门快速而便携地解决组合优化问题的套件.它包含了: 约束编程求解器. 简单而统一的接口,用于多种线性规划和混合整数规划求解, ...

  4. 使用.NET Core与Google Optimization Tools实现员工排班计划Scheduling

    上一篇说完<Google Optimization Tools介绍>,让大家初步了解了Google Optimization Tools是一款约束求解(CP)的高效套件.那么我们用.NET ...

  5. Google PageSpeed Tools 性能测试分析

    今天给大家介绍下一个工具:Google PageSpeed Tools,根据官方的介绍,简单梳理如下: Page Speed Insights能针对移动设备和电脑设备衡量网页的性能.该工具会抓取网址两 ...

  6. Google performance Tools (gperftools) 使用心得

    Google performance Tools (gperftools) 使用心得 gperftools是google开发的一款非常实用的工具集,主要包括:性能优异的malloc free内存分配器 ...

  7. 学习笔记24—win10环境下python版libsvm的安装

    1.前言 由于毕业设计需要用到libsvm,所以最近专心于配置libsvm,曾经尝试过在matlab中安装,但是没有成功.最终在Python环境中完成安装. 2.LIBSVM介绍 LIBSVM 是台湾 ...

  8. 监控linux流量python版

    python版监控linux流量 直接上代码,使用OptionParser来传入参数 #coding:utf-8 #------------- #Author:Hu #Data:20150520 #- ...

  9. pyDes 实现 Python 版的 DES 对称加密/解密--转

    https://my.oschina.net/leejun2005/blog/586451 手头有个 Java 版的 DES 加密/解密程序,最近想着将其 Python 重构下,方便后续脚本解析,捣鼓 ...

随机推荐

  1. convert 函数的使用

    先说问题:今天项目中很多模块出现了一个集体性的问题,时间检索时选择同一天 检索不出数据(表中数据确实存在),其实这个问题在做东西的时候领导说过,记忆中我解决了,但是后来写代码可能把这个问题忘记了! 其 ...

  2. cmp指令

    cmp是比较指令,cmp的功能相当于减法指令,只是不保存结果.cmp指令执行后,将对标志寄存器产生影响.其他相关指令通过识别这些被影响的标志寄存器位来得知比较结果. cmp指令格式: cmp 操作对象 ...

  3. 定时执行自动化脚本-(二)ant发送邮件及邮件中添加附件

    发送邮件及邮件添加附件均需要用java来实现 1.idea创建一个maven的java项目,目录结构如下 2.pom.xml文件添加依赖的javax.mail <dependencies> ...

  4. no.1 voice

    1. import win32com.client text="hello world" speaker = win32com.client.Dispatch("SAPI ...

  5. Python(五) 字典

  6. java28

    1.使用多态的优点 把要创建的多个子类缩减为一个父类接着传入参数,用参数调用子类的方法, 输出时直接调用父类的方法,这时传参传创建的对象 2.多态方法的调用 调用的方法前有static时,会默认调用父 ...

  7. MFC的停靠窗口中插入对话框,在对话框中添加控件并做控件自适应

    单文档程序添加了停靠窗口后,可能会在停靠窗口中添加一些控件.在这里我的做法是在对话框上添加控件并布局,然后将这个对话框插入到停靠窗口中. 步骤 1.插入对话框,在对话框中放入控件(我的为树形控件),并 ...

  8. ABP框架系列之三十四:(Multi-Tenancy-多租户)

    What Is Multi Tenancy? "Software Multitenancy refers to a software architecture in which a sing ...

  9. [f]计时器

    // 计时器 function Timer(ele) { this._mStr = ''; this._sStr = ''; this._m = 0; this._s = 0; this._setTi ...

  10. jzoj3156. 【GDOI2013模拟1】病毒传播

    题意: 村庄里有m个人,初始有一些人感染了病毒.如果第i个人的编号i满足,有一对(a,b)(a是初始病毒感染者编号,b为前一天的感染者编号)使\(a*b mod m =i\),则第i个人会感染病毒.每 ...