上一篇介绍了《使用.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. Working days

    form FRM_GET_WORKING_DAYS TABLES pt_days CHANGING pv_duration. DATA:ls_xt001w TYPE t001w, lv_sdate T ...

  2. js学习(5)语法专题

    Js是一种动态类型语言,变量没有类型限制,可以随时赋值 强制转换: 主要指使用Number(),String()和Boolean()三个函数,手动将各个类型的值,分别转换为数字,字符串或布尔值 Num ...

  3. eclipse 安装lombok插件

    下载lombok 下载地址:https://projectlombok.org/downloads/lombok.jar 或者访问官网下载  https://projectlombok.org/ 安装 ...

  4. eclipse使用lomnok无效

    把下载好的jar包去掉版本号放到与eclipse.exe同级目录 修改sts.ini或者eclipse.ini 在最后面加上: -javaagent:lombok.jar-Xbootclasspath ...

  5. vue 总结

    VUE总结 双花括号{{}} 01.index.hmlt main.js 内存的数据可以更改 v-model 双休数据绑定 代码: <!DOCTYPE html> <html lan ...

  6. mybatis进阶-5resultMap总结

    resultType: 作用: 将查询结果按照sql列名pojo属性名一致性映射到pojo中. 场合: 常见一些明细记录的展示,比如用户购买商品明细,将关联查询信息全部展示在页面时,此时可直接使用re ...

  7. javaweb开发3.基于Servlet+JSP+JavaBean开发模式的用户登录注册

    转载孤傲苍狼博客http://www.cnblogs.com/xdp-gacl/p/3902537.html 1.层次比较分明的项目结构图

  8. for循环的实例

    1.大马驮2石粮食,中马驮1石粮食,两头小马驮一石粮食,要用100匹马,驮100石粮食,该如//首先我们要知道一百石粮食需要这些马分别几匹 //第一个是大马,需要五十匹马for(var x=0;x&l ...

  9. getObjectURL 上传图片预览

    js 函数 function getObjectURL(file) {    var url = null ;     if (window.createObjectURL!=undefined) { ...

  10. Win7 64位使用IDA Pro 6.8调试64位exe程序

    有点小坑,记录备忘. 首先搞个IDA Pro6.8,写本文时能找到的最高版本,试了下果然比6.6强大许多,其实6.6也没怎么用过...... 32位版本有个Local Win32 debugger,但 ...