OPL中定义索引集合(集合的集合)及参数声明方法咨询
OPL集合的集合实现方案
OPL本身不直接支持原生的嵌套集合类型(即集合的集合),但可以通过元组+集合数组的组合方式实现你需要的索引集合+关联参数的效果,逻辑完全对齐你在AMPL中的写法。
完整实现示例如下:
1. 基础类型与结构定义
// 定义计算平台枚举 enum CompPlatforms { DC1, DC2, MP1 }; // 定义配置枚举 enum Confs { conf1, conf2, conf3 }; // 定义元组存储合法的<平台+配置>组合,用于关联属性参数 tuple PlatformConf { CompPlatforms platform; Confs conf; }
2. 实现索引集合(等价AMPL的set CONFCP{COMPPLATFORMS})
// 集合数组:每个计算平台对应自己支持的配置集合 {Confs} confCP[CompPlatforms] = [ {"conf1", "conf3"}, // DC1支持的配置 {"conf1", "conf2", "conf3"}, // DC2支持的配置 {"conf1", "conf3"} // MP1支持的配置 ];
3. 定义vcpu参数(等价AMPL的param vcpu {d in COMPPLATFORMS, CONFCP[d]})
// 生成所有合法的<平台+配置>组合集合 {PlatformConf} validPlatformConf = {<p, c> | p in CompPlatforms, c in confCP[p]}; // 基于合法组合集合定义vcpu参数 float vcpu[validPlatformConf] = [ <DC1, conf1>: 8, <DC1, conf3>: 16, <DC2, conf1>: 4, <DC2, conf2>: 8, <DC2, conf3>: 32, <MP1, conf1>: 2, <MP1, conf3>: 4 ];
4. 调用示例
execute { // 遍历所有合法组合输出vcpu值 for (var pc in validPlatformConf) { writeln("平台:", pc.platform, " 配置:", pc.conf, " vcpu容量:", vcpu[pc]); } }
如果需要做模型和数据分离,你可以把confCP、vcpu的数值部分放到.dat文件中定义,不需要硬编码在.mod模型文件里。
内容的提问来源于stack exchange,提问作者Annisa Sarah
相关产品推荐
相关产品推荐

