0
votes

I'm trying to build a custom experiment...but it gives the following error enter image description here

I don't get what is going wrong!

It is because in the Main I defined the following? enter image description here

At the very beginning I want one agent to receive a message, why the custom experiment gives me trouble while the normal simulation experiment doesn't?

The code for the custom experiment I took it from the example models

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

1 Answers

0
votes

It's hard to say definitively from that detail but I suspect what's happening is:

  • your susc_s is a parameter/variable which affects the number of agents starting in the S state

  • your optimisation experiment tries different values of this parameter. (That's what optimisations do given you mapped this to your continuous optimisation variable.)

  • it chooses 0 for the first run, which means your Main startup code doesn't find any agent in that state and thus returns null for a (causing the exception)

Probably when you run it manually via your Simulation experiment you are setting susc_s to some non-zero value (or a value that always causes some agents to start in your S state).

(Or there's some other model parameter that affects the number of agents starting in the S state. Because you haven't set this up explicitly in your custom experiment, it gets the default value which means no agents start in the S state. You set up the optimisation variables to map to the model parameters you want to vary but you still need to set any other model parameters appropriately unless the default values are all OK.)