How to add a sum inside another sum under execute in cplex

26 views Asked by At

Good day to all. I ask you to help me with this issue. I have to add this formula into my cplex code:

Probfunction

I am struggling to add the sum inside the sum in the denominator. Can anyone please help me on how this code would work on cplex? Specifically, under execute. I thank you in advance for any help.

I tried to add the sum in two parts, but it did not work, since the sums need to happen simultaneously.

1

There are 1 answers

0
Alex Fleischer On

You can write the formula in OPL and then use it in an execute block. Or you can use a loop in the execute block.

range r=1..10;
int a[i in r]=i;

float f=sum(i in r)a[i]*a[i]/sum(i in r) i;

execute
{
  var f2a=0;
  var f2b=0;
  for(var i in r)
  {
    f2a+=a[i]*a[i];
    f2b+=i;
  }
  var f2=f2a/f2b;
  writeln("f=",f);
  writeln("f2=",f2);
}

You get the same result either way