public override int AddSos(object solver, string name, int sostype, int priority, int count, int[] sosvars, double[] weights) { GRBModel grbSolver = solver as GRBModel; if (grbSolver == null) { return(0); } if ((sostype != 2) & (sostype != 1)) { return(0); } GRBVar[] sosGrbVars = new GRBVar[count]; GRBVar[] allgrbVars = grbSolver.GetVars(); for (int i = 0; i < count; i++) { sosGrbVars[i] = allgrbVars[sosvars[i]]; } if (sostype == 2) { grbSolver.AddSOS(sosGrbVars, weights, GRB.SOS_TYPE2); } if (sostype == 1) { grbSolver.AddSOS(sosGrbVars, weights, GRB.SOS_TYPE1); } return(1); }
static void Main() { try { GRBEnv env = new GRBEnv(); GRBModel model = new GRBModel(env); // Create variables double[] ub = { 1, 1, 2 }; double[] obj = { -2, -1, -1 }; string[] names = { "x0", "x1", "x2" }; GRBVar[] x = model.AddVars(null, ub, obj, null, names); // Integrate new variables model.Update(); // Add first SOS1: x0=0 or x1=0 GRBVar[] sosv1 = { x[0], x[1] }; double[] soswt1 = { 1, 2 }; model.AddSOS(sosv1, soswt1, GRB.SOS_TYPE1); // Add second SOS1: x0=0 or x2=0 GRBVar[] sosv2 = { x[0], x[2] }; double[] soswt2 = { 1, 2 }; model.AddSOS(sosv2, soswt2, GRB.SOS_TYPE1); // Optimize model model.Optimize(); for (int i = 0; i < 3; i++) { Console.WriteLine(x[i].Get(GRB.StringAttr.VarName) + " " + x[i].Get(GRB.DoubleAttr.X)); } // Dispose of model and env model.Dispose(); env.Dispose(); } catch (GRBException e) { Console.WriteLine("Error code: " + e.ErrorCode + ". " + e.Message); } }
static void Main() { try { GRBEnv env = new GRBEnv(); GRBModel model = new GRBModel(env); // Create variables double[] ub = {1, 1, 2}; double[] obj = {-2, -1, -1}; string[] names = {"x0", "x1", "x2"}; GRBVar[] x = model.AddVars(null, ub, obj, null, names); // Integrate new variables model.Update(); // Add first SOS1: x0=0 or x1=0 GRBVar[] sosv1 = {x[0], x[1]}; double[] soswt1 = {1, 2}; model.AddSOS(sosv1, soswt1, GRB.SOS_TYPE1); // Add second SOS1: x0=0 or x2=0 GRBVar[] sosv2 = {x[0], x[2]}; double[] soswt2 = {1, 2}; model.AddSOS(sosv2, soswt2, GRB.SOS_TYPE1); // Optimize model model.Optimize(); for (int i = 0; i < 3; i++) Console.WriteLine(x[i].Get(GRB.StringAttr.VarName) + " " + x[i].Get(GRB.DoubleAttr.X)); // Dispose of model and env model.Dispose(); env.Dispose(); } catch (GRBException e) { Console.WriteLine("Error code: " + e.ErrorCode + ". " + e.Message); } }