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 是什么?(以下翻译为 ...
随机推荐
- 学以致用八---centos7.2 安装vim8+支持python3
目的:打造基于python的vim环境 也是在地铁上突然产生的一个想法,平时都是在pycharm上练习python,但有时候会提示激活码过期,又得上网找激活码,够折腾的.那何不在linux环境下来搭建 ...
- 【慕课网实战】Spark Streaming实时流处理项目实战笔记十二之铭文升级版
铭文一级: ======Pull方式整合 Flume Agent的编写: flume_pull_streaming.conf simple-agent.sources = netcat-sources ...
- openssl建立证书,非常详细配置ssl+apache
原文链接:http://blog.51yip.com/apachenginx/958.html openssl建立证书,非常详细配置ssl+apache 张映 发表于 2010-08-07 分类目录: ...
- ArcGIS API 和GIServer
ArcGIS API 和GIServer 先后以ArcGIS Server(9.3)和GIServer(2.2)为服务端,以ArcGIS API for Flex(1.2).ArcGIS API f ...
- 安装mysql后必做的两件事
1..删除掉不需要的用户 查看用户表mysql> SELECT User,Host FROM mysql.user; +------+-------------------------+ | U ...
- 【repost】JavaScript运动框架之速度时间版本
一.JavaScript运动框架之速度版 1.1 运动框架的实现思路 运动,其实就是在一段时间内改变 left . right . width . height . opactiy 的值,到达目的地之 ...
- Android viewpager + 可缩放的imageview
http://files.cnblogs.com/files/liaolandemengxiang/PhotoWallFallsDemo.rar http://files.cnblogs.com/fi ...
- poj2481
题意:给定一些线段(s, e),起点为s,终点为e,求每一段线段被多少线段包含(不包括相等) 思路:很明显的树状数组题目..但是做的时候想了挺久..(下面的x为线段起点, y为线段终点) 做法1:先对 ...
- mysql数据导入导出方法总结
MySQL数据备份还原方式总结: 一.将数据导入到指定的数据库 第一种导入方式: (linux下和Windows 下语法是一样的,只是路径的书写方式不同而已) 1.创建一个空数据库 2.进入MySQL ...
- ASP.NET WebAPI (反)序列化用[SerializableAttribute]修饰的类的一个坑
发现问题 在 ASP.NET WebAPI 项目中,有这样的 ViewModel 类: [Serializable] class Product { public int Id { get; set; ...