lim kiat tsen
lim kiat tsen

Reputation: 1

Scripting parser error on writeln function in CPLEX OPL

Im having some error using the writeln function in CPLEX

Error for line 40 error message

.mod file

setof(int) cellTowers=...;
setof(int) Regions=...;

int Population[Regions]=...;
int Coverage[cellTowers][Regions]=...;
int Budget=...;

float Cost[cellTowers]=...;


 // Decision Variables
dvar boolean x[cellTowers]; // Binary variable: 1 if a tower is built, 0 otherwise

// Objective Function (to maximize coverage of all towers)
maximize sum(t in cellTowers, r in Regions) Population[r] * Coverage[t][r] * x[t];

// Constraints
subject to {
  // Budget Constraint
  sum(t in cellTowers) Cost[t] * x[t] <= Budget;

  // Optional: If specific constraints are needed, like mandatory coverage for certain regions
}


// Execute block for results
execute {
    writeln("Selected Towers: ");
    for (t in cellTowers)
        if (x[t] > 0.5)
            writeln("Tower ", t, " is selected.");

    writeln("Total Population Covered: ", 
        sum(t in cellTowers, r in Regions) Population[r] * Coverage[t][r] * x[t]);
    writeln("Total Cost: ", sum(t in cellTowers) Cost[t] * x[t]);
}

The picture above is the error where i get scripting parser error: missing ')'. I have tried debugging the execute part by separating the three parts of wrtieln so far the first writeln I am able to print which tower is selecte, so its not the problem. But error occurs when I put the second and third one individually, but not too sure what is the scripting error about, therefore need some help here thanks !

.dat file for reference

cellTowers = {0, 1, 2, 3, 4, 5}; // List of possible tower locations
Regions = {0, 1, 2, 3, 4, 5, 6, 7, 8}; // List of regions to cover

// Population in each region
Population = [523, 690, 420, 1010, 1200, 850, 400, 1008, 950];

// Coverage matrix: 1 if tower t covers region r, 0 otherwise

Coverage = [
  [1, 1, 0, 0, 0, 1, 0, 0, 0], // Tower 0 coverage
  [1, 0, 0, 0, 0, 0, 0, 1, 1], // Tower 1 coverage
  [0, 0, 1, 1, 1, 0, 1, 0, 0], // Tower 2 coverage
  [0, 0, 1, 0, 0, 1, 1, 0, 0], // Tower 3 coverage
  [1, 0, 1, 0, 0, 0, 1, 1, 1], // Tower 4 coverage
  [0, 0, 0, 1, 1, 0, 0, 0, 1], // Tower 5 coverage
];

// Cost of building each tower in millions
Cost = [4.2, 6.1, 5.2, 5.5, 4.8, 9.2];

// Total budget in millions
Budget = 20;

Upvotes: 0

Views: 45

Answers (1)

Alex Fleischer
Alex Fleischer

Reputation: 10062

This is a common error : you mix OPL modeling language and OPL scripting language/

If you write

float popcov=sum(t in cellTowers, r in Regions) (Population[r] * Coverage[t][r] * x[t]);
float totcost= sum(t in cellTowers) Cost[t] * x[t];
// Execute block for results
execute {
    writeln("Selected Towers: ");
    for (t in cellTowers)
        if (x[t] > 0.5)
            writeln("Tower ", t, " is selected.");

    writeln("Total Population Covered: ", 
        popcov);
    writeln("Total Cost: ",totcost);
}

then your display will work fine

Upvotes: 0

Related Questions