CPLEX OPL 中 wrtiern 函数的脚本解析器错误

问题描述 投票:0回答:1

我在 CPLEX 中使用 writeln 函数时遇到一些错误

第 40 行出错 错误信息

.mod 文件

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]);
}

上图是我遇到脚本解析器错误:缺少')'的错误。我已经尝试通过分离 wrtiern 的三个部分来调试执行部分,到目前为止,第一个 writeln 我能够打印哪个塔被选中,所以这不是问题。但是当我单独放置第二个和第三个时出现错误,但不太确定脚本错误是什么,因此需要一些帮助,谢谢!

.dat 文件供参考

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;

cplex
1个回答
0
投票

这是一个常见错误:混合使用 OPL 建模语言和 OPL 脚本语言/

如果你写

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);
}

然后你的显示器就可以正常工作了

© www.soinside.com 2019 - 2024. All rights reserved.