| |||||||||||||||||||||||
UG - Examples from 'BCL Reference Manual' Description The following examples are discussed in detail in the 'BCL User Guide and Reference Manual':
Source Files By clicking on a file name, a preview is opened at the bottom of this page.
Data Files xbexpl1.java /******************************************************** * Xpress-BCL Java Example Problems * ================================ * * file xbexpl1.java * ````````````````` * BCL user guide example. * Definition of variables and constraints, * variable arrays and SOS, followed by file output, * solving and printing of solutions. * * (c) 2008-2024 Fair Isaac Corporation * author: S.Heipcke, Jan. 2000, rev. Dec. 2011 ********************************************************/ import com.dashoptimization.*; import java.io.*; public class xbexpl1 { /**************************************************************************/ /* Expl 1: This example corresponds to the one printed in the */ /* BCL User Guide. It shows how to define variables and */ /* constraints, and Special Ordered Sets, followed by file output, */ /* solving and printing of solutions. */ /* Set the following parameter to true to try out a problem formulation */ /* using Special Ordered Sets: */ static final boolean SOS = false; /**************************************************************************/ static final int NJ = 4; /* Number of jobs */ static final int NT = 10; /* Time limit */ /**** DATA ****/ static final double[] DUR = {3, 4, 2, 2}; /* Durations of jobs */ static XPRBvar[] start; /* Start times of jobs */ static XPRBvar[][] delta; /* Binaries for start times */ static XPRBvar z; /* Maximum completion time (makespan) */ static XPRBsos[] set; /* Sets regrouping start times for jobs */ /*************************************************************************/ static void jobsModel(XPRBprob p) throws IOException { XPRBexpr le; int j, t; /****VARIABLES****/ start = new XPRBvar[NJ]; /* Create start time variables */ for (j = 0; j < NJ; j++) start[j] = p.newVar("start"); z = p.newVar("z", XPRB.PL, 0, NT); /* Declare the makespan variable */ delta = new XPRBvar[NJ][NT]; for (j = 0; j < NJ; j++) /* Declare binaries for each job */ for (t = 0; t < (NT - DUR[j] + 1); t++) delta[j][t] = p.newVar("delta" + (j + 1) + (t + 1), XPRB.BV); /****CONSTRAINTS****/ for (j = 0; j < NJ; j++) /* Calculate maximal completion time */ p.newCtr("Makespan", start[j].add(DUR[j]).lEql(z)); p.newCtr("Prec", start[0].add(DUR[0]).lEql(start[2])); /* Precedence rel. between jobs */ for (j = 0; j < NJ; j++) { /* Linking start times and binaries */ le = new XPRBexpr(); for (t = 0; t < (NT - DUR[j] + 1); t++) le.add(delta[j][t].mul((t + 1))); p.newCtr("Link_" + (j + 1), le.eql(start[j])); } for (j = 0; j < NJ; j++) { /* One unique start time for each job */ le = new XPRBexpr(); for (t = 0; t < (NT - DUR[j] + 1); t++) le.add(delta[j][t]); p.newCtr("One_" + (j + 1), le.eql(1)); } /****OBJECTIVE****/ p.setObj(z); /* Define and set objective function */ /****BOUNDS****/ for (j = 0; j < NJ; j++) start[j].setUB(NT - DUR[j] + 1); /* Upper bounds on start time variables */ /****OUTPUT****/ p.print(); /* Print out the problem definition */ p.exportProb(XPRB.MPS, "expl1"); /* Output matrix to MPS file */ } /*************************************************************************/ static void jobsSolve(XPRBprob p) { int j, t, statmip; if (!SOS) for (j = 0; j < NJ; j++) for (t = 0; t < NT - DUR[j] + 1; t++) delta[j][t].setDir(XPRB.PR, 10 * (t + 1)); /* Give highest priority to variables for earlier start times */ else for (j = 0; j < NJ; j++) set[j].setDir(XPRB.DN); /* First branch downwards on sets */ p.setSense(XPRB.MINIM); p.mipOptimize(""); /* Solve the problem as MIP */ statmip = p.getMIPStat(); /* Get the MIP problem status */ if ((statmip == XPRB.MIP_SOLUTION) || (statmip == XPRB.MIP_OPTIMAL)) { /* An integer solution has been found */ System.out.println("Objective: " + p.getObjVal()); for (j = 0; j < NJ; j++) { /* Print the solution for all start times */ System.out.println(start[j].getName() + ": " + start[j].getSol()); for (t = 0; t < NT - DUR[j] + 1; t++) System.out.print(delta[j][t].getName() + ": " + delta[j][t].getSol()); System.out.println(); } } } /*************************************************************************/ public static void main(String[] args) throws Exception { try (XPRBprob p = new XPRBprob("jobs"); /* Initialize BCL and create a new problem */ XPRBexprContext context = new XPRBexprContext() /* Release XPRBexpr instances at end of block. */) { if (!SOS) jobsModel(p); /* Basic problem definition */ else jobsModelb(p); /* Formulation using SOS */ jobsSolve(p); /* Solve and print solution */ } } /*************************************************************************/ static void jobsModelb(XPRBprob p) throws IOException { /**** SOS-formulation ****/ XPRBexpr le; int j, t; /****VARIABLES****/ start = new XPRBvar[NJ]; /* Create start time variables */ for (j = 0; j < NJ; j++) start[j] = p.newVar("start"); z = p.newVar("z", XPRB.PL, 0, NT); /* Declare the makespan variable */ delta = new XPRBvar[NJ][NT]; for (j = 0; j < NJ; j++) /* Declare binaries for each job */ for (t = 0; t < (NT - DUR[j] + 1); t++) delta[j][t] = p.newVar("delta" + (j + 1) + (t + 1), XPRB.PL, 0, 1); /****CONSTRAINTS****/ for (j = 0; j < NJ; j++) /* Calculate maximal completion time */ p.newCtr("Makespan", start[j].add(DUR[j]).lEql(z)); p.newCtr("Prec", start[0].add(DUR[0]).lEql(start[2])); /* Precedence rel. betw. jobs */ for (j = 0; j < NJ; j++) { /* Linking start times and binaries */ le = new XPRBexpr(); for (t = 0; t < (NT - DUR[j] + 1); t++) le.add(delta[j][t].mul((t + 1))); p.newCtr("Link_" + (j + 1), le.eql(start[j])); } for (j = 0; j < NJ; j++) { /* One unique start time for each job */ le = new XPRBexpr(); for (t = 0; t < (NT - DUR[j] + 1); t++) le.add(delta[j][t]); p.newCtr("One_" + (j + 1), le.eql(1)); } /****OBJECTIVE****/ p.setObj(z); /* Define and set objective function */ /****BOUNDS****/ for (j = 0; j < NJ; j++) start[j].setUB(NT - DUR[j] + 1); /* Upper bounds on start time variables */ /****SETS****/ set = new XPRBsos[NJ]; for (j = 0; j < NJ; j++) { le = new XPRBexpr(); for (t = 0; t < (NT - DUR[j] + 1); t++) le.add(delta[j][t].mul((t + 1))); set[j] = p.newSos("sosj", XPRB.S1, le); } /****OUTPUT****/ p.print(); /* Print out the problem definition */ p.exportProb(XPRB.MPS, "expl1"); /* Output matrix to MPS file */ } } | |||||||||||||||||||||||
© Copyright 2024 Fair Isaac Corporation. |