Optimizing agent-based models

Sometimes we need to fine-tune our ABMs parameters to a specific outcome. The brute-force solution can quickly become infeasible for even for a few different parameter settings over a number of valid scan ranges. Most of the time, ABMs are also stochastic, so the effect of a parameter setting should be derived from taking the average value only after running the model several times.

Here we show how to use the evolutionary algorithms in BlackBoxOptim.jl with Agents.jl, to optimize the parameters of an epidemiological model (SIR). We explain this model in detail in SIR model for the spread of COVID-19. For brevity here, we just import

include("siroptim.jl") # From the examples directory

which provides us a model_initiation helper function to build a SIR model, and an agent_step! function.

To look for optimal parameters, we need to define a cost function. The cost function takes as arguments the model parameters that we want to tune; in a SIR model, that would be the migration rate, death rate, transmission rate, when an infected person has been detected (β_det), or when the remain undetected (β_und), infection period, reinfection probability, and time until the infection is detected. The function returns an objective: this value takes the form one or more numbers, which the optimiser will attempt to minimize.

using BlackBoxOptim, Random
using Statistics: mean

function cost(x)
    model = model_initiation(;
        Ns = [500, 500, 500],
        migration_rate = x[1],
        death_rate = x[2],
        β_det = x[3],
        β_und = x[4],
        infection_period = x[5],
        reinfection_probability = x[6],
        detection_time = x[7],
    )

    infected_fraction(model) =
        count(a.status == :I for a in allagents(model)) / nagents(model)

    _, data = run!(
        model,
        agent_step!,
        50;
        mdata = [infected_fraction],
        when_model = [50],
        replicates = 10,
    )

    return mean(data.infected_fraction)
end
cost (generic function with 1 method)

This cost function runs our model 10 times for 50 days, then returns the average number of infected people. When we pass this function to an optimiser, we will effectively be asking for a set of parameters that can reduce the number of infected people to the lowest possible number.

We can now test the function cost with some reasonable parameter values.

Random.seed!(10)

x0 = [
    0.2,  # migration_rate
    0.1,  # death_rate
    0.05, # β_det
    0.3,  # β_und
    10,   # infection_period
    0.1,  # reinfection_probability
    5,    # detection_time
]
cost(x0)
0.9348851444455306

With these initial values, 94% of the population is infected after the 50 day period.

We now let the optimization algorithm change parameters to minimize the number of infected individuals. Complete details on how to use this optimiser can be found in the BlackBoxOptim readme. Here, we assign a range of possible parameter values we would like to test, and a cutoff time in the event that certain parameter sets are unfeasible and cause our model to never converge to a solution.

result = bboptimize(
    cost,
    SearchRange = [
        (0.0, 1.0),
        (0.0, 1.0),
        (0.0, 1.0),
        (0.0, 1.0),
        (7.0, 13.0),
        (0.0, 1.0),
        (2.0, 6.0),
    ],
    NumDimensions = 7,
    MaxTime = 20,
)
best_fitness(result)
0.11400568097805301

With the new parameter values found in result, we find that the fraction of the infected population can be dropped down to 11%. These values of these parameters are now:

best_candidate(result)
7-element Array{Float64,1}:
  0.14295189314028073
  0.9563511728519357
  0.11642612651074881
  0.1247861896135058
 12.633440015975424
  0.0019240394102884694
  3.8685012358501547

Unfortunately we've not given the optimiser information we probably needed to. Notice that the death rate is 96%, with reinfection quite low. When all the infected individuals die, infection doesn't transmit - the optimiser has managed to reduce the infection rate by killing the infected.

This is not the work of some sadistic AI, just an oversight in our instructions. Let's modify the cost function to also keep the mortality rate low.

First, we'll run the model with our new-found parameters:

x = best_candidate(result)

Random.seed!(0)

model = model_initiation(;
    Ns = [500, 500, 500],
    migration_rate = x[1],
    death_rate = x[2],
    β_det = x[3],
    β_und = x[4],
    infection_period = x[5],
    reinfection_probability = x[6],
    detection_time = x[7],
)

_, data =
    run!(model, agent_step!, 50; mdata = [nagents], when_model = [50], replicates = 10)

mean(data.nagents)
1358.9

About 10% of the population dies with these parameters over our 50 day window.

We can define a multi-objective cost function that minimizes the number of infected and deaths by returning more than one value in our cost function.

function cost_multi(x)
    model = model_initiation(;
        Ns = [500, 500, 500],
        migration_rate = x[1],
        death_rate = x[2],
        β_det = x[3],
        β_und = x[4],
        infection_period = x[5],
        reinfection_probability = x[6],
        detection_time = x[7],
    )

    initial_size = nagents(model)

    infected_fraction(model) =
        count(a.status == :I for a in allagents(model)) / nagents(model)
    n_fraction(model) = -1.0 * nagents(model) / initial_size

    _, data = run!(
        model,
        agent_step!,
        50;
        mdata = [infected_fraction, n_fraction],
        when_model = [50],
        replicates = 10,
    )

    return mean(data.infected_fraction), mean(data.n_fraction)
end
cost_multi (generic function with 1 method)

Notice that our new objective n_fraction is negative. It would be simpler to state we'd like to 'maximise the living population', but the optimiser we're using here focuses on minimising objectives only, therefore we must 'minimise the number of agents dying'.

cost_multi(x0)
(0.9613671038668243, -0.7817999999999999)

The cost of our initial parameter values is high: most of the population (96%) is infected and 22% die.

Let's minimize this multi-objective cost function. There is more than one way to approach such an optimisation. Again, refer to the BlackBoxOptim readme for specifics.

result = bboptimize(
    cost_multi,
    Method = :borg_moea,
    FitnessScheme = ParetoFitnessScheme{2}(is_minimizing = true),
    SearchRange = [
        (0.0, 1.0),
        (0.0, 1.0),
        (0.0, 1.0),
        (0.0, 1.0),
        (7.0, 13.0),
        (0.0, 1.0),
        (2.0, 6.0),
    ],
    NumDimensions = 7,
    MaxTime = 55,
)
best_fitness(result)
(0.0002010274938093588, -0.9970666666666664)
best_candidate(result)
7-element Array{Float64,1}:
 0.33047213757587385
 0.4162200712338514
 0.19614410158370602
 0.001988769978834718
 7.602018447426495
 0.5364418966125286
 4.025083782376916

These parameters look better: about 0.3% of the population dies and 0.02% are infected:

The algorithm managed to minimize the number of infected and deaths while still increasing death rate to 42%, reinfection probability to 53%, and migration rates to 33%. The most important change however, was decreasing the transmission rate when individuals are infected and undetected from 30% in our initial calculation, to 0.2%.

Over a longer period of time than 50 days, that high death rate will take its toll though. Let's reduce that rate and check the cost.

x = best_candidate(result)
x[2] = 0.02
cost_multi(x)
(0.00046675567423230975, -0.9996666666666666)

The fraction of infected increases to 0.04%. This is an interesting result: since this virus model is not as deadly, the chances of re-infection increase. We now have a set of parameters to strive towards in the real world. Insights such as these assist us to enact countermeasures like social distancing to mitigate infection risks.