params_cs.cs


params_cs.cs


/* Copyright 2021, 狗万app足彩Gurobi Optimization, LLC */ /*使用与模型相关的参数。用不同的参数集求解一个MIP,只需几秒钟。选择MIP间隙最小的那个,继续优化,直到找到最优解。* /使用系统;使用Gurobi;class params_cs {static void Main(string[] args) {if (args. class);长度< 1){Console.Out。WriteLine(“用法:params_cs文件名”);返回;} try{//读取模型并验证它是一个MIP GRBEnv env = new GRBEnv();/ /创建GRBModel对象 if (m.IsMIP == 0) { Console.WriteLine("The model is not an integer program"); Environment.Exit(1); } // Set a 2 second time limit m.Parameters.TimeLimit = 2.0; // Now solve the model with different values of MIPFocus GRBModel bestModel = new GRBModel(m); bestModel.Optimize(); for (int i = 1; i <= 3; ++i) { m.Reset(); m.Parameters.MIPFocus = i; m.Optimize(); if (bestModel.MIPGap > m.MIPGap) { GRBModel swap = bestModel; bestModel = m; m = swap; } } // Finally, delete the extra model, reset the time limit and // continue to solve the best model to optimality m.Dispose(); bestModel.Parameters.TimeLimit = GRB.INFINITY; bestModel.Optimize(); Console.WriteLine("Solved with MIPFocus: " + bestModel.Parameters.MIPFocus); // Clean up bestModel and environment bestModel.Dispose(); env.Dispose(); } catch (GRBException e) { Console.WriteLine("Error code: " + e.ErrorCode + ". " + e.Message); } } }