Google Optimization Tools介绍
Google Optimization Tools(OR-Tools)是一款专门快速而便携地解决组合优化问题的套件。它包含了:
- 约束编程求解器。
- 简单而统一的接口,用于多种线性规划和混合整数规划求解,包括 CBC、CLP、GLOP、GLPK、Gurobi、CPLEX和SCIP。
- 图算法 (最短路径、最小成本、最大流量、线性求和分配)。
- 经典旅行推销员问题和车辆路径问题的算法。
- 经典装箱和背包算法。
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#版本的链接为:Mac, Ubuntu 17.04, Ubuntu 16.04, Ubuntu 14.04, CentOS 7, Debian 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++ 代码
- #include "ortools/linear_solver/linear_solver.h"
- #include "ortools/linear_solver/linear_solver.pb.h"
- namespace operations_research {
- void RunTest(
- MPSolver::OptimizationProblemType optimization_problem_type) {
- MPSolver solver("Glop", optimization_problem_type);
- MPVariable* const x = solver.MakeNumVar(0.0, , "x");
- MPVariable* const y = solver.MakeNumVar(0.0, , "y");
- MPObjective* const objective = solver.MutableObjective();
- objective->SetCoefficient(x, );
- objective->SetCoefficient(y, );
- objective->SetMaximization();
- solver.Solve();
- printf("\nSolution:");
- printf("\nx = %.1f", x->solution_value());
- printf("\ny = %.1f", y->solution_value());
- }
- void RunExample() {
- RunTest(MPSolver::GLOP_LINEAR_PROGRAMMING);
- }
- }
- int main(int argc, char** argv) {
- operations_research::RunExample();
- return ;
- }
C# 代码
- using System;
- using Google.OrTools.LinearSolver;
- public class my_program
- {
- private static void RunLinearProgrammingExample(String solverType)
- {
- Solver solver = Solver.CreateSolver("IntegerProgramming", solverType);
- Variable x = solver.MakeNumVar(0.0, 1.0, "x");
- Variable y = solver.MakeNumVar(0.0, 2.0, "y");
- Objective objective = solver.Objective();
- objective.SetCoefficient(x, );
- objective.SetCoefficient(y, );
- objective.SetMaximization();
- solver.Solve();
- Console.WriteLine("Solution:");
- Console.WriteLine("x = " + x.SolutionValue());
- Console.WriteLine("y = " + y.SolutionValue());
- }
- static void Main()
- {
- RunLinearProgrammingExample("GLOP_LINEAR_PROGRAMMING");
- }
- }
Python 代码
- from __future__ import print_function
- from ortools.linear_solver import pywraplp
- def main():
- solver = pywraplp.Solver('SolveSimpleSystem',
- pywraplp.Solver.GLOP_LINEAR_PROGRAMMING)
- x = solver.NumVar(0, 1, 'x')
- y = solver.NumVar(0, 2, 'y')
- objective = solver.Objective()
- objective.SetCoefficient(x, 1)
- objective.SetCoefficient(y, 1)
- objective.SetMaximization()
- solver.Solve()
- print('Solution:')
- print('x = ', x.solution_value())
- print('y = ', y.solution_value())
- if __name__ == '__main__':
- main()
Java 代码
- import com.google.ortools.linearsolver.MPConstraint;
- import com.google.ortools.linearsolver.MPObjective;
- import com.google.ortools.linearsolver.MPSolver;
- import com.google.ortools.linearsolver.MPVariable;
- public class my_program {
- static { System.loadLibrary("jniortools"); }
- private static MPSolver createSolver (String solverType) {
- return new MPSolver("my_program",
- MPSolver.OptimizationProblemType.valueOf(solverType));
- }
- private static void runmy_program(String solverType,
- boolean printModel) {
- MPSolver solver = createSolver(solverType);
- MPVariable x = solver.makeNumVar(0.0, 1.0, "x");
- MPVariable y = solver.makeNumVar(0.0, 2.0, "y");
- MPObjective objective = solver.objective();
- objective.setCoefficient(y, 1);
- objective.setMaximization();
- solver.solve();
- System.out.println("Solution:");
- System.out.println("x = " + x.solutionValue());
- System.out.println("y = " + y.solutionValue());
- }
- public static void main(String[] args) throws Exception {
- runmy_program("GLOP_LINEAR_PROGRAMMING", false);
- }
- }
执行结果如图:
下一篇这个系列的文章,将具体介绍一种约束求解的应用场景。
Google Optimization Tools介绍的更多相关文章
- 使用.NET Core与Google Optimization Tools实现员工排班计划Scheduling
上一篇说完<Google Optimization Tools介绍>,让大家初步了解了Google Optimization Tools是一款约束求解(CP)的高效套件.那么我们用.NET ...
- Google Optimization Tools实现加工车间任务规划【Python版】
上一篇介绍了<使用.NET Core与Google Optimization Tools实现加工车间任务规划>,这次将Google官方文档python实现的版本的完整源码献出来,以满足喜爱 ...
- 使用.NET Core与Google Optimization Tools实现加工车间任务规划
前一篇文章<使用.NET Core与Google Optimization Tools实现员工排班计划Scheduling>算是一种针对内容的规划,而针对时间顺序任务规划,加工车间的工活儿 ...
- Google Optimization Tools实现员工排班计划Scheduling【Python版】
上一篇介绍了<使用.Net Core与Google Optimization Tools实现员工排班计划Scheduling>,这次将Google官方文档python实现的版本的完整源码献 ...
- Google PageSpeed Tools 性能测试分析
今天给大家介绍下一个工具:Google PageSpeed Tools,根据官方的介绍,简单梳理如下: Page Speed Insights能针对移动设备和电脑设备衡量网页的性能.该工具会抓取网址两 ...
- Google performance Tools (gperftools) 使用心得
Google performance Tools (gperftools) 使用心得 gperftools是google开发的一款非常实用的工具集,主要包括:性能优异的malloc free内存分配器 ...
- Google Congestion Control介绍
随着网络带宽的日益增加和便携式设备,如智能手机或平板电脑处理能力的增强,基于互联网的实时通信已经成为热点. 虽然视频会议已商用了多年,特别是SKYPE这样的视频应用在互联网上已有10年时间,但针对实时 ...
- paper 8:支持向量机系列五:Numerical Optimization —— 简要介绍求解求解 SVM 的数值优化算法。
作为支持向量机系列的基本篇的最后一篇文章,我在这里打算简单地介绍一下用于优化 dual 问题的 Sequential Minimal Optimization (SMO) 方法.确确实实只是简单介绍一 ...
- (转)Google Fonts 的介绍与使用
转载自“前端笔记” http://www.cnblogs.com/milly/archive/2013/05/10/google-fonts.html Google Fonts 是什么?(以下翻译为 ...
随机推荐
- 禁止浏览器缓存js
方法:在js文件后加上数学随机数; Math.random() 比如:源代码为 <script src="./js/lib/require/require.js" data- ...
- mybatis-generator扩展教程系列 -- 自定义generatorConfig.xml参数
http://blog.csdn.net/shadowsick/article/details/53413235
- noip第13课作业
1. 排身高 [问题描述] 鹏鹏的班上一共有 n 个学生.刚好每个同学的身高互不相同.鹏鹏想知道,所有同学中身高第二高的是谁. 输入格式:输入共两行,第一行有一个整数 n(2≤n≤100),表示 ...
- android 发送url带中文出现乱码怎么解决
上传的时候参数中带中文的时候发送参数的时候就有可能出现乱码,这种情况怎么解决呢,就是设置url的格式为utf-8 httpRequest.setEntity(new UrlEncodedFormEnt ...
- nodeclub route
这里是把web_router.js放在根目录下,也可以放在routes文件件下,其实都可以. 这里就是一些url与controller和middleware对应
- .net core 与ELK(1)安装Elasticsearch
1.安装java jdk [elsearch@localhost bin]$ java -version openjdk version "1.8.0_181" OpenJDK R ...
- C# Argument 'picture' must be a picture that can be used as an Icon
Scenario: 创建了一个WinForm的小程序,希望将它显示在任务栏,所以在工具栏中的“公共控件”里,拖入NotifyIcon控件—notifyIcon1,这个是程序运行任务栏右侧通知区域图标显 ...
- 背水一战 Windows 10 (55) - 控件(集合类): SemanticZoom, ISemanticZoomInformation
[源码下载] 背水一战 Windows 10 (55) - 控件(集合类): SemanticZoom, ISemanticZoomInformation 作者:webabcd 介绍背水一战 Wind ...
- 17_python_成员
一.类成员 1.字段 class Province: country = '中国' # 实例 (静态) 字段:类变量. 不属于对象, 对象可以访问 def __init__(self, name): ...
- Java并发编程总结2——慎用CAS
一.CAS和synchronized适用场景 1.对于资源竞争较少的情况,使用synchronized同步锁进行线程阻塞和唤醒切换以及用户态内核态间的切换操作额外浪费消耗cpu资源:而CAS基于硬件实 ...