You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在CPLEX中按单元格索引从Excel读取元组并支持动态范围?

Using Cell Indices with Variable Ranges in CPLEX SheetRead

Absolutely, you can make this work! The key is to dynamically construct the range string using CPLEX's string concatenation function, since your n value changes per scenario. Here's a step-by-step breakdown of how to implement this:

Step 1: Define or load your variable n

First, ensure n is a known parameter (not a decision variable—SheetRead runs during data initialization, before solving). You can either hardcode it or read it directly from Excel:

int n;
// Example: Read n from Excel cell (1,0) (which is A2 in the "Data" sheet)
n from SheetRead(sheet, "'Data'!(1,0)");

Step 2: Build the dynamic range string

Use the concat function to assemble your cell index range, inserting the variable n into the string:

string tupleRange = concat("'Data'!(0,0):(0,", n, ")");

This will generate a string like 'Data'!(0,0):(0,5) if n equals 5, exactly matching your desired format.

Step 3: Load your tuple using the dynamic range

Pass the constructed string to SheetRead to pull in your tuple data:

// Define your tuple structure first
tuple MyTuple {
    int id;
    float value;
    // Add other elements as needed for your use case
}

// Declare your tuple array
MyTuple T[];

// Load data with the dynamically generated range
T from SheetRead(sheet, tupleRange);

Important Notes

  • CPLEX uses 0-based indexing for cell coordinates, so (0,0) directly corresponds to Excel's A1 cell—this aligns perfectly with your initial example.
  • n must be a parameter (a fixed value known at data input time). Decision variables won't work here because SheetRead executes before the solve phase begins.
  • Double-check that n is a non-negative integer and that your Excel sheet has enough data in the specified range. Out-of-bounds ranges will trigger CPLEX errors.

内容的提问来源于stack exchange,提问作者StabiloHB

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 19:07:50