| |||||||||||||
Chgprobs - Working with multiple problems Description This example defines 3 very small problems, making changes to the problem definition after matrix generation and retrieving solution information. It also shows BCL warnings.
Source Files By clicking on a file name, a preview is opened at the bottom of this page.
xbexpl.cs /******************************************************** Xpress-BCL C# Example Problems ============================== file xbexpl.cs `````````````` Working with multiple problems. (c) 2008-2024 Fair Isaac Corporation authors: S.Heipcke, D.Brett. ********************************************************/ using System; using System.Text; using System.IO; using BCL; namespace Examples { public class TestMultiple { const bool CHGCTR = true; /* Accessing and modifying constraints */ const bool CHGVAR = true; /* Accessing and modifying variables */ const bool UNBOUNDED = true; /* Solve a small unbounded problem */ XPRBprob p2 = new XPRBprob("expl2"); XPRBprob p3 = new XPRBprob("expl3"); XPRBprob p4 = new XPRBprob("expl4"); /***********************************************************************/ public static void Main() { XPRB.init(); TestMultiple TestInstance = new TestMultiple(); if(CHGCTR) TestInstance.expl2(); if(CHGVAR) TestInstance.expl3(); if(UNBOUNDED) { TestInstance.expl4(); if(CHGCTR && CHGVAR) TestInstance.expl5(); } return; } /***********************************************************************/ /**** Expl 2: changing bounds and operations on constraints ****/ void expl2() { XPRBvar[] x = new XPRBvar[5]; XPRBctr[] ctr = new XPRBctr[4]; XPRBexpr lobj = new XPRBexpr(); double[] objcof = {2.0,1.0,1.0,1.0,0}; int i; /* Define 5 integer variables in 0,...,100 */ for(i=0;i<5;i++) x[i] = p2.newVar("x_" + i, BCLconstant.XPRB_UI, 0, 100); /* Create the constraints: ctr0: x0 +10 <= x1 ctr1: x1 <= x3 ctr2: x1 + 8 <= x2 */ ctr[0]=p2.newCtr("ctr0", x[0] + 10 <= x[1]); ctr[1]=p2.newCtr("ctr1", x[1] <= x[3]); ctr[2]=p2.newCtr("ctr2", x[1] + 8 <= x[2]); lobj = new XPRBexpr(0); ; for(i=0;i<5;i++) lobj += objcof[i]*x[i]; p2.setObj(p2.newCtr("OBJ",lobj)); /* Select objective function */ p2.setSense(BCLconstant.XPRB_MINIM); /* Set objective sense to minimization */ System.Console.WriteLine("Problem status: " + p2.getProbStat() + " LP status: "); System.Console.WriteLine(p2.getLPStat() + " MIP status: " + p2.getMIPStat()); p2.exportProb(BCLconstant.XPRB_LP,"expl2"); /* Matrix generation and output */ p2.print(); /* Print current problem definition */ p2.lpOptimize(); /* Solve the LP */ System.Console.WriteLine("Problem status: " + p2.getProbStat() + " LP status: "); System.Console.WriteLine(p2.getLPStat() + " MIP status: " + p2.getMIPStat()); System.Console.WriteLine("Objective: " + p2.getObjVal()); for(i=0;i<4;i++) /* Print solution values */ System.Console.Write(x[i].getName() + ":" + x[i].getSol() + " "); System.Console.WriteLine(); ctr[0].setRange(-15,-5); /* Transform constraint into range constraint */ System.Console.WriteLine(); System.Console.WriteLine("<<<<<<<<Constraint transformed into range:>>>>>>>>"); p2.print(); /* Print current problem definition */ for(i=0;i<4;i++) { x[i].print(); /* Print new variable bounds */ System.Console.Write(" "); } System.Console.WriteLine(); p2.mipOptimize(); /* Solve global */ System.Console.Write("Problem status: " + p2.getProbStat() + " LP status: "); System.Console.WriteLine(p2.getLPStat() + " MIP status: " + p2.getMIPStat()); System.Console.WriteLine("Objective: " + p2.getObjVal()); for(i=0;i<4;i++) /* Print solution values */ System.Console.Write(x[i].getName() + ":" + x[i].getSol() + " "); System.Console.WriteLine(); ctr[0].setType(BCLconstant.XPRB_L); /* Change range constraint back to constraint */ System.Console.WriteLine(); System.Console.WriteLine("<<<<<<<<Constraint restored to inequality:>>>>>>>>"); p2.print(); /* Print current problem definition */ ctr[0].setTerm(-10); /* Set new RHS value */ System.Console.WriteLine("<<<<<<<<Restore original RHS value:>>>>>>>>"); p2.print(); /* Print current problem definition */ x[1].setLB(15); /* Change the bound on a variable */ System.Console.WriteLine("<<<<<<<<Variable bound changed:>>>>>>>>"); for(i=0;i<4;i++) { x[i].print(); /* Print new variable bounds */ System.Console.Write(" "); } System.Console.WriteLine(); p2.mipOptimize(); /* Solve global */ System.Console.Write("Problem status: " + p2.getProbStat() + " LP status: "); System.Console.WriteLine(p2.getLPStat() + " MIP status: " + p2.getMIPStat()); System.Console.WriteLine("Objective: " + p2.getObjVal()); for(i=0;i<4;i++) /* Print solution values */ System.Console.Write(x[i].getName() + ":" + x[i].getSol() + " "); System.Console.WriteLine(); /* Change constraint coefficient and RHS */ ctr[1].setTerm(x[1],-3); /* ctr1: x1 <= 3*x3 */ ctr[0].addTerm(-10); /* ctr0: x0 + 20 <= x1 */ System.Console.WriteLine("<<<<<<<<Constraint coefficient and RHS changed:>>>>>>>>"); for(i=0;i<3;i++) ctr[i].print(); for(i=0;i<4;i++) { x[i].print(); /* Print new variable bounds */ System.Console.Write(" "); } System.Console.WriteLine(); p2.mipOptimize(); /* Solve global */ System.Console.Write("Problem status: " + p2.getProbStat() + " LP status: "); System.Console.WriteLine(p2.getLPStat() + " MIP status: " + p2.getMIPStat()); System.Console.WriteLine("Objective: " + p2.getObjVal()); for(i=0;i<4;i++) /* Print solution values */ System.Console.Write(x[i].getName() + ":" + x[i].getSol() + " "); System.Console.WriteLine(); /* Change constraint type */ ctr[2].setType(BCLconstant.XPRB_G); /* ctr2: x1 + 8 >= x2 */ System.Console.WriteLine(); System.Console.WriteLine("<<<<<<<<Constraint type changed:>>>>>>>>"); for(i=0;i<3;i++) ctr[i].print(); for(i=0;i<4;i++) { x[i].print(); /* Print variable bounds */ System.Console.Write(" "); } System.Console.WriteLine(); p2.mipOptimize(); /* Solve global */ System.Console.Write("Problem status: " + p2.getProbStat() + " LP status: "); System.Console.WriteLine(p2.getLPStat() + " MIP status: " + p2.getMIPStat()); System.Console.WriteLine("Objective: " + p2.getObjVal()); for(i=0;i<4;i++) /* Print solution values */ System.Console.Write(x[i].getName() + ":" + x[i].getSol() + " "); System.Console.WriteLine(); /* Add another constraint ctr3: x0 +37<= x2 */ ctr[3] = p2.newCtr("ctr3", x[0]+37 <= x[2]); System.Console.WriteLine(); System.Console.WriteLine("<<<<<<<<Constraint added:>>>>>>>>"); p2.print(); for(i=0;i<4;i++) { x[i].print(); /* Print variable bounds */ System.Console.Write(" "); } System.Console.WriteLine(); p2.mipOptimize(); /* Solve global */ System.Console.Write("Problem status: " + p2.getProbStat() + " LP status: "); System.Console.WriteLine(p2.getLPStat() + " MIP status: " + p2.getMIPStat()); System.Console.WriteLine("Objective: " + p2.getObjVal()); for(i=0;i<4;i++) /* Print solution values */ System.Console.Write(x[i].getName() + ":" + x[i].getSol() + " "); System.Console.WriteLine(); /* delete a constraint */ p2.delCtr(ctr[2]); System.Console.WriteLine("<<<<<<<<Constraint deleted:>>>>>>>>"); p2.print(); for(i=0;i<4;i++) { x[i].print(); /* Print variable bounds */ System.Console.Write(" "); } System.Console.WriteLine(); p2.mipOptimize(); /* Solve global */ System.Console.Write("Problem status: " + p2.getProbStat() + " LP status: "); System.Console.WriteLine(p2.getLPStat() + " MIP status: " + p2.getMIPStat()); System.Console.WriteLine("Objective: " + p2.getObjVal()); for(i=0;i<4;i++) /* Print solution values */ System.Console.Write(x[i].getName() + ":" + x[i].getSol() + " "); System.Console.WriteLine(); } /**** Expl 3: Knapsack problem: accessing variables ****/ void expl3() { XPRBvar[] x = new XPRBvar[4]; XPRBexpr le, lobj; XPRBctr ctr; double[] coeff = {30.0, 32.0, 27.0, 11.0}; double[] objcof = {9.0, 15.0, 8.0, 3.0}; int i; for(i=0;i<4;i++) /* Define 4 binary variables */ x[i] = p3.newVar("x_" + i, BCLconstant.XPRB_BV); /* Create the knapsack constraint: sum_i coeff[i]*x[i] <= 70 */ le = new XPRBexpr(); for(i=0;i<4;i++) le += coeff[i]*x[i]; ctr = p3.newCtr("sumkn", le <= 70); lobj = new XPRBexpr(); for(i=0;i<4;i++) lobj += objcof[i]*x[i]; p3.setObj(p3.newCtr("OBJ",lobj)); /* Set objective function */ /* p3.print(); */ /* Uncomment to print the problem */ p3.exportProb(BCLconstant.XPRB_MPS,"expl3"); /* Matrix output in MPS format */ p3.setSense(BCLconstant.XPRB_MAXIM); /* Change to maximization */ p3.mipOptimize(); /* Solve global */ System.Console.WriteLine("Objective: " + p3.getObjVal()); /* Get objective value */ for(i=0;i<4;i++) /* Print the solution */ System.Console.WriteLine(x[i].getName() + ": " + x[i].getSol() + " (rc:" + x[i].getRCost() + ")"); System.Console.WriteLine("Dual: " + ctr.getDual() + ", slack:" + ctr.getSlack()); /* Print dual & slack values */ System.Console.WriteLine(); System.Console.WriteLine("<<<<<<<<Variable type changed from BV to UI>>>>>>>>"); x[1].setType(BCLconstant.XPRB_UI); /* Change variable type */ System.Console.Write(x[1].getName() + ": bounds: " + x[1].getLB() + " " + x[1].getUB()); System.Console.WriteLine(", type: " + x[1].getType() + ", index: " + x[1].getColNum()); p3.mipOptimize(); /* Re-solve global */ System.Console.WriteLine("Objective: " + p3.getObjVal()); /* Get objective value */ System.Console.WriteLine(); System.Console.WriteLine("<<<<<<<<Variable bound changed: no matrix regeneration>>>>>>>>"); x[1].setUB(3); /* Change variable bound */ System.Console.Write(x[1].getName() + ": bounds: " + x[1].getLB() + " " + x[1].getUB()); System.Console.WriteLine(", type: " + x[1].getType() + ", index: " + x[1].getColNum()); p3.mipOptimize(); /* Re-solve global */ System.Console.WriteLine("Objective: " + p3.getObjVal()); /* Get objective value */ for(i=0;i<4;i++) /* Print solution values */ System.Console.Write(x[i].getName() + ":" + x[i].getSol() + " "); System.Console.WriteLine(); System.Console.WriteLine(); System.Console.WriteLine("<<<<<<<<Variable type changed from UI to PI>>>>>>>>"); x[1].setType(BCLconstant.XPRB_PI); /* Change variable type */ x[1].setLim(2); /* Set the integer limit for the partial integer variable */ x[1].print(); System.Console.WriteLine(); /* Print current variable definition */ p3.mipOptimize(); /* Re-solve global */ System.Console.WriteLine("Objective: " + p3.getObjVal()); /* Get objective value */ for(i=0;i<4;i++) /* Print the solution */ System.Console.WriteLine(x[i].getName() + ": " + x[i].getSol() + " (rc:" + x[i].getRCost() + ")"); System.Console.WriteLine("Dual: " + ctr.getDual() + ", slack:" + ctr.getSlack()); /* Print dual & slack values */ } /****Expl 4: a small unbounded problem ****/ void expl4() { XPRBvar[] x = new XPRBvar[2]; int i; p4=new XPRBprob("expl4"); /* Create a new problem */ /* Define 2 variables in [0,PLUSINFINITY] */ for(i=0;i<2;i++) x[i]=p4.newVar("x_" + i); /* Create the constraints: ctr0: 4*x0 + x1 >= 4 ctr1: x0 + x1 >= 3 ctr2: x0 + 2*x1 >= 4 */ p4.newCtr("c1", 4*x[0] + x[1] >= 4); p4.newCtr("c2", x[0] + x[1] >= 3); p4.newCtr("c3", x[0] + 2*x[1] >= 4); p4.setObj(p4.newCtr("OBJ", x[0]+x[1])); /* Define and set obj. function */ /* Try out the effect of solving without presolve: * XPRSprob xprsp = p4.getXPRSprob(); * xprsp.Presolve = 0; * xprsp.MIPPresolve = 0; */ p4.setSense(BCLconstant.XPRB_MAXIM); /* Change to maximization */ p4.lpOptimize(); /* Solve the LP */ System.Console.Write("Problem status: " + p4.getProbStat() + " LP status: "); System.Console.WriteLine(p4.getLPStat() + " MIP status: " + p4.getMIPStat()); System.Console.WriteLine("Objective: " + p4.getObjVal()); /* Get objective value */ for(i=0;i<2;i++) /* Print solution values */ System.Console.Write(x[i].getName() + ":" + x[i].getSol() + " "); System.Console.WriteLine(); } /***Expl5: Working with different problems****/ void expl5() { int i; System.Console.WriteLine(); System.Console.WriteLine("<<<<<<<<Re-solve problem " + p2.getName() + ">>>>>>>>"); p2.mipOptimize(); /* Solve global */ System.Console.Write("Problem status: " + p2.getProbStat() + " LP status: "); System.Console.WriteLine(p2.getLPStat() + " MIP status: " + p2.getMIPStat()); System.Console.WriteLine("Objective: " + p2.getObjVal()); /* Get objective value */ for(i=0;i<4;i++) /* Print solution values */ System.Console.Write("x_" + i + ":" + p2.getVarByName("x_" + i).getSol() + " "); System.Console.WriteLine(); System.Console.WriteLine(); System.Console.WriteLine("<<<<<<<<Delete problem " + p4.getName() + ">>>>>>>>"); p4.print(); /* Print the problem def. */ System.Console.WriteLine("<<<<<<<<Re-solve problem " + p3.getName() + " and print it>>>>>>>>"); p3.print(); /* Print the problem def. */ p3.mipOptimize(); /* Solve global */ System.Console.Write("Problem status: " + p3.getProbStat() + " LP status: "); System.Console.WriteLine(p3.getLPStat() + " MIP status: " + p3.getMIPStat()); System.Console.WriteLine("Objective: " + p3.getObjVal()); /* Get objective value */ for(i=0;i<4;i++) /* Print solution values */ System.Console.Write("x_" + i + ":" + p3.getVarByName("x_" + i).getSol() + " "); System.Console.WriteLine(); } } } | |||||||||||||
© Copyright 2024 Fair Isaac Corporation. |