/********************************************************
* Xpress-BCL Java Example Problems
* ================================
*
* file xbexpl.java
* ````````````````
* Working with multiple problems.
*
* (c) 2008-2024 Fair Isaac Corporation
* author: S.Heipcke, Jan. 2000, rev. Mar. 2011
********************************************************/
import com.dashoptimization.*;
import java.io.*;
/********************************************************/
/* This file illustrates how to */
/* - do changes to the problem definition */
/* - retrieve solution information */
/* - define and work with several problems */
/* */
/* Set at least one of the following options to true. */
/* It is possible to define all together. In this case */
/* the last function (expl5) that shows how to switch */
/* between problems is activated too. */
/********************************************************/
public class xbexpl {
static final boolean CHGCTR = true; /* Accessing & modifying constraints */
static final boolean CHGVAR = true; /* Accessing & modifying variables */
static final boolean UNBOUNDED = true; /* Solve a small unbounded problem */
static XPRBprob p2, p3, p4;
/***********************************************************************/
public static void main(String[] args) {
try (XPRB bcl = new XPRB(); /* Initialize BCL */
XPRBexprContext context =
new XPRBexprContext() /* Release XPRBexpr instances at end of block. */) {
try {
if (CHGCTR) expl2(bcl);
if (CHGVAR) expl3(bcl);
if (UNBOUNDED) {
expl4(bcl);
if (CHGCTR && CHGVAR) expl5(bcl);
}
} catch (IOException e) {
System.err.println(e.getMessage());
System.exit(1);
} finally {
if (p4 != null) p4.close();
if (p3 != null) p3.close();
if (p2 != null) p2.close();
}
}
}
/***********************************************************************/
/**** Expl 2: changing bounds and operations on constraints ****/
static void expl2(XPRB bcl) throws IOException {
XPRBvar[] x;
XPRBctr[] ctr;
XPRBexpr lobj;
double[] objcof = {2.0, 1.0, 1.0, 1.0, 0};
int i;
p2 = bcl.newProb("expl2"); /* Create a new problem */
/* Define 5 integer variables in 0,...,100 */
x = new XPRBvar[5];
for (i = 0; i < 5; i++) x[i] = p2.newVar("x_" + i, XPRB.UI, 0, 100);
/* Create the constraints:
ctr0: x0 +10 <= x1
ctr1: x1 <= x3
ctr2: x1 + 8 <= x2 */
ctr = new XPRBctr[4];
ctr[0] = p2.newCtr("ctr0", x[0].add(10).lEql(x[1]));
ctr[1] = p2.newCtr("ctr1", x[1].lEql(x[3]));
ctr[2] = p2.newCtr("ctr2", x[1].add(8).lEql(x[2]));
lobj = new XPRBexpr();
for (i = 0; i < 5; i++) lobj.add(x[i].mul(objcof[i]));
p2.setObj(lobj); /* Select objective function */
p2.setSense(XPRB.MINIM); /* Set objective sense to minimization */
System.out.println(
"Problem status: "
+ p2.getProbStat()
+ " LP status: "
+ p2.getLPStat()
+ " MIP status: "
+ p2.getMIPStat());
p2.exportProb(XPRB.LP, "expl2"); /* Matrix generation and output */
p2.print(); /* Print current problem definition */
p2.lpOptimize(""); /* Solve the LP */
System.out.println(
"Problem status: "
+ p2.getProbStat()
+ " LP status: "
+ p2.getLPStat()
+ " MIP status: "
+ p2.getMIPStat());
System.out.println("Objective: " + p2.getObjVal());
for (i = 0; i < 4; i++) /* Print solution values */
System.out.print(x[i].getName() + ":" + x[i].getSol() + " ");
System.out.println();
ctr[0].setRange(-15, -5); /* Transform constraint into range constr. */
System.out.println("\n<<<<<<<<Constraint transformed into range:>>>>>>>>");
p2.print(); /* Print current problem definition */
for (i = 0; i < 4; i++) {
x[i].print();
System.out.print(" ");
} /* Print new variable bounds */
System.out.println();
p2.mipOptimize(""); /* Solve the MIP */
System.out.println(
"Problem status: "
+ p2.getProbStat()
+ " LP status: "
+ p2.getLPStat()
+ " MIP status: "
+ p2.getMIPStat());
System.out.println("Objective: " + p2.getObjVal());
for (i = 0; i < 4; i++) /* Print solution values */
System.out.print(x[i].getName() + ":" + x[i].getSol() + " ");
System.out.println();
ctr[0].setType(XPRB.L); /* Change range constraint back to constraint */
System.out.println("\n<<<<<<<<Constraint restored to inequality:>>>>>>>>");
p2.print(); /* Print current problem definition */
ctr[0].setTerm(-10); /* Set new RHS value */
System.out.println("<<<<<<<<Restore original RHS value:>>>>>>>>");
p2.print(); /* Print current problem definition */
x[1].setLB(15); /* Change the bound on a variable */
System.out.println("<<<<<<<<Variable bound changed:>>>>>>>>");
for (i = 0; i < 4; i++) {
x[i].print();
System.out.print(" ");
} /* Print new variable bounds */
System.out.println();
p2.mipOptimize(""); /* Solve the MIP */
System.out.println(
"Problem status: "
+ p2.getProbStat()
+ " LP status: "
+ p2.getLPStat()
+ " MIP status: "
+ p2.getMIPStat());
System.out.println("Objective: " + p2.getObjVal());
for (i = 0; i < 4; i++) /* Print solution values */
System.out.print(x[i].getName() + ":" + x[i].getSol() + " ");
System.out.println();
/* Change constraint coefficient and RHS */
ctr[1].setTerm(x[1], -3); /* ctr1: x1 <= 3*x3 */
ctr[0].addTerm(-10); /* ctr0: x0 + 20 <= x1 */
System.out.println("\n<<<<<<<<Constraint coefficient and RHS changed:>>>>>>>>");
for (i = 0; i < 3; i++) ctr[i].print();
for (i = 0; i < 4; i++) {
x[i].print();
System.out.print(" ");
} /* Print new variable bounds */
System.out.println();
p2.mipOptimize(""); /* Solve the MIP */
System.out.println(
"Problem status: "
+ p2.getProbStat()
+ " LP status: "
+ p2.getLPStat()
+ " MIP status: "
+ p2.getMIPStat());
System.out.println("Objective: " + p2.getObjVal());
for (i = 0; i < 4; i++) /* Print solution values */
System.out.print(x[i].getName() + ":" + x[i].getSol() + " ");
System.out.println();
/* Change constraint type */
ctr[2].setType(XPRB.G); /* ctr2: x1 + 8 >= x2 */
System.out.println("\n<<<<<<<<Constraint type changed:>>>>>>>>");
for (i = 0; i < 3; i++) ctr[i].print();
for (i = 0; i < 4; i++) {
x[i].print();
System.out.print(" ");
} /* Print variable bounds */
System.out.println();
p2.mipOptimize(""); /* Solve the MIP */
System.out.println(
"Problem status: "
+ p2.getProbStat()
+ " LP status: "
+ p2.getLPStat()
+ " MIP status: "
+ p2.getMIPStat());
System.out.println("Objective: " + p2.getObjVal());
for (i = 0; i < 4; i++) /* Print solution values */
System.out.print(x[i].getName() + ":" + x[i].getSol() + " ");
System.out.println();
/* Add another constraint ctr3: x0 +37<= x2 */
ctr[3] = p2.newCtr("ctr3", x[0].add(37).lEql(x[2]));
System.out.println("\n<<<<<<<<Constraint added:>>>>>>>>");
p2.print();
for (i = 0; i < 4; i++) {
x[i].print();
System.out.print(" ");
} /* Print variable bounds */
System.out.println();
p2.mipOptimize(""); /* Solve the MIP */
System.out.println(
"Problem status: "
+ p2.getProbStat()
+ " LP status: "
+ p2.getLPStat()
+ " MIP status: "
+ p2.getMIPStat());
System.out.println("Objective: " + p2.getObjVal());
for (i = 0; i < 4; i++) /* Print solution values */
System.out.print(x[i].getName() + ":" + x[i].getSol() + " ");
System.out.println();
/* Delete a constraint */
p2.delCtr(ctr[2]);
System.out.println("\n<<<<<<<<Constraint deleted:>>>>>>>>");
p2.print();
for (i = 0; i < 4; i++) {
x[i].print();
System.out.print(" ");
} /* Print variable bounds */
System.out.println();
p2.mipOptimize(""); /* Solve the MIP */
System.out.println(
"Problem status: "
+ p2.getProbStat()
+ " LP status: "
+ p2.getLPStat()
+ " MIP status: "
+ p2.getMIPStat());
System.out.println("Objective: " + p2.getObjVal());
for (i = 0; i < 4; i++) /* Print solution values */
System.out.print(x[i].getName() + ":" + x[i].getSol() + " ");
System.out.println();
}
/**** Expl 3: Knapsack problem: accessing variables ****/
static void expl3(XPRB bcl) throws IOException {
XPRBvar[] x;
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;
p3 = bcl.newProb("expl3"); /* Create a new problem */
x = new XPRBvar[4];
for (i = 0; i < 4; i++) /* Define 4 binary variables */ x[i] = p3.newVar("x_" + i, XPRB.BV);
/* Create the knapsack constraint:
sum_i coeff[i]*x[i] <= 70 */
le = new XPRBexpr();
for (i = 0; i < 4; i++) le.add(x[i].mul(coeff[i]));
ctr = p3.newCtr("sumkn", le.lEql(70));
lobj = new XPRBexpr();
for (i = 0; i < 4; i++) lobj.add(x[i].mul(objcof[i]));
p3.setObj(lobj); /* Set objective function */
/* p3.print(); */
/* Uncomment to print the problem */
p3.exportProb(XPRB.MPS, "expl3"); /* Matrix output in MPS format */
p3.setSense(XPRB.MAXIM); /* Change to maximization */
p3.mipOptimize(""); /* Solve the MIP */
System.out.println("Objective: " + p3.getObjVal()); /* Get objective value */
for (i = 0; i < 4; i++) /* Print the solution */
System.out.println(x[i].getName() + ": " + x[i].getSol() + " (rc:" + x[i].getRCost() + "),");
System.out.println("Dual: " + ctr.getDual() + " slack: " + ctr.getSlack());
/* Print dual & slack values */
System.out.println("\n<<<<<<<<Variable type changed from BV to UI>>>>>>>>");
x[1].setType(XPRB.UI); /* Change variable type */
System.out.println(
x[1].getName()
+ ": bounds: "
+ x[1].getLB()
+ " "
+ x[1].getUB()
+ ", type: "
+ x[1].getType()
+ ", index: "
+ x[1].getColNum());
p3.mipOptimize(""); /* Re-solve the MIP */
System.out.println("Objective: " + p3.getObjVal()); /* Get objective value */
System.out.println("\n<<<<<<<<Variable bound changed: no matrix regeneration>>>>>>>>");
x[1].setUB(3); /* Change variable bound */
System.out.println(
x[1].getName()
+ ": bounds: "
+ x[1].getLB()
+ " "
+ x[1].getUB()
+ ", type: "
+ x[1].getType()
+ ", index: "
+ x[1].getColNum());
p3.mipOptimize(""); /* Re-solve the MIP */
System.out.println("Objective: " + p3.getObjVal()); /* Get objective value */
for (i = 0; i < 4; i++) /* Print solution values */
System.out.print(x[i].getName() + ":" + x[i].getSol() + " ");
System.out.println("\n\n<<<<<<<<Variable type changed from UI to PI>>>>>>>>");
x[1].setType(XPRB.PI); /* Change variable type */
x[1].setLim(2); /* Set the integer limit for
the partial integer variable */
x[1].print();
System.out.println(); /* Print current variable definition */
p3.mipOptimize(""); /* Re-solve the MIP */
System.out.println("Objective: " + p3.getObjVal()); /* Get objective value */
for (i = 0; i < 4; i++) /* Print the solution */
System.out.println(x[i].getName() + ": " + x[i].getSol() + " (rc:" + x[i].getRCost() + "),");
System.out.println("Dual: " + ctr.getDual() + " slack: " + ctr.getSlack());
/* Print dual & slack values */
}
/****Expl 4: a small unbounded problem ****/
static void expl4(XPRB bcl) {
XPRBvar[] x;
int i;
p4 = bcl.newProb("expl4"); /* Create a new problem */
/* Define 2 variables in [0,PLUSINFINITY] */
x = new XPRBvar[2];
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", x[0].mul(4).add(x[1]).gEql(4));
p4.newCtr("c2", x[0].add(x[1]).gEql(3));
p4.newCtr("c3", x[0].add(x[1].mul(2)).gEql(4));
p4.setObj(x[0].add(x[1])); /* Define and set objective function */
p4.setSense(XPRB.MAXIM); /* Change to maximization */
p4.lpOptimize(""); /* Solve the LP */
System.out.println(
"Problem status: "
+ p4.getProbStat()
+ " LP status: "
+ p4.getLPStat()
+ " MIP status: "
+ p4.getMIPStat());
System.out.println("Objective: " + p4.getObjVal()); /* Get objective value */
for (i = 0; i < 2; i++) /* Print solution values */
System.out.print(x[i].getName() + ":" + x[i].getSol() + " ");
System.out.println();
}
/***Expl5: Working with different problems****/
static void expl5(XPRB bcl) {
int i;
System.out.println("\n<<<<<<<<Re-solve problem " + p2.getName() + ">>>>>>>>");
p2.mipOptimize(""); /* Solve the MIP */
System.out.println(
"Problem status: "
+ p2.getProbStat()
+ " LP status: "
+ p2.getLPStat()
+ " MIP status: "
+ p2.getMIPStat());
System.out.println("Objective: " + p2.getObjVal()); /* Get objective value */
for (i = 0; i < 4; i++) /* Print solution values */
System.out.print("x_" + i + ":" + p2.getVarByName("x_" + i).getSol());
/* In C and C++, here we delete the problem "expl4". With Java, the
closest correspondence is to explicitely close this problem. */
System.out.println("\n\n<<<<<<<<Finalize prob4>>>>>>>>");
p4.print();
p4.close();
p4 = null;
System.out.println("\n\n<<<<<<<<Re-solve problem " + p3.getName() + " and print it>>>>>>>>");
p3.print(); /* Print the problem def. */
p3.mipOptimize(""); /* Solve the MIP */
System.out.println(
"Problem status: "
+ p3.getProbStat()
+ " LP status: "
+ p3.getLPStat()
+ " MIP status: "
+ p3.getMIPStat());
System.out.println("Objective: " + p3.getObjVal()); /* Get objective value */
for (i = 0; i < 4; i++) /* Print solution values */
System.out.print("x_" + i + ":" + p3.getVarByName("x_" + i).getSol());
System.out.println();
}
}
|