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

AnyLogic多目标优化中获取OptQuest解非决策变量值的方案咨询

可行实现方案

方案1:最优解重跑仿真(最稳妥,零侵入原优化逻辑)

你拿到最优解的决策变量参数后,手动启动一次单次仿真即可,你用了固定随机种子1,仿真结果和优化过程中运行的结果完全一致,不会有误差。
在你for循环中获取bestSolution后添加如下代码:

// 手动初始化仿真引擎拿非决策变量
Engine tempEngine = createEngine();
tempEngine.setStartTime(0.0);
tempEngine.setTimeUnit(DAY);
tempEngine.setStopTime(365);
Main tempRoot = new Main(tempEngine, null, null);
tempRoot.setParametersToDefaultValues();
// 赋值最优解的决策变量
tempRoot.Rr = (int)bestSolution.GetVariableValue(Rr);
tempRoot.RQ = (int)bestSolution.GetVariableValue(RQ);
tempRoot.Dr = (int)bestSolution.GetVariableValue(Dr);
tempRoot.DQ = (int)bestSolution.GetVariableValue(DQ);
tempRoot.Mr = (int)bestSolution.GetVariableValue(Mr);
tempRoot.MQ = (int)bestSolution.GetVariableValue(MQ);
tempRoot.getDefaultRandomGenerator().setSeed(1);
tempEngine.start(tempRoot);
tempEngine.runFast();
// 直接拿你需要的非决策变量
double rBackOrderCost = tempRoot.RBackOrderCost;
double rCarryingCost = tempRoot.RCarryingCost;
tempEngine.stop();

之后把这两个变量加到你原来的traceln格式化输出里就行。


方案2:使用Solution的UserTag存储附加数据(无需额外跑仿真)

OptQuest的COptQuestSolution原生支持自定义用户数据挂载,你可以在仿真回调里把需要的非决策变量直接存到当前solution里,后续直接读取即可。

  1. 首先在evaluate回调的仿真跑完、引擎销毁前,添加数据挂载代码:
// 原有设置目标、约束的代码不变
solution.SetObjectiveValue(obj1, root.RObj);
solution.SetObjectiveValue(obj2, root.MultObjs);
solution.SetRequirementValue(ReqROverstock,root.ROverstock);
// 新增:打包非决策变量存到solution的UserTag里
HashMap<String, Double> extraMetrics = new HashMap<>();
extraMetrics.put("RBackOrderCost", root.RBackOrderCost);
extraMetrics.put("RCarryingCost", root.RCarryingCost);
extraMetrics.put("DProfit", root.DProfit);
extraMetrics.put("MProfit", root.MProfit);
solution.SetUserTag(extraMetrics);
// 原有引擎销毁代码不变
engine.stop();
  1. 之后拿到bestSolution时直接读取即可:
COptQuestSolution bestSolution = opt.GetBestSolution();
HashMap<String, Double> extraMetrics = (HashMap<String, Double>) bestSolution.GetUserTag();
double rBackOrderCost = extraMetrics.get("RBackOrderCost");
double rCarryingCost = extraMetrics.get("RCarryingCost");
double dProfit = extraMetrics.get("DProfit");
double mProfit = extraMetrics.get("MProfit");

之前方案失效原因说明

  • 虚拟决策变量/虚拟目标方案:OptQuest会对所有注册的变量做寻优调整,你写入的值会被寻优逻辑覆盖,所以存不住
  • 外部数组存储方案:你没有给每个解做唯一标识匹配,拿到最优解后无法对应到数组中哪条是它的记录,只要用决策变量组合做key存HashMap就可以正常使用

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 19:54:01