| |||||||||||
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.c /******************************************************** BCL Example Problems ==================== file xbexpl.c ````````````` Working with multiple problems. (c) 2008-2024 Fair Isaac Corporation author: S.Heipcke, Jan. 2000, rev. Mar. 2011 ********************************************************/ #include <stdio.h> #include "xprb.h" /********************************************************/ /* This file illustrates how to */ /* - do changes to the problem definition */ /* - retrieve solution information */ /* - define and work with several problems */ /* */ /* Define at least one of the following options. It is */ /* possible to define all together. In this case the */ /* last function (expl5) that shows how to switch */ /* between problems is activated too. */ /********************************************************/ #define CHGCTR /* Accessing and modifying constraints */ #define CHGVAR /* Accessing and modifying variables */ #define UNBOUNDED /* Solve a small unbounded problem */ void expl2(void); void expl3(void); void expl4(void); void expl5(void); XPRBprob prob2, prob3, prob4; /* Problem definitions */ /***********************************************************************/ int main(int argc, char **argv) { #ifdef CHGCTR expl2(); #endif #ifdef CHGVAR expl3(); #endif #ifdef UNBOUNDED expl4(); #ifdef CHGCTR #ifdef CHGVAR expl5(); #endif #endif #endif return 0; } /***********************************************************************/ /**** Expl 2: changing bounds and operations on constraints ****/ void expl2(void) { XPRBvar x[5]; XPRBctr ctr[4], cobj; double objcof[]={2.0,1.0,1.0,1.0,0}; int i; prob2=XPRBnewprob("expl2"); /* Create a new problem */ /* Define 5 integer variables in 0,...,100 */ for(i=0;i<5;i++) x[i]=XPRBnewvar(prob2,XPRB_UI,XPRBnewname("x_%d",i),0,100); /* Create the constraints: ctr0: x0 +10 <= x1 ctr1: x1 <= x3 ctr2: x1 + 8 <= x2 */ ctr[0]=XPRBnewprec(prob2,"ctr0",x[0],10,x[1]); ctr[1]=XPRBnewprec(prob2,"ctr1",x[1],0,x[3]); ctr[2]=XPRBnewprec(prob2,"ctr2",x[1],8,x[2]); cobj = XPRBnewctr(prob2,"OBJ",XPRB_N); for(i=0;i<5;i++) XPRBaddterm(cobj,x[i],objcof[i]); XPRBsetobj(prob2,cobj); /* Select objective function */ XPRBsetsense(prob2,XPRB_MINIM); /* Set objective sense to minimization */ printf("Problem status: %d LP status: %d MIP status: %d\n", XPRBgetprobstat(prob2),XPRBgetlpstat(prob2),XPRBgetmipstat(prob2)); XPRBexportprob(prob2,XPRB_LP,"expl2"); /* Matrix generation and output */ XPRBprintprob(prob2); /* Print current problem definition */ XPRBlpoptimize(prob2,""); /* Solve the LP */ printf("Problem status: %d LP status: %d MIP status: %d\n", XPRBgetprobstat(prob2),XPRBgetlpstat(prob2),XPRBgetmipstat(prob2)); printf("Objective: %g\n",XPRBgetobjval(prob2)); for(i=0;i<4;i++) /* Print solution values */ printf("%s:%g ", XPRBgetvarname(x[i]), XPRBgetsol(x[i])); printf("\n"); XPRBsetrange(ctr[0],-15,-5); /* Transform ctr[0] into range constraint */ printf("\n<<<<<<<<Constraint transformed into range:>>>>>>>>\n"); XPRBprintprob(prob2); /* Print current problem definition */ for(i=0;i<4;i++) { XPRBprintvar(x[i]); printf(" "); } /* Print new variable bounds */ printf("\n"); XPRBmipoptimize(prob2,""); /* Solve MIP */ printf("Problem status: %d LP status: %d MIP status: %d\n", XPRBgetprobstat(prob2),XPRBgetlpstat(prob2),XPRBgetmipstat(prob2)); printf("Objective: %g\n",XPRBgetobjval(prob2)); for(i=0;i<4;i++) /* Print solution values */ printf("%s:%g ", XPRBgetvarname(x[i]), XPRBgetsol(x[i])); printf("\n"); XPRBsetctrtype(ctr[0],XPRB_L); /* Change ctr[0] back to constraint */ printf("\n<<<<<<<<Constraint restored to inequality:>>>>>>>>\n"); XPRBprintprob(prob2); /* Print current problem definition */ XPRBsetterm(ctr[0],NULL,-10); /* Set new RHS value */ printf("<<<<<<<<Restore original RHS value:>>>>>>>>\n"); XPRBprintprob(prob2); /* Print current problem definition */ XPRBsetlb(x[1],15); /* Change the bound on a variable */ printf("<<<<<<<<Variable bound changed:>>>>>>>>\n"); for(i=0;i<4;i++) { XPRBprintvar(x[i]); printf(" "); } /* Print new variable bounds */ printf("\n"); XPRBmipoptimize(prob2,""); /* Solve MIP */ printf("Problem status: %d LP status: %d MIP status: %d\n", XPRBgetprobstat(prob2),XPRBgetlpstat(prob2),XPRBgetmipstat(prob2)); printf("Objective: %g\n",XPRBgetobjval(prob2)); for(i=0;i<4;i++) /* Print solution values */ printf("%s:%g ", XPRBgetvarname(x[i]), XPRBgetsol(x[i])); printf("\n"); /**** Change constraint coefficient and RHS ****/ XPRBsetterm(ctr[1],x[1],-3); /* ctr1: x1 <= 3*x3 */ XPRBaddterm(ctr[0],NULL,-10); /* ctr0: x0 + 20 <= x1 */ printf("\n<<<<<<<<Constraint coefficient and RHS changed:>>>>>>>>\n"); for(i=0;i<3;i++) XPRBprintctr(ctr[i]); for(i=0;i<4;i++) { XPRBprintvar(x[i]); printf(" "); } /* Print new variable bounds */ printf("\n"); XPRBmipoptimize(prob2,""); /* Re-generate the matrix and solve MIP */ printf("Problem status: %d LP status: %d MIP status: %d\n", XPRBgetprobstat(prob2),XPRBgetlpstat(prob2),XPRBgetmipstat(prob2)); printf("Objective: %g\n",XPRBgetobjval(prob2)); for(i=0;i<4;i++) /* Print solution values */ printf("%s:%g ", XPRBgetvarname(x[i]), XPRBgetsol(x[i])); printf("\n"); /**** Change constraint type ****/ XPRBsetctrtype(ctr[2],XPRB_G); /* ctr2: x1 + 8 >= x2 */ printf("\n<<<<<<<<Constraint type changed:>>>>>>>>\n"); for(i=0;i<3;i++) XPRBprintctr(ctr[i]); for(i=0;i<4;i++) { XPRBprintvar(x[i]); printf(" "); } /* Print variable bounds */ printf("\n"); XPRBmipoptimize(prob2,""); /* Re-generate the matrix and solve MIP */ printf("Problem status: %d LP status: %d MIP status: %d\n", XPRBgetprobstat(prob2),XPRBgetlpstat(prob2),XPRBgetmipstat(prob2)); printf("Objective: %g\n",XPRBgetobjval(prob2)); for(i=0;i<4;i++) /* Print solution values */ printf("%s:%g ", XPRBgetvarname(x[i]), XPRBgetsol(x[i])); printf("\n"); /**** Add another constraint ****/ ctr[3]=XPRBnewprec(prob2,"ctr3",x[0],37,x[2]); /* ctr4: x0 +37<= x2 */ printf("\n<<<<<<<<Constraint added:>>>>>>>>\n"); XPRBprintprob(prob2); for(i=0;i<4;i++) { XPRBprintvar(x[i]); printf(" "); } /* Print variable bounds */ printf("\n"); XPRBmipoptimize(prob2,""); /* Re-generate the matrix and solve MIP */ printf("Problem status: %d LP status: %d MIP status: %d\n", XPRBgetprobstat(prob2),XPRBgetlpstat(prob2),XPRBgetmipstat(prob2)); printf("Objective: %g\n",XPRBgetobjval(prob2)); for(i=0;i<4;i++) /* Print solution values */ printf("%s:%g ", XPRBgetvarname(x[i]), XPRBgetsol(x[i])); printf("\n"); /**** Delete a constraint ****/ XPRBdelctr(ctr[2]); printf("\n<<<<<<<<Constraint deleted:>>>>>>>>\n"); XPRBprintprob(prob2); for(i=0;i<4;i++) { XPRBprintvar(x[i]); printf(" "); } /* Print variable bounds */ printf("\n"); XPRBmipoptimize(prob2,""); /* Re-generate the matrix and solve MIP */ printf("Problem status: %d LP status: %d MIP status: %d\n", XPRBgetprobstat(prob2),XPRBgetlpstat(prob2),XPRBgetmipstat(prob2)); printf("Objective: %g\n",XPRBgetobjval(prob2)); for(i=0;i<4;i++) /* Print solution values */ printf("%s:%g ", XPRBgetvarname(x[i]), XPRBgetsol(x[i])); printf("\n"); } /**** Expl 3: Knapsack problem: accessing variables ****/ void expl3(void) { XPRBvar x[4]; XPRBctr ctr, cobj; double coeff[]={30.0,32.0,27.0,11.0}; double objcof[]={9.0,15.0,8.0,3.0}, lbd, ubd; int i; prob3=XPRBnewprob("expl3"); /* Create a new problem */ for(i=0;i<4;i++) /* Define 4 binary variables */ x[i]=XPRBnewvar(prob3,XPRB_BV,XPRBnewname("x_%d",i),0,1); /* Create the knapsack constraint: sum_i coeff[i]*x[i] <= 70 */ ctr = XPRBnewctr(prob3,"sumkn",XPRB_L); for(i=0;i<4;i++) XPRBaddterm(ctr,x[i],coeff[i]); XPRBaddterm(ctr,NULL,70); cobj = XPRBnewctr(prob3,"OBJ",XPRB_N); for(i=0;i<4;i++) XPRBaddterm(cobj,x[i],objcof[i]); XPRBsetobj(prob3,cobj); /* Select objective function */ /* XPRBprintprob(prob3); */ /* Uncomment to print the problem */ XPRBexportprob(prob3,XPRB_MPS,"expl3"); /* Matrix output in MPS format */ XPRBsetsense(prob3,XPRB_MAXIM); /* Change to maximization */ XPRBmipoptimize(prob3,""); /* Solve MIP */ printf("Objective: %g\n",XPRBgetobjval(prob3)); /* Get objective value */ for(i=0;i<4;i++) /* Print the solution */ printf("%s: %g (rc:%g),\n",XPRBgetvarname(x[i]),XPRBgetsol(x[i]), XPRBgetrcost(x[i])); printf("Dual: %g, slack: %g\n",XPRBgetdual(ctr),XPRBgetslack(ctr)); /* Print dual & slack values */ printf("\n<<<<<<<<Variable type changed from BV to UI>>>>>>>>\n"); XPRBsetvartype(x[1],XPRB_UI); /* Change variable type */ XPRBgetbounds(x[1], &lbd, &ubd); /* Get variable bounds */ printf("%s: bounds: %g %g, type: %d, index: %d\n", XPRBgetvarname(x[1]),lbd, ubd, XPRBgetvartype(x[1]),XPRBgetcolnum(x[1])); XPRBmipoptimize(prob3,""); /* Re-solve MIP */ printf("Objective: %g\n",XPRBgetobjval(prob3)); /* Get objective value */ printf("\n<<<<<<<<Variable bound changed: no matrix regeneration>>>>>>>>\n"); XPRBsetub(x[1],3); /* Change variable bound */ XPRBgetbounds(x[1], &lbd, &ubd); /* Get variable bounds */ printf("%s: bounds: %g %g, type: %d, index: %d\n", XPRBgetvarname(x[1]),lbd, ubd, XPRBgetvartype(x[1]),XPRBgetcolnum(x[1])); XPRBmipoptimize(prob3,""); /* Re-solve MIP */ printf("Objective: %g\n",XPRBgetobjval(prob3)); /* Get objective value */ for(i=0;i<4;i++) /* Print solution values */ printf("%s:%g ", XPRBgetvarname(x[i]), XPRBgetsol(x[i])); printf("\n\n<<<<<<<<Variable type changed from UI to PI>>>>>>>>\n"); XPRBsetvartype(x[1],XPRB_PI); /* Change variable type */ XPRBsetlim(x[1],2); /* set the integer limit for the partial integer variable */ XPRBprintvar(x[1]); printf("\n"); /* Print current variable definition */ XPRBmipoptimize(prob3,""); /* Re-solve MIP */ printf("Objective: %g\n",XPRBgetobjval(prob3)); /* Get objective value */ for(i=0;i<4;i++) /* Print the solution */ printf("%s: %g (rc:%g),\n",XPRBgetvarname(x[i]),XPRBgetsol(x[i]), XPRBgetrcost(x[i])); printf("Dual: %g, slack: %g\n",XPRBgetdual(ctr),XPRBgetslack(ctr)); /* Print dual & slack values */ } /****Expl 4: a small unbounded problem ****/ void expl4(void) { XPRBvar x[2]; XPRBctr ctr; double objcof[]={1.0,1.0}; int i; prob4=XPRBnewprob("expl4"); /* Create a new problem */ /* Define 2 variables in [0,PLUSINFINITY] */ for(i=0;i<2;i++) x[i]=XPRBnewvar(prob4,XPRB_PL,XPRBnewname("x_%d",i),0,XPRB_INFINITY); /* Create the constraints: ctr0: 4*x0 + x1 >= 4 ctr1: x0 + x1 >= 3 ctr2: x0 + 2*x1 >= 4 */ ctr=XPRBnewctr(prob4,"c1",XPRB_G); XPRBaddterm(ctr,x[0],4); XPRBaddterm(ctr,x[1],1); XPRBaddterm(ctr,NULL,4); ctr=XPRBnewctr(prob4,"c2",XPRB_G); XPRBaddterm(ctr,x[0],1); XPRBaddterm(ctr,x[1],1); XPRBaddterm(ctr,NULL,3); ctr=XPRBnewctr(prob4,"c3",XPRB_G); XPRBaddterm(ctr,x[0],1); XPRBaddterm(ctr,x[1],2); XPRBaddterm(ctr,NULL,4); ctr = XPRBnewctr(prob4,"OBJ",XPRB_N); for(i=0;i<2;i++) XPRBaddterm(ctr,x[i],objcof[i]); XPRBsetobj(prob4,ctr); /* Select objective function */ /* Try out the effect of solving without presolve: XPRSsetintcontrol(XPRBgetXPRSprob(prob4), XPRS_PRESOLVE, 0); XPRSsetintcontrol(XPRBgetXPRSprob(prob4), XPRS_MIPPRESOLVE, 0); */ XPRBsetsense(prob4,XPRB_MAXIM); /* Change to maximization */ XPRBlpoptimize(prob4,""); /* Solve the LP */ printf("Problem status: %d LP status: %d MIP status: %d\n", XPRBgetprobstat(prob4),XPRBgetlpstat(prob4),XPRBgetmipstat(prob4)); printf("Objective: %g\n",XPRBgetobjval(prob4)); /* Get objective value */ for(i=0;i<2;i++) /* Print solution values */ printf("%s:%g ", XPRBgetvarname(x[i]), XPRBgetsol(x[i])); printf("\n"); } /***Expl5: Working with different problems****/ void expl5(void) { int i; /**** Re-solve problem 2 ****/ printf("\n<<<<<<<<Re-solve prob2>>>>>>>>\n"); XPRBsetsense(prob2,XPRB_MINIM); /* Change to minimization */ XPRBmipoptimize(prob2,""); /* Solve global */ printf("Problem status: %d LP status: %d MIP status: %d\n", XPRBgetprobstat(prob2),XPRBgetlpstat(prob2),XPRBgetmipstat(prob2)); printf("Objective: %g\n",XPRBgetobjval(prob2)); /* Get objective value */ for(i=0;i<4;i++) /* Print solution values */ printf("x_%d:%g ", i, XPRBgetsol(XPRBgetbyname(prob2,XPRBnewname("x_%d",i),XPRB_VAR))); /**** Delete problem 4 ****/ printf("\n\n<<<<<<<<Delete prob4>>>>>>>>\n"); XPRBprintprob(prob4); /* Print the problem def. */ XPRBdelprob(prob4); /* Delete the problem */ /**** Re-solve problem 3 ****/ printf("<<<<<<<<Re-solve prob3 and print it>>>>>>>>\n"); XPRBprintprob(prob3); /* Print the problem def. */ XPRBsetsense(prob3,XPRB_MAXIM); /* Change to maximization */ XPRBmipoptimize(prob3,""); /* Solve MIP */ printf("Problem status: %d LP status: %d MIP status: %d\n", XPRBgetprobstat(prob3),XPRBgetlpstat(prob3),XPRBgetmipstat(prob3)); printf("Objective: %g\n",XPRBgetobjval(prob3)); /* Get objective value */ for(i=0;i<4;i++) /* Print solution values */ printf("x_%d:%g ", i, XPRBgetsol(XPRBgetbyname(prob3,XPRBnewname("x_%d",i),XPRB_VAR))); printf("\n"); } | |||||||||||
© Copyright 2024 Fair Isaac Corporation. |