上一篇介绍了《使用.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. python--linux上如何执行shell命令

    1.os.system(command) 参考链接:https://www.jb51.net/article/141877.htm

  2. swift 分组tableview 设置分区投或者尾部,隐藏默认间隔高度

    1.隐藏尾部或者头部,配套使用 //注册头部id tv.register(JYWithdrawalRecordSectionView.self, forHeaderFooterViewReuseIde ...

  3. [leetcode]205. Isomorphic Strings 同构字符串

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  4. Eclipse中 *.properties 文件编码设置

    Eclipse 中的默认编码格式为 ISO-8895-1,在此编码下中文的会显示如下的效果 解决方法 Windows --> Preference --> General Types -- ...

  5. Axure RP Extension for Chrome 插件安装

    描述 我的chmod浏览器上不去谷歌商店,我用的是蓝灯,登上商店后搜索Axure RP Extension for Chrome,下载安装,完成后进入这个插件的详细信息: 使用 打开用axure生成的 ...

  6. Java程序员职业生涯规划完整版:从程序员到CTO( 摘)

    在技巧方面无论我们怎么学习,总感觉需要晋升自已不知道自己处于什么水平了.但如果有清晰的指示图供参考还是非常不错的,这样我们清楚的知道我们大概处于那个阶段和水平. Java程序员 高等特性 反射.泛型. ...

  7. Reading | 《机器学习》(周志华)(未完待续)

    目录 I. 大师对人工智能和机器学习的看法 II. Introduction A. What is Machine Learning 什么是机器学习 B. Basic terms 基础术语 C. In ...

  8. HashMap TreeMap的区别

    Map主要用于存储健值对,根据键得到值,因此不允许键重复(重复就覆盖了),但允许值重复.Hashmap 是一个最常用的Map,它根据键的HashCode 值存储数据,根据键可以直接获取它的值,具有很快 ...

  9. 使用Phabricator进行代码审查

    Pharicator 是FB的代码审查工具,主要开发者为Evan Priestley,是一个开源软件,可在Apache许可证第2版下作为自由软件分发.详细信息可查看官方文档.这里从应用的角度,一步一步 ...

  10. ASP.NET Core OceLot 微服务实践

    1.OceLot中间件介绍 在传统的BS应用中,随着业务需求的快速发展变化,需求不断增长,迫切需要一种更加快速高效的软件交付方式.微服务可以弥补单体应用不足,是一种更加快速高效软件架构风格.单体应用被 ...