AnyLogic - 如何创建自定义实验

时间:2017-02-17 10:02:16

标签: anylogic

我已经使用AnyLogic构建了我的第一个基于代理的模型,现在我需要校准它。我尝试了可用的实验,但它需要太长时间,我无法模拟超过10,000个代理。所以我想尝试一个自定义实验......但我不知道如何构建它!

我构建了一个非常简单的模型 enter image description here 其中参数为bernoulli(main.calib),然后我想最大化进入最终状态的代理数量。

我从帮助中提供的实验中复制了代码并进行了一些小的调整

try {
// Create Engine, initialize random number generator:
Engine engine = createEngine();
// Set stop time:
engine.setStopTime( 2 );

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

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

// 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.calib = 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.end );
            // 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.5);
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()); }

以下是结果......

enter image description here

我不明白为什么目标始终为零......

感谢您的帮助!!

1 个答案:

答案 0 :(得分:0)

AnyLogic的个人学习版对可以执行某些操作的代理程序有一些限制,并且它也不允许自定义实验。也许AnyLogic正在默默地失败。只是在黑暗中拍摄,因为我不是常规的AnyLogic用户。