Google Optimization Tools(OR-Tools)是一款专门快速而便携地解决组合优化问题的套件。它包含了:

  • 约束编程求解器。
  • 简单而统一的接口,用于多种线性规划和混合整数规划求解,包括 CBCCLPGLOPGLPKGurobiCPLEXSCIP
  • 图算法 (最短路径、最小成本、最大流量、线性求和分配)。
  • 经典旅行推销员问题和车辆路径问题的算法。
  • 经典装箱和背包算法。

Google使用C++开发了OR-Tools库,但支持Python,C#,或Java语言调用。

安装Google OR-Tools

Google OR-Tools的源码在[Github] google/or-tools。其它开发环境下的安装如下。

Linux or Mac下安装

1. 确认使用了Python 2.7+,3.5+版本,以及pip 9.0.1+版本。

2. Mac OSX系统需要安装命令行工具Xcode,在Terminal中执行xcode-select --install

Linux系统需要安装g++,在Terminal中执行sudo apt-get install g++ make

如果使用C#请确认安装了Mono 4.2.0+的64位版本。

3. 在Terminal中执行pip install --upgrade ortools直接安装Python版本的OR-Tools包。C++/Java/C#版本的链接为:MacUbuntu 17.04Ubuntu 16.04Ubuntu 14.04CentOS 7Debian 9 ,下载到指定目录后执行make all

Windows下安装

Python版本的包的安装和Linux一样,可自行选用合适的开发工具。若是使用C++、C#,推荐使用64位版本的Windows10操作系统,并且使用Microsoft Visual Studio 2015 或者 2017作为开发工具,相应的库文件下载地址为: Visual Studio 2017 the Visual Studio 2015

  • C++使用lib/ortools.lib, 并且将or‑tools/include添加到项目引用。
  • Java使用jar命令调用lib/com.google.ortools.lib的方式,并且将 ‑Djava.library.path=PATH_TO_or‑tools/lib添加到命令行。
  • C#添加bin/Google.OrTools.dll到项目依赖,或者使用NuGet搜索Google.OrTools进行安装。

Demo

以下是几种支持语言的demo,运行一下验证是否安装正确。

C++ 代码

  1. #include "ortools/linear_solver/linear_solver.h"
  2. #include "ortools/linear_solver/linear_solver.pb.h"
  3.  
  4. namespace operations_research {
  5. void RunTest(
  6. MPSolver::OptimizationProblemType optimization_problem_type) {
  7. MPSolver solver("Glop", optimization_problem_type);
  8. MPVariable* const x = solver.MakeNumVar(0.0, , "x");
  9. MPVariable* const y = solver.MakeNumVar(0.0, , "y");
  10. MPObjective* const objective = solver.MutableObjective();
  11. objective->SetCoefficient(x, );
  12. objective->SetCoefficient(y, );
  13. objective->SetMaximization();
  14. solver.Solve();
  15. printf("\nSolution:");
  16. printf("\nx = %.1f", x->solution_value());
  17. printf("\ny = %.1f", y->solution_value());
  18. }
  19.  
  20. void RunExample() {
  21. RunTest(MPSolver::GLOP_LINEAR_PROGRAMMING);
  22. }
  23. }
  24.  
  25. int main(int argc, char** argv) {
  26. operations_research::RunExample();
  27. return ;
  28. }

C# 代码

  1. using System;
  2. using Google.OrTools.LinearSolver;
  3.  
  4. public class my_program
  5. {
  6. private static void RunLinearProgrammingExample(String solverType)
  7. {
  8. Solver solver = Solver.CreateSolver("IntegerProgramming", solverType);
  9. Variable x = solver.MakeNumVar(0.0, 1.0, "x");
  10. Variable y = solver.MakeNumVar(0.0, 2.0, "y");
  11. Objective objective = solver.Objective();
  12. objective.SetCoefficient(x, );
  13. objective.SetCoefficient(y, );
  14. objective.SetMaximization();
  15. solver.Solve();
  16. Console.WriteLine("Solution:");
  17. Console.WriteLine("x = " + x.SolutionValue());
  18. Console.WriteLine("y = " + y.SolutionValue());
  19. }
  20.  
  21. static void Main()
  22. {
  23. RunLinearProgrammingExample("GLOP_LINEAR_PROGRAMMING");
  24. }
  25. }

Python 代码

  1. from __future__ import print_function
  2. from ortools.linear_solver import pywraplp
  3.  
  4. def main():
  5. solver = pywraplp.Solver('SolveSimpleSystem',
  6. pywraplp.Solver.GLOP_LINEAR_PROGRAMMING)
  7. x = solver.NumVar(0, 1, 'x')
  8. y = solver.NumVar(0, 2, 'y')
  9. objective = solver.Objective()
  10. objective.SetCoefficient(x, 1)
  11. objective.SetCoefficient(y, 1)
  12. objective.SetMaximization()
  13. solver.Solve()
  14. print('Solution:')
  15. print('x = ', x.solution_value())
  16. print('y = ', y.solution_value())
  17.  
  18. if __name__ == '__main__':
  19. main()

Java 代码

  1. import com.google.ortools.linearsolver.MPConstraint;
  2. import com.google.ortools.linearsolver.MPObjective;
  3. import com.google.ortools.linearsolver.MPSolver;
  4. import com.google.ortools.linearsolver.MPVariable;
  5.  
  6. public class my_program {
  7. static { System.loadLibrary("jniortools"); }
  8.  
  9. private static MPSolver createSolver (String solverType) {
  10. return new MPSolver("my_program",
  11. MPSolver.OptimizationProblemType.valueOf(solverType));
  12. }
  13.  
  14. private static void runmy_program(String solverType,
  15. boolean printModel) {
  16. MPSolver solver = createSolver(solverType);
  17. MPVariable x = solver.makeNumVar(0.0, 1.0, "x");
  18. MPVariable y = solver.makeNumVar(0.0, 2.0, "y");
  19. MPObjective objective = solver.objective();
  20. objective.setCoefficient(y, 1);
  21. objective.setMaximization();
  22. solver.solve();
  23. System.out.println("Solution:");
  24. System.out.println("x = " + x.solutionValue());
  25. System.out.println("y = " + y.solutionValue());
  26. }
  27.  
  28. public static void main(String[] args) throws Exception {
  29. runmy_program("GLOP_LINEAR_PROGRAMMING", false);
  30. }
  31. }

执行结果如图:

下一篇这个系列的文章,将具体介绍一种约束求解的应用场景。

Google Optimization Tools介绍的更多相关文章

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

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

  2. Google Optimization Tools实现加工车间任务规划【Python版】

    上一篇介绍了<使用.NET Core与Google Optimization Tools实现加工车间任务规划>,这次将Google官方文档python实现的版本的完整源码献出来,以满足喜爱 ...

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

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

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

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

  5. Google PageSpeed Tools 性能测试分析

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

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

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

  7. Google Congestion Control介绍

    随着网络带宽的日益增加和便携式设备,如智能手机或平板电脑处理能力的增强,基于互联网的实时通信已经成为热点. 虽然视频会议已商用了多年,特别是SKYPE这样的视频应用在互联网上已有10年时间,但针对实时 ...

  8. paper 8:支持向量机系列五:Numerical Optimization —— 简要介绍求解求解 SVM 的数值优化算法。

    作为支持向量机系列的基本篇的最后一篇文章,我在这里打算简单地介绍一下用于优化 dual 问题的 Sequential Minimal Optimization (SMO) 方法.确确实实只是简单介绍一 ...

  9. (转)Google Fonts 的介绍与使用

    转载自“前端笔记”  http://www.cnblogs.com/milly/archive/2013/05/10/google-fonts.html Google Fonts 是什么?(以下翻译为 ...

随机推荐

  1. 禁止浏览器缓存js

    方法:在js文件后加上数学随机数; Math.random() 比如:源代码为 <script src="./js/lib/require/require.js" data- ...

  2. mybatis-generator扩展教程系列 -- 自定义generatorConfig.xml参数

    http://blog.csdn.net/shadowsick/article/details/53413235

  3. noip第13课作业

    1.    排身高 [问题描述] 鹏鹏的班上一共有 n 个学生.刚好每个同学的身高互不相同.鹏鹏想知道,所有同学中身高第二高的是谁. 输入格式:输入共两行,第一行有一个整数 n(2≤n≤100),表示 ...

  4. android 发送url带中文出现乱码怎么解决

    上传的时候参数中带中文的时候发送参数的时候就有可能出现乱码,这种情况怎么解决呢,就是设置url的格式为utf-8 httpRequest.setEntity(new UrlEncodedFormEnt ...

  5. nodeclub route

    这里是把web_router.js放在根目录下,也可以放在routes文件件下,其实都可以. 这里就是一些url与controller和middleware对应

  6. .net core 与ELK(1)安装Elasticsearch

    1.安装java jdk [elsearch@localhost bin]$ java -version openjdk version "1.8.0_181" OpenJDK R ...

  7. C# Argument 'picture' must be a picture that can be used as an Icon

    Scenario: 创建了一个WinForm的小程序,希望将它显示在任务栏,所以在工具栏中的“公共控件”里,拖入NotifyIcon控件—notifyIcon1,这个是程序运行任务栏右侧通知区域图标显 ...

  8. 背水一战 Windows 10 (55) - 控件(集合类): SemanticZoom, ISemanticZoomInformation

    [源码下载] 背水一战 Windows 10 (55) - 控件(集合类): SemanticZoom, ISemanticZoomInformation 作者:webabcd 介绍背水一战 Wind ...

  9. 17_python_成员

    一.类成员 1.字段 class Province: country = '中国' # 实例 (静态) 字段:类变量. 不属于对象, 对象可以访问 def __init__(self, name): ...

  10. Java并发编程总结2——慎用CAS

    一.CAS和synchronized适用场景 1.对于资源竞争较少的情况,使用synchronized同步锁进行线程阻塞和唤醒切换以及用户态内核态间的切换操作额外浪费消耗cpu资源:而CAS基于硬件实 ...