Initializing help system before first use

GoalObj - Archimedian and pre-emptive goal programming using objective functions


Type: Goal Programming
Rating: 3 (intermediate)
Description: A small linear problem with multiple objectives is solved by Archimedian and pre-emptive goal programming. The example uses functions to access information about constraints and shows how to solve a problem repeatedly with a modified objective function.
File(s): xbgoalobj.cxx


xbgoalobj.cxx
/********************************************************
  Xpress-BCL C++ Example Problems
  ===============================

  file xbgoalobj.cxx
  ``````````````````
  Archimedian and pre-emptive goal programming
  using objective functions.

  (c) 2008 Fair Isaac Corporation
      author: S.Heipcke, 2005, rev. Mar. 2011
********************************************************/

#include <iostream>
#include <cmath>
#include <cstring>
#include "xprb_cpp.h"
#include "xprs.h"

using namespace std;
using namespace ::dashoptimization;

#define NGOALS 3

// **** Data ****
const char *Type[] = {"perc", "abs", "perc"};
const char *Sense[] = {"max", "min", "max"};
double Weight[] = {100, 1, 0.1};
double Deviation[] = {10, 4, 20};

void modelgo()
{
 XPRBvar x,y;
 XPRBctr goalCtr[NGOALS], aCtr;
 double Target[NGOALS];
 XPRBexpr goal[NGOALS], wobj;
 XPRBprob *prob;
 int i,g;

 prob = new XPRBprob("Goal");

 // Adding the variables
 x = prob->newVar("x",XPRB_PL);
 y = prob->newVar("y",XPRB_PL);

 // Adding a constraint
 aCtr = prob->newCtr("Limit", 42*x + 13*y <= 100);

 // Goals
 goal[0] = 5*x +  2*y - 20;
 goal[1] = -3*x + 15*y - 48;
 goal[2] = 1.5*x + 21*y - 3.8;
 for(g=0;g<NGOALS;g++) 
  goalCtr[g] = prob->newCtr(XPRBnewname("Goal%d",(g+1)), goal[g]);

 // **** Archimedian GP ****
 cout << "Archimedian:" << endl;
 wobj = 0;
 for(g=0;g<NGOALS;g++) 
 {
  if (strcmp(Sense[g],"max")==0)
   wobj -= Weight[g]*goal[g];
  else
   wobj += Weight[g]*goal[g]; 
 } 
 prob->setObj(wobj);
 XPRSsetintcontrol(prob->getXPRSprob(),XPRS_OUTPUTLOG, 0);
 prob->lpOptimize("");

 // Solution printout
 cout << " Solution: x: " << x.getSol() << ", y: " << y.getSol() << endl;
 cout <<" Goal   Target     Value" << endl;
 for(g=0;g<NGOALS;g++) 
   cout << "  " << g+1 << "       " << Sense[g] << "     " << (goalCtr[g].getAct() - goalCtr[g].getRHS()) << endl; 
  
  
 // **** Prememptive GP ****  
 cout << "Prememptive:" << endl;
 i=-1;
 while (i<NGOALS-1) 
 {
  i+=1;
  if (strcmp(Sense[i],"max")==0) 
  {
   prob->setObj(goal[i]);
   prob->setSense(XPRB_MAXIM);
   prob->lpOptimize("");
   if (prob->getLPStat() != XPRB_LP_OPTIMAL)
   {
    cout << "Cannot satisfy goal " << i+1 << endl;
    break;
   }
   else
   {
    Target[i]=prob->getObjVal(); 
    if (strcmp(Type[i],"perc")==0)
     Target[i]-= abs(Target[i])*Deviation[i]/100;
    else
     Target[i]-= Deviation[i];
    if (i<NGOALS-1) goalCtr[i] -= Target[i];
    goalCtr[i].setType(XPRB_G); 
   }
  }
  else
  {
   prob->setObj(goal[i]);
   prob->setSense(XPRB_MINIM);
   prob->lpOptimize("");
   if (prob->getLPStat() != XPRB_LP_OPTIMAL)
   {
    cout << "Cannot satisfy goal " << i+1 << endl;
    break;
   }
   else
   {
    Target[i]=prob->getObjVal(); 
    if (strcmp(Type[i],"perc")==0)
     Target[i]+= abs(Target[i])*Deviation[i]/100;
    else
     Target[i]+= Deviation[i];
    if (i<NGOALS-1) goalCtr[i] -= Target[i];
    goalCtr[i].setType(XPRB_L);
   } 
  }
  cout << "Solution(" << i+1 << "):  x: " << x.getSol() << ", y: " << y.getSol() << endl;
 }

 // Solution printout
 cout << " Goal       Target         Value" << endl;
  for(g=0;g<=i;g++) 
  {
   cout << "  " << g+1 << "     " << (goalCtr[g].getType()==XPRB_G?" >=  ":" <=  ") << Target[g];
   if(g==NGOALS-1) 
    cout << "     " << prob->getObjVal() << endl;
   else   
    cout << "     " << (goalCtr[g].getAct() - goalCtr[g].getRHS()) + Target[g] << endl;
  }

 delete prob;
}

int main(int argc, char **argv)
{
 if (XPRB::init()) return 1;

 modelgo();
 XPRB::finish();

 return 0;
}

© 2001-2020 Fair Isaac Corporation. All rights reserved. This documentation is the property of Fair Isaac Corporation (“FICO”). Receipt or possession of this documentation does not convey rights to disclose, reproduce, make derivative works, use, or allow others to use it except solely for internal evaluation purposes to determine whether to purchase a license to the software described in this documentation, or as otherwise set forth in a written software license agreement between you and FICO (or a FICO affiliate). Use of this documentation and the software described in it must conform strictly to the foregoing permitted uses, and no other use is permitted.