AnyLogic - 自定义实验的麻烦

时间:2017-03-01 15:43:57

标签: anylogic

我正在尝试构建自定义实验...但它会出现以下错误enter image description here

我不知道出了什么问题!

这是因为在Main我定义了以下内容? enter image description here

一开始我想让一个代理收到一条消息,为什么自定义实验会给我带来麻烦,而正常的模拟实验却没有?

我从示例模型

中获取的自定义实验的代码
try {
// Create Engine, initialize random number generator:
Engine engine = createEngine();
// Set stop time:
engine.setStopTime( 3287 );

// Create optimization variable 
final COptQuestContinuousVariable v = new COptQuestContinuousVariable();
v.SetLowerBound(0.0);
v.SetUpperBound(0.03);

// Create objective
final COptQuestObjective obj = new COptQuestUserControlledObjective();
obj.SetMinimize();

// Create optimization engine
final COptQuestOptimization opt = ExperimentOptimization.createOptimization(engine, new OptimizationCallback() {

    @Override
    public void evaluate(COptQuestOptimization optimization,
            COptQuestSolution solution, Engine engine) {
        try {
            // Create new root object:
            Main root = new Main( engine, null, null );
            // Setup parameters of root object here
            root.susc_s = solution.GetVariableValue(v);
            // Prepare Engine for simulation:
            engine.start( root );
            // Start simulation in fast mode:
            engine.runFast();
            // Process results of simulation here
            solution.SetObjectiveValue( obj, root.objective );
            // Destroy the model:
            engine.stop();
        } catch (COptQuestException e) {
            traceln(e.Description());
        }
    }

    // Trace each iteration (optional!)
    @Override
    public void monitorStatus(COptQuestOptimization optimization,
            COptQuestSolution solution, Engine engine) {
        try {
            traceln(String.format("  %3d : %6.2f : %8.2f  -- %8.2f",
                solution.GetIteration(), solution.GetVariableValue(v),
                solution.GetObjectiveValue(),
                optimization.GetBestSolution() != null ?
                optimization.GetBestSolution().GetObjectiveValue(obj) : Double.NaN));
        } catch (COptQuestException e) {
            traceln(e.Description());
        }
    }

});

// Setup optimization engine
opt.AddVariable(v);
opt.AddObjective(obj);
// Set the number of iterations to run
opt.SetMaximumIterations(30);

// Add suggested solution (initial solution)
COptQuestSolution suggestedSolution = opt.CreateSolution();
suggestedSolution.SetVariableValue(v, 0.014);
opt.AddSuggestedSolution(suggestedSolution);

traceln(" Iter : Param  : Objective -- Best obj.");
traceln("-------------------------------------------");
// Perform optimization
opt.Optimize();
traceln("-------------------------------------------");

// Output results
COptQuestSolution bestSolution = opt.GetBestSolution();
traceln("Best objective: " + format(bestSolution.GetObjectiveValue(obj)));
traceln("   is feasible: " + format(bestSolution.IsFeasible()));
traceln("Best parameter: " + format(bestSolution.GetVariableValue(v)));
traceln("Best iteration: " + bestSolution.GetIteration());  }     catch (COptQuestException e) {
traceln(e.Description());  }

1 个答案:

答案 0 :(得分:0)

很难从这个细节中明确地说出来,但我怀疑发生了什么:

  • 您的susc_s是一个参数/变量,会影响以S状态开始的代理人数

  • 您的优化实验会尝试此参数的不同值。 (这就是你将优化映射到连续优化变量时所做的优化。)

  • 它为第一次运行选择0,这意味着您的Main启动代码找不到该状态的任何代理,因此返回null a(引起异常)

当您通过Simulation实验手动运行时,您可能会将susc_s设置为某个非零值(或一个值会导致某些代理以S状态启动)。

(或者还有一些其他模型参数会影响以S状态开始的代理数量。因为您还没有在自定义实验中明确设置它,所以它会获得默认值,这意味着没有代理程序以S状态启动。您设置优化变量以映射到要更改的模型参数,但仍需要适当设置任何其他模型参数,除非默认值都正常。)