0

I am trying to write the code of the progressive hedge algorithm in OPL CPLEX. However, I am unable to write the correct code in OPL CPLEX. The progressive hedge algorithm algorithm is attached here. Can you suggest to me the correct code of this algorithm in OPL CPLEX: (progressive hedge alorithm)

The code that I am trying is given below:

execute {
    writeln("Starting Progressive Hedging Algorithm...");

    // Step 2: Initial solution for all subproblems
    for (var s in scenario) {
        // Solve initial subproblem
        thisOplModel.z = z[s];
        thisOplModel.x = x[s];
        thisOplModel.generate();
        if (thisOplModel.solve()) {
            zSub[s] = thisOplModel.z[s];
            xSub[s] = thisOplModel.x[s];
        } else {
            writeln("Failed to solve subproblem for scenario ", s);
        }
    }

    // Compute initial averages
    // Compute weighted averages for z and x after solving subproblems in the current iteration
    zAvg = sum(s in scenario) (omega[s] * zSub[s]);
    xAvg = sum(s in scenario) (omega[s] * xSub[s]);

    // Step 3: Iterative process
    while (iter < maxIter) {
        iter++;
        writeln("Iteration ", iter);

        // Update multipliers and solve each subproblem
        for (var s in scenario) {
            // Update multipliers
            m[s] += rhoZ * (zPrev[s] - zAvg);
            w[s] += rhoX * (xPrev[s] - xAvg);

            // Define relaxed subproblem
            thisOplModel.z = z[s];
            thisOplModel.x = x[s];
            thisOplModel.m = m[s];
            thisOplModel.w = w[s];
            thisOplModel.generate();
            if (thisOplModel.solve()) {
                zSub[s] = thisOplModel.z[s];
                xSub[s] = thisOplModel.x[s];
            } else {
                writeln("Failed to solve subproblem for scenario ", s);
            }
        }

        // Compute averages
        zAvg = sum(s in scenario) (omega[s] * zSub[s]);
        xAvg = sum(s in scenario) (omega[s] * xSub[s]);

        // Compute convergence metric
        float g = sum(s in scenario) (omega[s] * (abs(zSub[s] - zAvg) + abs(xSub[s] - xAvg)));

        writeln("Convergence metric g: ", g);

        // Check for convergence
        if (g < epsilon) {
            writeln("Convergence achieved at iteration ", iter);
            break;
        }

        // Update previous solutions
        for (var s in scenario) {
            zPrev[s] = zSub[s];
            xPrev[s] = xSub[s];
        }
    }
}

The error is -- Scripting parser error: missing ')'.

1 Answer 1

0

You wrote

zAvg = sum(s in scenario) (omega[s] * zSub[s]);
xAvg = sum(s in scenario) (omega[s] * xSub[s]);

But as I shared in https://github.com/AlexFleischerParis/oplscripting/blob/main/commonpitfalls.mod

sum works in OPL but not in OPL scripting. You should use a workaround to compute the sum in scripting.

// Pitfall 1 : mix OPL and OPL scripting which are 2 langguages

range r=1..5;
int v[i in r]=i;

// In OPL we can write
int su=sum(i in r) v[i];

execute
{
  writeln("su=",su);
  // which gives su=15
}

execute
{
// But in scripting we cannot write
// int su=sum(i in r) v[i];
// because that's OPL
// what we can write:

function compute_sum_array(vRange,vArray)
{
 var s=0;
 for(var i in vRange) s+=vArray[i];
 return s; 
}  

var su2=compute_sum_array(r,v);
writeln("su2=",su2);
  // which gives su2=15
  
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.