Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.

 View Only
Expand all | Collapse all

record objective function value to Excel

  • 1.  record objective function value to Excel

    Posted Tue July 17, 2018 02:01 AM

    Originally posted by: A6UM_Can_Nguyen


    Hi Alex,

    How to record an objective function value to Excel? could we use a tuple to record it?

     

    Thank you,

    Bests,

    Nguyen


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 2.  Re: record objective function value to Excel

    Posted Tue July 17, 2018 03:57 AM

    Hi,

    let me change a bit the example oil in order to show you an example:

    .mod

    {string} Gasolines = ...;
    {string} Oils = ...;
    tuple gasType {
      float demand;
      float price;
      float octane;
      float lead;
    }

    tuple oilType {
      float capacity;
      float price;
      float octane;
      float lead;
    }
    gasType Gas[Gasolines] = ...;
    oilType Oil[Oils] = ...;
    float MaxProduction = ...;
    float ProdCost = ...;

    dvar float+ a[Gasolines] in 2..2;
    dvar float+ Blend[Oils][Gasolines];

    dvar float obj;

    maximize obj;
     
    subject to {
    obj==sum( g in Gasolines , o in Oils )
        (Gas[g].price - Oil[o].price - ProdCost) * Blend[o][g]
        - sum(g in Gasolines) a[g];

     

      forall( g in Gasolines )
        ctDemand:
          sum( o in Oils )
            Blend[o][g] == Gas[g].demand + 10*a[g];
      forall( o in Oils )
        ctCapacity:   
          sum( g in Gasolines )
            Blend[o][g] <= Oil[o].capacity;
      ctMaxProd:  
        sum( o in Oils , g in Gasolines )
          Blend[o][g] <= MaxProduction;
      forall( g in Gasolines )
        ctOctane:
          sum( o in Oils )
            (Oil[o].octane - Gas[g].octane) * Blend[o][g] >= 0;
      forall( g in Gasolines )
        ctLead:
          sum( o in Oils )
            (Oil[o].lead - Gas[g].lead) * Blend[o][g] <= 0;
    }

    execute DISPLAY_REDUCED_COSTS{
      for( var g in Gasolines ) {
        writeln("a[",g,"].reducedCost = ",a[g].reducedCost);
      }
    }

    .dat

    SheetConnection sheet("oilSheet.xls");


    Gasolines from SheetRead(sheet,"'gas data'!A2:A4");
    Oils from SheetRead(sheet,"'oil data'!A2:A4");
    Gas from SheetRead(sheet,"'gas data'!B2:E4");
    Oil from SheetRead(sheet,"'oil data'!B2:E4");
    MaxProduction = 14000;
    ProdCost = 4;


    a to SheetWrite(sheet,"RESULT!A2:A4");
    Blend to SheetWrite(sheet,"RESULT!B2:D4");
    obj to SheetWrite(sheet,"RESULT!E1:E1");

    regards

     

    https://www.linkedin.com/pulse/what-optimization-how-can-help-you-do-more-less-zoo-buses-fleischer/


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 3.  Re: record objective function value to Excel

    Posted Tue July 17, 2018 11:25 AM

    Originally posted by: A6UM_Can_Nguyen


    Hi Alex,

    Thank you.

    But If the objective function has 2 parts, so how to record to Excel for each separate part. For example, obj==sum( g in Gasolines , o in Oils ) (Gas[g].price - Oil[o].price - ProdCost) * Blend[o][g] - sum(g in Gasolines) a[g];

    Where, sum( g in Gasolines , o in Oils ) (Gas[g].price - Oil[o].price - ProdCost) * Blend[o][g]  is part  (1)

                 and Blend[o][g] - sum(g in Gasolines) a[g] is  is part (2)

    How to record part (1) to E1, and part (2) to E2 in Excel?

    Bests,

    Nguyen

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 4.  Re: record objective function value to Excel

    Posted Tue July 17, 2018 11:37 AM

    Hi,

    then you could use a dvar array

    .mod

    {string} Gasolines = ...;
    {string} Oils = ...;
    tuple gasType {
      float demand;
      float price;
      float octane;
      float lead;
    }

    tuple oilType {
      float capacity;
      float price;
      float octane;
      float lead;
    }
    gasType Gas[Gasolines] = ...;
    oilType Oil[Oils] = ...;
    float MaxProduction = ...;
    float ProdCost = ...;

    dvar float+ a[Gasolines] in 2..2;
    dvar float+ Blend[Oils][Gasolines];

    dvar float obj[1..2];

    maximize obj[1]+obj[2];
     
    subject to {
    obj[1]==sum( g in Gasolines , o in Oils )
        (Gas[g].price - Oil[o].price - ProdCost) * Blend[o][g]
        ;

    obj[2]==
        - sum(g in Gasolines) a[g];

      forall( g in Gasolines )
        ctDemand:
          sum( o in Oils )
            Blend[o][g] == Gas[g].demand + 10*a[g];
      forall( o in Oils )
        ctCapacity:   
          sum( g in Gasolines )
            Blend[o][g] <= Oil[o].capacity;
      ctMaxProd:  
        sum( o in Oils , g in Gasolines )
          Blend[o][g] <= MaxProduction;
      forall( g in Gasolines )
        ctOctane:
          sum( o in Oils )
            (Oil[o].octane - Gas[g].octane) * Blend[o][g] >= 0;
      forall( g in Gasolines )
        ctLead:
          sum( o in Oils )
            (Oil[o].lead - Gas[g].lead) * Blend[o][g] <= 0;
    }

    execute DISPLAY_REDUCED_COSTS{
      for( var g in Gasolines ) {
        writeln("a[",g,"].reducedCost = ",a[g].reducedCost);
      }
    }

     

    .dat

     

    SheetConnection sheet("oilSheet.xls");


    Gasolines from SheetRead(sheet,"'gas data'!A2:A4");
    Oils from SheetRead(sheet,"'oil data'!A2:A4");
    Gas from SheetRead(sheet,"'gas data'!B2:E4");
    Oil from SheetRead(sheet,"'oil data'!B2:E4");
    MaxProduction = 14000;
    ProdCost = 4;


    a to SheetWrite(sheet,"RESULT!A2:A4");
    Blend to SheetWrite(sheet,"RESULT!B2:D4");
    obj to SheetWrite(sheet,"RESULT!E1:E2");

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 5.  Re: record objective function value to Excel

    Posted Tue July 17, 2018 11:59 AM

    Originally posted by: A6UM_Can_Nguyen


    Hi Alex,

    Yes. It is very good to get different parts of the objective function. 

    As I know, we usually put the objective function outside "subject to", but in this case, you put the objective function (obj[1], obj[2]) into  "subject to". Please tell me to know if we put such objective function, then the results of the model, does it have any changing?

    Bests,

    Nguyen

     

     

     

     

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 6.  Re: record objective function value to Excel

    Posted Tue July 17, 2018 12:08 PM

    Hi

    the objective is

    maximize obj[1]+obj[2];

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 7.  Re: record objective function value to Excel

    Posted Tue July 17, 2018 12:21 PM

    Originally posted by: A6UM_Can_Nguyen


    Hi Alex,

    Ok. But we should be case 1 or case 2. what is a difference between them?

    Case 1: 

    maximize obj[1]+obj[2];

    subject to {
    obj[1]==sum( g in Gasolines , o in Oils )
        (Gas[g].price - Oil[o].price - ProdCost) * Blend[o][g]
        ;

    obj[2]==
        - sum(g in Gasolines) a[g];

    Case 2:

    maximize obj[1]+obj[2];
    obj[1]==sum( g in Gasolines , o in Oils )
        (Gas[g].price - Oil[o].price - ProdCost) * Blend[o][g]
        ;

    obj[2]==
        - sum(g in Gasolines) a[g];

    subject to {

    }

    Bests,

    Nguyen

     

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 8.  Re: record objective function value to Excel

    Posted Tue July 17, 2018 12:25 PM

    Hi,

    case 2 does not work.

    case 1 should work better

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 9.  Re: record objective function value to Excel

    Posted Tue July 17, 2018 01:03 PM

    Originally posted by: A6UM_Can_Nguyen


    Hi Alex,

    Ok. Thank you for your help.

    Bests and regards,

    Nguyen

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer