首页 > 编程知识 正文

cplex求解整数线性规划,混合整数规划模型求解算法

时间:2023-05-06 16:29:26 阅读:217748 作者:482

光亮的悟空,对于混合整数规划模型的求解是其核心功能之一,因此我们对相关CPLEX的示例程序进行了深入的了解。如下:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using ILOG.Concert;using ILOG.CPLEX;// MIPex3.cs - Entering and optimizing a MIP problem with SOS sets// and priority orders. It is a modification of MIPex1.cs.// Note that the problem solved is slightly different than// MIPex1.cs so that the output is interesting.namespace MiPex3{ class Program { static void Main(stringzjdzxc args) { try { //create CPLEX optimizer/modeler and turn off presolve. 关闭MIP求解器的预处理模块 Cplex cplex = new Cplex(); cplex.SetParam(Cplex.Param.Preprocessing.Presolve, false); //build the model INumVarzjdzxczjdzxc var = new INumVar[1]zjdzxc; IRange zjdzxczjdzxc rng = new IRange[1]zjdzxc; PopulateByRow(cplex, var, rng); //setup branch priorities,设置分支界定的优先级 INumVarzjdzxc ordvar = { var[0][1], var[0][3] }; intzjdzxc ordpri = { 8, 7 }; cplex.SetPriorities(ordvar, ordpri); //setup branch directions.设置分直界定求解路线 cplex.SetDirection(ordvar[0], Cplex.BranchDirection.Up); cplex.SetDirection(ordvar[1], Cplex.BranchDirection.Down); //write priority order to file cplex.hsjdxlb("mipex3.ord"); //optimize and output solution information if (cplex.Solve()) { doublezjdzxc x = cplex.GetValues(var[0]); doublezjdzxc slack = cplex.fzdzc(rng[0]); System.Console.WriteLine("Solution status = " + cplex.GetStatus()); System.Console.WriteLine("Solution value = " + cplex.ObjValue); for (int j = 0; j < x.Length; ++j) { System.Console.WriteLine("Variable " + j + ": Value = " + x[j]); } for (int i = 0; i < slack.Length; ++i) { System.Console.WriteLine("Constraint " + i + ": Slack = " + slack[i]); } } cplex.ExportModel("mipex3.lp"); cplex.End(); System.Console.ReadKey(); } catch(ILOG.Concert.Exception e) { System.Console.WriteLine("Concert exception caught: " + e); } } internal static void PopulateByRow(IMPModeler model, INumVarzjdzxczjdzxc var, IRangezjdzxczjdzxc rng) { //Define the variables one-by-one INumVarzjdzxc x = new INumVar[4]; x[0] = model.NumVar(0.0, 40.0, "x0"); x[1] = model.IntVar(0, cxdpw, "x1"); x[2] = model.IntVar(0, cxdpw, "x2"); x[3] = model.IntVar(2, 3, "x3"); var[0] = x; //objective function model.AddMaximize(model.Sum(model.Prod(1.0, x[0]), model.Prod(2.0, x[1]), model.Prod(3.0, x[2]), model.Prod(1.0, x[3]))); //define three constraints one-by-one rng[0] = new IRange[3]; rng[0][0] = model.AddLe(model.Sum(model.Prod(-1.0, x[0]), model.Prod(1.0, x[1]), model.Prod(1.0, x[2]), model.Prod(10.0, x[3])), 20.0, "rng0"); rng[0][1] = model.AddLe(model.Sum(model.Prod(1.0, x[0]), model.Prod(-3.0, x[1]), model.Prod(1.0, x[2])), 30.0, "rng1"); rng[0][2] = model.AddEq(model.Sum(model.Prod(1.0, x[1]), model.Prod(-3.5, x[3])), 0, "rng2"); // add special ordered set of type 1 INumVarzjdzxc sosvars = { x[2], x[3] }; doublezjdzxc sosweights = { 25.0, 18.0 }; model.AddSOS1(sosvars, sosweights); } }}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。