2017-02-17 130 views
0

我已經使用AnyLogic構建了我的第一個基於代理的模型,現在我需要校準它。我嘗試了可用的實驗,但它耗時太長,我無法模擬超過10,000個代理。所以我想嘗試一下自定義實驗......但是我不知道如何構建它!AnyLogic - 如何創建自定義實驗

我建立了一個非常簡單的模型 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

我不明白爲什麼客觀總是零...

感謝您的幫助!

+0

我還沒有很多的自定義實驗。主要是參數變化。但是你在哪裏告訴實驗最後階段的代理人數量? –

回答

0

AnyLogic的個人學習版對有多少代理可以做某些事情有一些限制,而且它也不允許自定義實驗。也許AnyLogic默默無聞。只是在黑暗中拍攝,因爲我不是常規的AnyLogic用戶。