0
我已经使用AnyLogic构建了我的第一个基于代理的模型,现在我需要校准它。我尝试了可用的实验,但它耗时太长,我无法模拟超过10,000个代理。所以我想尝试一下自定义实验......但是我不知道如何构建它!AnyLogic - 如何创建自定义实验
我建立了一个非常简单的模型 其中参数是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()); }
和下面是结果...
我不明白为什么客观总是零...
感谢您的帮助!
我还没有很多的自定义实验。主要是参数变化。但是你在哪里告诉实验最后阶段的代理人数量? –