Menu

#52 Setting decision variables as an array

Any Version
open
nobody
None
5
2015-09-07
2015-09-03
Anonymous
No

Hi

I want to ask if it is possible to set my decision variables as an array, for example if I have 12 decision variables that are represented and stored in an array of [3][4], and I have tryied set the variables as follows

for (int i = 0;i < 3; i++)
{
  for (int j = 0; j < 4; j++)
  {
      solution.setVariable([i][j], EncodingUtils.newReal(-10.0, 10.0));
  }
}

and I am getting the following error message
Syntax error on token "(", Expression expected after this token

associated with
solution.setVariable([i][j], EncodingUtils.newReal(-10.0, 10.0));

Please any one can help in this issue

Kind regards

Discussion

  • Anonymous

    Anonymous - 2015-09-03

    Another question related to the previous Ticket is that >>>>
    Is it compulsory to use the decision variables directly within the objective
    function? or it is allowed to use them elsewhere to calculate other parameters that will be used with the objective function???

    Thank you

     
  • Anonymous

    Anonymous - 2015-09-03

    Regarding the first question, you'll need to scalarize the 3x4 matrix into an array:

    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 4; j++) {
            solution.setVariable(i*4+j, EncodingUtils.newReal(-10.0, 10.0));
        }
    }
    

    The objective functions are calculated within a problem's public void evaluate(Solution solution) { ... } method. All calculations for computing the objective functions from the decision variables must be included within this method.

    Hope this helps answer your questions!

     
  • Anonymous

    Anonymous - 2015-09-03

    Hi
    Thank you for your respond
    What I mean is that, I want to set and assign the decision variables to to hold the values of variables that I have already initialized and saved in an array [3][4].

     
  • Anonymous

    Anonymous - 2015-09-04

    Hi
    in addition to the previous question
    Can I set the decision variables to hold values of some other variables used elsewhere within the program as follows:

    solution.setVariable(0, EncodingUtils.newReala));
    solution.setVariable(1, EncodingUtils.newRealb));
    solution.setVariable(2, EncodingUtils.newReal(c));

    where (a,b,c) are some variables that their values are changing in each iteration.

     
  • Anonymous

    Anonymous - 2015-09-04

    I'm not 100% sure I understand the question, so please ask for clarification if I'm not answering your question. I may also help if you can attach some example code of your problem.

    The decision variables can only be defined using a lower and upper value:

    solution.setVariable(0, EncodingUtils.newReal(lower, upper));
    

    This variable will be assigned values within these bounds by the algorithm. It sounds like you want to have some decision variables set to a specific value that you are calculating elsewhere in the program. If that's the case, then you likely do not want to save them as decision variables. Instead, just access the variables directly.

    public class MyProblem implements Problem {
        public double a = 0.5;
    
        public void evaluate(Solution solution) {
            double[] x = EncodingUtils.getReal(solution);
            double obj1= a * x[0];
            ...
        }
    }
    

    You can then change the value of a anywhere in your program. If, for example, you want to update the value every 100 evaluations:

    public class MyProblem implements Problem {
        public double a = 0.5;
    
        public int evaluations = 0;
    
        public void evaluate(Solution solution) {
            evaluations++;
    
            if (evaluations % 100 == 0) {
                a = ...update value of a...
            }
    
            double[] x = EncodingUtils.getReal(solution);
            double obj1= a * x[0];
            ...
        }
    }
    
     
  • Anonymous

    Anonymous - 2015-09-07

    Thanks a lot for your valued feedback, the code now performing much better and needs some more modification to be more reliable.

    Best regards