Decision Optimization

Decision Optimization

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

 View Only
Expand all | Collapse all

Assigning value to an array element in IBM ILOG CPLEX optimizer -opl

  • 1.  Assigning value to an array element in IBM ILOG CPLEX optimizer -opl

    Posted Mon October 25, 2010 10:57 AM

    Originally posted by: Ebisa


    I am just starting to use the cplex optimizer, and I have this small problem to fix.
    In my program, I wanted to assign a value to the first element of a dvar array. Here is the releveant part of my program:

    int noOfTimesteps = ...;
    range time_steps = 1 ..noOfTimesteps;
    int max_storage = ...;
    int min_storage = ...;
    int initial_charge_level = ...;

    dvar int charge_leveltime_steps in min_storage..max_storage;
    charge_level[1] = initial_charge_level;

    But the last line in the code is creating an error message that says "syntax error, unexpected (integer-literal)". I would appreciate if someone can help me on how to fix this. How can I assign value to the first element of the array?

    Regards,
    #ConstraintProgramming-General
    #DecisionOptimization


  • 2.  Re: Assigning value to an array element in IBM ILOG CPLEX optimizer -opl

    Posted Mon October 25, 2010 11:48 AM

    Originally posted by: GGR


    Hi Ebisa

    To initialize arrays in a model files (apart at declaration time) you must use the script: That is your OPL code could be:

    int noOfTimesteps = 10;
    range time_steps = 1 ..noOfTimesteps;
    int max_storage = 10;
    int min_storage = 1;
    int initial_charge_level = 2;
    int charge_leveltime_steps;

    dvar int charge_leveltime_steps in min_storage..max_storage;

    // dynamic initialisation of the array charge_level
    execute {
    charge_level[1] = initial_charge_level;
    };

    Hope that helps

    Note, if you want to use constraint programming, you must decalre in head of you OPL model file

    using CP;

    If you have an issue about OPL using constraint programing, the right forum is "OPL using CP Optimizer". If you have an issue about OPL using CPLEX Linear Programming, the right forum is "OPL using CPLEX Optimizer". Sorry for that mess but posting in a wrong forum may lead to delayed (or no) answer

    Cheers
    #ConstraintProgramming-General
    #DecisionOptimization


  • 3.  Re: Assigning value to an array element in IBM ILOG CPLEX optimizer -opl

    Posted Mon October 25, 2010 12:12 PM

    Originally posted by: GGR


    Hi Ebisa

    Sorry, I missed charge_level is an array of variable.

    To initialize a variable range you can either do it in the declaration of the array:

    dvar int charge_leveli in time_steps
    in ((i == 1)? initial_charge_level : min_storage)..((i==1) ? initial_charge_level : max_storage);

    That is possible for not too complex cases
    Either in the constraint. That is in the OPL subject to block:
    subject to {
    charge_level[1] == initial_charge_level;
    }
    Last in a script execute block:

    execute {
    charge_level[1].UB = initial_charge_level;
    charge_level[1].LB = initial_charge_level;
    }

    So
    for a constant to initialiie:
    either in the declaration, either in a script execute block
    for a variable to initialize
    either in the declaration, either in a script execute block
    either in the model subject to block as constraints

    Cheers
    #ConstraintProgramming-General
    #DecisionOptimization


  • 4.  Re: Assigning value to an array element in IBM ILOG CPLEX optimizer -opl

    Posted Wed October 27, 2010 04:03 AM

    Originally posted by: Ebisa


    Thank you for your replies,
    they were helpful.

    Regards
    #ConstraintProgramming-General
    #DecisionOptimization