Lpmod.java


Lpmod.java


/* Copyright 2021, 狗万app足彩Gurobi Optimization, LLC */ /*这个例子从一个文件中读取一个LP模型并解决它。如果模型可以求解,则找到最小的正变量,并将其上界设为0,然后采用两种方式求解模型:先提前开始,后不提前开始(即。“从头开始”)。* /进口gurobi。*;public class Lpmod {public static void main(String[] args) {if (args. String[] args. String);length < 1) {System.out. length = 1;println("Usage: java Lpmod filename");system . exit (1);} try{//读取模型,确定是否为LP GRBEnv env = new GRBEnv();/ /创建GRBModel = env, args, [0];if (model.get(GRB.IntAttr.IsMIP) != 0) {System.out. ismip .get(GRB.IntAttr.IsMIP) !println(“模型不是一个线性规划”); System.exit(1); } model.optimize(); int status = model.get(GRB.IntAttr.Status); if (status == GRB.Status.INF_OR_UNBD || status == GRB.Status.INFEASIBLE || status == GRB.Status.UNBOUNDED ) { System.out.println("The model cannot be solved because it is " + "infeasible or unbounded"); System.exit(1); } if (status != GRB.Status.OPTIMAL) { System.out.println("Optimization was stopped with status " + status); System.exit(0); } // Find the smallest variable value double minVal = GRB.INFINITY; GRBVar minVar = null; for (GRBVar v : model.getVars()) { double sol = v.get(GRB.DoubleAttr.X); if ((sol > 0.0001) && (sol < minVal) && (v.get(GRB.DoubleAttr.LB) == 0.0)) { minVal = sol; minVar = v; } } System.out.println("\n*** Setting " + minVar.get(GRB.StringAttr.VarName) + " from " + minVal + " to zero ***\n"); minVar.set(GRB.DoubleAttr.UB, 0.0); // Solve from this starting point model.optimize(); // Save iteration & time info double warmCount = model.get(GRB.DoubleAttr.IterCount); double warmTime = model.get(GRB.DoubleAttr.Runtime); // Reset the model and resolve System.out.println("\n*** Resetting and solving " + "without an advanced start ***\n"); model.reset(); model.optimize(); double coldCount = model.get(GRB.DoubleAttr.IterCount); double coldTime = model.get(GRB.DoubleAttr.Runtime); System.out.println("\n*** Warm start: " + warmCount + " iterations, " + warmTime + " seconds"); System.out.println("*** Cold start: " + coldCount + " iterations, " + coldTime + " seconds"); // Dispose of model and environment model.dispose(); env.dispose(); } catch (GRBException e) { System.out.println("Error code: " + e.getErrorCode() + ". " + e.getMessage()); } } }