C#线性规划

1、Google OR-Tools

https://developers.google.com/optimization/introduction/dotnet

支持.Net Core 3.1+

demo:

using System;
using Google.OrTools.LinearSolver;
public class BasicExample
{
    static void Main()
    {
        // Create the linear solver with the GLOP backend.
        Solver solver = Solver.CreateSolver("GLOP");
        // Create the variables x and y.
        Variable x = solver.MakeNumVar(0.0, 1.0, "x");
        Variable y = solver.MakeNumVar(0.0, 2.0, "y");
        Console.WriteLine("Number of variables = " + solver.NumVariables());
        // Create a linear constraint, 0 <= x + y <= 2.
        Constraint ct = solver.MakeConstraint(0.0, 2.0, "ct");
        ct.SetCoefficient(x, 1);
        ct.SetCoefficient(y, 1);
        Console.WriteLine("Number of constraints = " + solver.NumConstraints());
        // Create the objective function, 3 * x + y.
        Objective objective = solver.Objective();
        objective.SetCoefficient(x, 3);
        objective.SetCoefficient(y, 1);
        objective.SetMaximization();
        solver.Solve();
        Console.WriteLine("Solution:");
        Console.WriteLine("Objective value = " + solver.Objective().Value());
        Console.WriteLine("x = " + x.SolutionValue());
        Console.WriteLine("y = " + y.SolutionValue());
    }
}

2、IBM ILOG CPLEX Optimization Studio

https://www.ibm.com/docs/zh/icos/12.8.0.0?topic=tutorial-presenting

支持.Net Framework

demo:

https://blog.csdn.net/weixin_37855929/article/details/85058276

using ILOG.Concert;
using ILOG.CPLEX;
using System;

public class LPex1
{ 
    public static void Main(string[] args)
    {    
        try
        {
            //实例化一个空模型
            Cplex cplexModel = new Cplex();
            //生成决策变量并赋值
            INumVar[][] deVar = new INumVar[1][];
            double[] lb = { 0.0, 0.0, 0.0, 0.0 };
            double[] ub = { double.MaxValue, double.MaxValue, double.MaxValue, double.MaxValue };
            string[] deVarName = { "x11", "x12", "x21", "x22" };
            INumVar[] x = cplexModel.NumVarArray(4, lb, ub, deVarName);
            deVar[0] = x;
            //目标函数
            double[] objCoef = { 50.0, 70.0, 50.0, 70.0 };//目标函数系数(object coefficient)
            cplexModel.AddMinimize(cplexModel.ScalProd(x, objCoef));
            //约束条件
            IRange[][] rng = new IRange[1][];
            rng[0] = new IRange[4];
            rng[0][0] = cplexModel.AddLe(cplexModel.Sum(cplexModel.Prod(1.0, x[3]),
                                                       cplexModel.Prod(1.0, x[1])), 112, "c1");
            rng[0][1] = cplexModel.AddLe(cplexModel.Sum(cplexModel.Prod(1.0, x[0]),
                                                         cplexModel.Prod(1.0, x[2])), 104.0, "c2");
            rng[0][2] = cplexModel.AddEq(cplexModel.Sum(cplexModel.Prod(20.0, x[0]),
                                                         cplexModel.Prod(40.0, x[1])), 3200.0, "c3");
            rng[0][3] = cplexModel.AddEq(cplexModel.Sum(cplexModel.Prod(10.0, x[2]),
                                                        cplexModel.Prod(30.0, x[3])), 2000.0, "c4");
            cplexModel.ExportModel("lpex1.lp");

            if (cplexModel.Solve())
            {
                int nvars = cplexModel.GetValues(deVar[0]).Length;
                for (int j = 0; j < nvars; ++j)
                {
                    cplexModel.Output().WriteLine("Variable   " + j +": Value = " + cplexModel.GetValues(deVar[0])[j] );
                }
            }
            cplexModel.End();
        }
        catch (ILOG.Concert.Exception e)
        {
            System.Console.WriteLine("Concert exception '" + e + "' caught");
        }
        Console.ReadKey();
    }
 

}

发表评论

邮箱地址不会被公开。