• 基于Or-Tools的线性规划问题求解


    OR-Tools官网例题

    画出可行域如图所示:
    在这里插入图片描述

    Python调用ortools求解

    导入求解器

    # 导入(或包含)or - tools线性求解器包装器,这是MIP求解器和线性求解器的接口,如下所示
    from ortools.linear_solver import pywraplp
    
    • 1
    • 2

    声明线性规划求解器

    MPsolver is a wrapper for several different solvers, including Glop. The code below declares the GLOP solver.

    solver = pywraplp.Solver.CreateSolver("GLOP")
    
    • 1

    创建决策变量

    x = solver.NumVar(0, solver.infinity(), "x")
    y = solver.NumVar(0, solver.infinity(), "y")
    f"Number of variables ={solver.NumVariables()}"
    
    • 1
    • 2
    • 3
    'Number of variables =2'
    
    • 1

    约束条件

    # Constraint 0: x + 2y <= 14.
    solver.Add(x + 2 * y <= 14.0)
    # Constraint 1: 3x - y >= 0.
    solver.Add(3 * x - y >= 0.0)
    # Constraint 2: x - y <= 2.
    solver.Add(x - y <= 2.0)
    
    print("Number of constraints =", solver.NumConstraints())
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    定义目标函数

    # Objective function: 3x + 4y.
    solver.Maximize(3 * x + 4 * y)
    
    • 1
    • 2

    调用求解器

    status = solver.Solve()
    
    • 1

    打印结果

    if status == pywraplp.Solver.OPTIMAL:
        print("Solution:")
        print("Objective value =", solver.Objective().Value())
        print("x =", x.solution_value())
        print("y =", y.solution_value())
    else:
        print("The problem does not have an optimal solution.")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    Solution:
    Objective value = 0.0
    x = 0.0
    y = 0.0
    
    • 1
    • 2
    • 3
    • 4
    
    
    • 1

    Java调用ortools求解

    package org.example;
    
    import com.google.ortools.Loader;
    import com.google.ortools.linearsolver.MPConstraint;
    import com.google.ortools.linearsolver.MPObjective;
    import com.google.ortools.linearsolver.MPSolver;
    import com.google.ortools.linearsolver.MPVariable;
    
    /**
     * Simple linear programming example.
     */
    public final class LinearProgrammingExample {
        public static void main(String[] args) {
            Loader.loadNativeLibraries();
            MPSolver solver = MPSolver.createSolver("GLOP");
    
            double infinity = java.lang.Double.POSITIVE_INFINITY;
            // x and y are continuous non-negative variables.
            MPVariable x = solver.makeNumVar(0.0, infinity, "x");
            MPVariable y = solver.makeNumVar(0.0, infinity, "y");
            System.out.println("Number of variables = " + solver.numVariables());
    
            // x + 2*y <= 14.
            MPConstraint c0 = solver.makeConstraint(-infinity, 14.0, "c0");
            c0.setCoefficient(x, 1);
            c0.setCoefficient(y, 2);
    
            // 3*x - y >= 0.
            MPConstraint c1 = solver.makeConstraint(0.0, infinity, "c1");
            c1.setCoefficient(x, 3);
            c1.setCoefficient(y, -1);
    
            // x - y <= 2.
            MPConstraint c2 = solver.makeConstraint(-infinity, 2.0, "c2");
            c2.setCoefficient(x, 1);
            c2.setCoefficient(y, -1);
            System.out.println("Number of constraints = " + solver.numConstraints());
    
            // Maximize 3 * x + 4 * y.
            MPObjective objective = solver.objective();
            objective.setCoefficient(x, 3);
            objective.setCoefficient(y, 4);
            objective.setMaximization();
    
            final MPSolver.ResultStatus resultStatus = solver.solve();
    
            if (resultStatus == MPSolver.ResultStatus.OPTIMAL) {
                System.out.println("Solution:");
                System.out.println("Objective value = " + objective.value());
                System.out.println("x = " + x.solutionValue());
                System.out.println("y = " + y.solutionValue());
            } else {
                System.err.println("The problem does not have an optimal solution!");
            }
    
            System.out.println("\nAdvanced usage:");
            System.out.println("Problem solved in " + solver.wallTime() + " milliseconds");//运行时间
            System.out.println("Problem solved in " + solver.iterations() + " iterations");//迭代次数
        }
    
        private LinearProgrammingExample() {
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
  • 相关阅读:
    分割回文串
    APISpace IP归属地查询接口案例代码
    JavaScript字符串③
    基础算法 - 常见算法模板题(最简洁写法)【上】
    android桌面插件每秒刷新
    Revit二次开发——轴网
    go语言学习-基本概念与流程控制
    Apache Paimon Flink引擎解析
    最通俗易懂的LSTM讲解,一个例子理解通透!!
    .NET5.0和Quartz.NET开发的极简任务调度平台
  • 原文地址:https://blog.csdn.net/qq_43276566/article/details/134016164