Decision Optimization

Decision Optimization

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

 View Only
Expand all | Collapse all

Tips & Tricks for Effective and Efficient ILOG Script Code

  • 1.  Tips & Tricks for Effective and Efficient ILOG Script Code

    Posted Wed December 03, 2025 09:23 PM

    I have some questions on the best and most efficient way to produce an optimization problem in the ILOG Script programming language, invoking CP Optimizer:

    The structure of the example model is:

    • c = variable1 + variable2
    • d = variable3 + variable4
    • p = c - d
    • p - variable5 = variable6
    • variable2 + variable5 = variable7

    where:

    • variable1 is a model parameter (user-defined value).
    • variable2, variable3, variable4, and variable5 are decision variables.
    • variable2 and variable3 take on values defined in lists of allowable values.
    • variable6 is required to take on one user-defined value.
    • variable7 is the sum of variable2 and variable3, which also needs to equal a value defined in a list of allowable values.

    What is the best way to write these using ILOG Script (eg use of constraints, expr int, dvar int, int)?

    Also, should variable6 be expressed as a constraint or defined as a model parameter?



    ------------------------------
    Stevie Turkington
    ------------------------------


  • 2.  RE: Tips & Tricks for Effective and Efficient ILOG Script Code

    Posted Thu December 04, 2025 04:48 AM

    Hi,

    no need of ILOG script, you can do that with OPL only:

     using CP;
     int variable1=2;
     int variable6=3;
     dvar int variable2;
     dvar int variable3 ;
     dvar int variable4;
     dvar int variable5;
     dexpr int variable7=variable2+variable3;
     
     dvar int c;
     dvar int d;
     dvar int p;
     
     subject to
     {
       variable2 in {1,2,3,8,10};
        variable3  in {1,2,3,8,10,12};;
     
        c == variable1 + variable2;
        d == variable3 + variable4;
        p == c - d;
        p - variable5 == variable6;
        variable2 + variable5 == variable7;
        
      }    

    works fine

    See some scripting examples I shared to see what OPL scripting is good for



    ------------------------------
    [Alex] [Fleischer]
    [Data and AI Technical Sales]
    [IBM]
    ------------------------------



  • 3.  RE: Tips & Tricks for Effective and Efficient ILOG Script Code

    Posted Fri December 05, 2025 01:59 AM
    Edited by Stevie Turkington Fri December 05, 2025 01:59 AM

    Thank you.

    Unfortunately, I am still encountering issues with the validity of calculations.

    I split a long equation into smaller parts, which for some reason gives me more solutions than when it was one big equation (constraints in both instances). This is despite nothing in the objective function changing.

    In fact, I get completely different values for the DVs - and they don't satisfy the (as an eg) int variable6 = 3 parameter.

    The one long equation does, but at the expense of other calculations not actually being what they say they are.

    Is this some kind of bug within the software?

    I double check everything now, given all the problems I've experienced with a solution purporting to be feasible, when in fact it isn't.



    ------------------------------
    Stevie Turkington
    ------------------------------



  • 4.  RE: Tips & Tricks for Effective and Efficient ILOG Script Code

    Posted Fri December 05, 2025 02:08 AM

    Note: I'm only splitting a long equation into smaller parts to see if I can pinpoint if my double-checking is leading me astray somewhere.

    I ensure even though the equation is over more lines, the result of the previous part merges with the next part. The necessary new variables are declared as decision variables, and aren't added to the objective function. The objective function remains exactly the same between long equations vs split-a-long-equation.



    ------------------------------
    Stevie Turkington
    ------------------------------



  • 5.  RE: Tips & Tricks for Effective and Efficient ILOG Script Code

    Posted Fri December 05, 2025 06:10 AM

    Hi,

    have you set bounds on all your decision variables ?

    This is quite good to avoid numerical problems with very large values.

    using CP;
    
    dvar int x in 0..10;
    dvar int y in 0..10;
    
    subject to
    {
      ct1:x<=y-2;
      ct2:y<=x-2;
    }

    gives

    infeasible whereas if you remove the bounds you could get some nonsense.

    Can you share the entire model you get issues with ?

    regards



    ------------------------------
    [Alex] [Fleischer]
    [Data and AI Technical Sales]
    [IBM]
    ------------------------------



  • 6.  RE: Tips & Tricks for Effective and Efficient ILOG Script Code

    Posted Mon December 08, 2025 02:56 AM

    Hi,

    again if you want other users to have a look, you could share your rentire model and data so that other users could try

    regards



    ------------------------------
    [Alex] [Fleischer]
    [Data and AI Technical Sales]
    [IBM]
    ------------------------------



  • 7.  RE: Tips & Tricks for Effective and Efficient ILOG Script Code

    Posted Mon December 08, 2025 01:35 PM
    Edited by Stevie Turkington Mon December 08, 2025 08:17 PM

    Hi Alex

    Expanding on my earlier example, is this the most effective and efficient way to code the following optimization problem:

    using CP;
    
    int variable1 = #; 
    int variable6 = #;
    int variable8 = #;
    
    dvar int variable2;
    dvar int variable3;
    dvar int variable4;
    dvar int variable5;
    dvar int d;
    dvar int b;
    dvar int c;
    dvar int p;
    
    dexpr int variable7 = variable4 + variable5;
    
    subject to {
    variable2 in {set of #'s};
    variable3 in {set of #'s};
    variable7 in {set of #'s};
    d in {set of #'s};
    
    b == variable8 + variable7;
    c == variable1 + variable2;
    d == variable3 - variable4;      
    p == c - d;
    p - variable5 == variable6;
    }

    I had accidentally written both variable2 + variable5 = variable7 and variable2 + variable3 = variable7 in my first example.
    The only equation which should apply in that is variable2 + variable3variable7. I have, nonetheless, expanded on the example in this post.



    ------------------------------
    Stevie Turkington
    ------------------------------



  • 8.  RE: Tips & Tricks for Effective and Efficient ILOG Script Code

    Posted Tue December 09, 2025 01:37 AM

    Unfortunately, I continue to face difficulties. It is not a matter of "large numbers", the calculations match when I check them apart from one.

    It seems an optimal solution is the values already provided for PLUS whatever number is necessary to "make one line work". However, there is no variable to factor in this "number to make it work". It is not accounted for but is being calculated as if there is that additional variable. This shouldn't be occuring so why is it happening?

    If there is a solution, it should satisfy the constraints. The software should not be adding adhoc numbers to "make it work" and purport to display feasible solutions, because a calculated value isn't what it says it is when you don't factor in whatever the software has decided to add (eg in one line a variable is valued x, but the same variable in another line is calculated on the basis x + "adhoc number").



    ------------------------------
    Stevie Turkington
    ------------------------------



  • 9.  RE: Tips & Tricks for Effective and Efficient ILOG Script Code

    Posted Tue December 09, 2025 11:15 PM

    I've managed to reduce the values decided for the decision variables to be 2 or 3 digits long. Even then, the issue I raised in my previous post persists. There is a serious calculation flaw.



    ------------------------------
    Stevie Turkington
    ------------------------------



  • 10.  RE: Tips & Tricks for Effective and Efficient ILOG Script Code

    Posted Wed December 10, 2025 10:28 AM

    Hello,

    your question is not very clear for me, but just looking at the small partial model you sent, I can see 13 linear equalities (counting the constraints to be in a given discrete set as equalities), and 12 variables. So, to complete the model to a feasible one, you would need to give compatible values to the different entries. You problem is certainly related.



    ------------------------------
    Olivier Lhomme
    ------------------------------



  • 11.  RE: Tips & Tricks for Effective and Efficient ILOG Script Code

    Posted Wed December 10, 2025 01:17 PM
    Edited by Stevie Turkington Wed December 10, 2025 01:20 PM

    For some reason, the software is saying a particular equation equals a certain value. Upon actually checking this, it does not. All other calculations are correct, apart from that one. There is no additional variable - declared (int) or decision (dvar int) - to factor in this difference. It appears one variable equals a value in all equations it appears, bar one where it has a different value.

    The software is calling the erroneous calculation a solution to the problem.
    ------------------------------
    Stevie Turkington
    ------------------------------



  • 12.  RE: Tips & Tricks for Effective and Efficient ILOG Script Code

    Posted Thu December 11, 2025 05:37 AM

    What dou mean by:
      "It appears one variable equals a value in all equations it appears, bar one where it has a different value." ?
      
    A variable is given only one value, there is no concept such as the value of variable X in a constraint C, so how can you say that the variable has different values in two constraints? 
     
    I second Alex, sending a code to show your problem is the best way to advance on this.



    ------------------------------
    Olivier Lhomme
    ------------------------------



  • 13.  RE: Tips & Tricks for Effective and Efficient ILOG Script Code

    Posted Sat December 13, 2025 12:55 AM

    I come to this conclusion because this is the behaviour I am seeing. Yes, it should equal one value throughout the whole model, but this doesn't appear to be the case.

    It is very frustrating and disappointing when I'm now double-checking everything it spits out as a result, to constantly find this error.

    Please PM me your email address and I'll send through the models where I am still encountering this odd behaviour.

    I have also ran Intel Processor Diagnostic Tool and Memory Diagnostic, in case there is a hardware fault which could cause the calculation error. Both tests came back with passes.



    ------------------------------
    Stevie Turkington
    ------------------------------



  • 14.  RE: Tips & Tricks for Effective and Efficient ILOG Script Code

    Posted Tue December 23, 2025 12:55 AM

    An update:

    Having touched base with Philippe R, this is being investigated as a potential bug, with feedback early January.



    ------------------------------
    Stevie Turkington
    ------------------------------