FICO
FICO Xpress Optimization Examples Repository
FICO Optimization Community FICO Xpress Optimization Home
Back to examples browserPrevious exampleNext example

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

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.

xbgoalobjcs.zip[download all files]

Source Files
By clicking on a file name, a preview is opened at the bottom of this page.
xbgoalobj.cs[download]
xbgoalobj.csproj[download]





xbgoalobj.cs

/********************************************************
  Xpress-BCL C# Example Problems
  ==============================

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

  (c) 2008-2024 Fair Isaac Corporation
      authors: S.Heipcke, D.Brett.
********************************************************/

using System;
using System.Text;
using System.IO;
using Optimizer;
using BCL;


namespace Examples
{
    public class TestAdvGoalObj
    {
        const int NGOALS = 3;

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

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

            prob = new XPRBprob("Goal");

            // Adding the variables
            x = prob.newVar("x", BCLconstant.XPRB_PL);
            y = prob.newVar("y", BCLconstant.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("Goal" + (g+1), goal[g]);

            // **** Archimedian GP ****
            System.Console.WriteLine("Archimedian:");
            wobj = new XPRBexpr(0);
            for(g=0;g<NGOALS;g++)
            {
                if (Sense[g] == "max")
                   wobj -= (Weight[g]*goal[g]);
                else
                   wobj += (Weight[g]*goal[g]);
            }
            prob.setObj(prob.newCtr(wobj));

            xprsp = prob.getXPRSprob();
            xprsp.OutputLog = 0;
            prob.setSense(BCLconstant.XPRB_MINIM);
            prob.lpOptimize();

            // Solution printout
            System.Console.WriteLine(" Solution: x: " + x.getSol() + ", y: " +
                                    y.getSol());
            System.Console.WriteLine(" Goal   Target     Value");
            for(g=0;g<NGOALS;g++)
                System.Console.WriteLine("  " + (g+1) + "       " + Sense[g] +
                    "     " + (goalCtr[g].getAct() - goalCtr[g].getRHS()));


            // **** Prememptive GP ****
            System.Console.WriteLine("Prememptive:");
            i=-1;
            while (i<NGOALS-1)
            {
                i+=1;
                if (Sense[i] == "max")
                {
                    prob.setObj(prob.newCtr("GoalO" + (i + 1), goal[i]));
                    prob.setSense(BCLconstant.XPRB_MAXIM);
                    prob.lpOptimize();
                    if (prob.getLPStat() != BCLconstant.XPRB_LP_OPTIMAL)
                    {
                        System.Console.WriteLine("Cannot satisfy goal "+(i+1));
                        break;
                    }
                    else
                    {
                        Target[i]=prob.getObjVal();
                        if (Type[i] == "perc")
                            Target[i]-= Math.Abs(Target[i])*Deviation[i]/100;
                        else
                            Target[i]-= Deviation[i];
                        if (i<NGOALS-1) goalCtr[i] -= Target[i];
                        goalCtr[i].setType(BCLconstant.XPRB_G);
                    }
                }
                else
                {
                    prob.setObj(prob.newCtr("Objective" + i, goal[i]));
                    prob.setSense(BCLconstant.XPRB_MINIM);
                    prob.lpOptimize();
                    if (prob.getLPStat() != BCLconstant.XPRB_LP_OPTIMAL)
                    {
                        System.Console.WriteLine("Cannot satisfy goal "+(i+1));
                        break;
                    }
                    else
                    {
                        Target[i]=prob.getObjVal();
                        if (Type[i] == "perc")
                            Target[i]+= Math.Abs(Target[i])*Deviation[i]/100;
                        else
                            Target[i]+= Deviation[i];
                        if (i<NGOALS-1) goalCtr[i] -= Target[i];
                        goalCtr[i].setType(BCLconstant.XPRB_L);
                    }
                }
                System.Console.WriteLine("Solution(" + (i+1) + "):  x: " +
                            x.getSol() + ", y: " + y.getSol());
            }

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

        }

        public static void Main()
        {
           XPRB.init();
           TestAdvGoalObj TestInstance = new TestAdvGoalObj();

            if (XPRB.init() != 0) return;

            TestInstance.modelgo();
            XPRB.finish();

            return;
        }
    }
}
Back to examples browserPrevious exampleNext example