def evaluate_model(params, i, D, return_dict):
    print("Starting sensitivity run %i out of %i" % (i + 1, len(D)))

    # Defining model parameters
    model_params, _ = data.get_params()
    for var, val in zip(vars, params):
        model_params[var] = val

    # Initiate and run models
    models = initiate_models(model_params)
    run_models(models)

    # Assign data to patches
    patches = data.assign_data(models)

    results = []
    for t in range(measurements):
        t += 1
        mean = get_average_biomass(models, t)
        loss = get_total_loss(patches, t)
        results += [*mean, *loss]

    return_dict[i] = results

    print("Finishing sensitivity run %i out of %i" % (i + 1, len(D)))
    return
Ejemplo n.º 2
0
def minimize_total(param, param_range, params_model, iterations, subset_nr, artificial, fname):
    loss_list = []
    optimalisation_list = []
    values = np.linspace(*param_range, iterations)
    for value in values:
        print("%s value: %.3f" % (param, value))

        # Collect parameters for fitting
        these_params = copy.deepcopy(params_model)
        these_params[param] = value

        # Initiate and run models
        models = initiate_models(these_params)
        run_models(models)

        # Assign data to patches
        patches = data.assign_data(models, artificial)

        # Subset patches for parameter fitting
        if subset_nr == 5: subset_nr = 4
        subsets = subset.all(patches)[subset_nr]

        loss_list.append((
            *get_total_loss(patches, 1), *get_subset_loss(subsets, 1),
            *get_total_loss(patches, 2), *get_subset_loss(subsets, 2),
            *get_total_loss(patches, 3), *get_subset_loss(subsets, 3)
        ))

    # total_loss = get_total_loss(patches, 2)
    # optimalisation_list.append(total_loss)
    # opt_RL, opt_BR = zip(*optimalisation_list)
    # i = opt_RL.index(min(opt_RL))
    # j = opt_BR.index(min(opt_BR))
    #
    # if param in ['c_rr', 'c_rb']:
    #     params_model[param] = values[i]
    # elif param in ['c_bb', 'c_br']:
    #     params_model[param] = values[j]
    # else:
    #     params_model[param] = values[int(round((i+j)/2))]

    np.savetxt(fname, loss_list)
    return
Ejemplo n.º 3
0
def minimize_diam(param, param_range, params_model, iterations, artificial, fname):
    loss_list = []
    values = np.linspace(*param_range, iterations)
    for value in values:
        print("%s value: %.3f" % (param, value))

        # Collect parameters for fitting
        these_params = copy.deepcopy(params_model)
        these_params[param] = value

        # Initiate and run models
        models = initiate_models(these_params)
        run_models(models)

        # Assign data to patches
        patches = data.assign_data(models, artificial)

        # Calculate loss
        loss_list.append(get_diameter_loss(patches))

    np.savetxt(fname, loss_list)
    return
Ejemplo n.º 4
0
        p, _ = data.get_params()
        p["alpha"], p["gamma"] = .15, .30
        p["c_rr"], p["c_bb"] = .01, .05
        p["c_rb"], p["c_br"] = .20, .15
        create_artificial_data(p)

    elif run_type == 'v':
        """
        Run interactive visualisation of model
        Second argument is the timestep-interval
        """
        # set-up models
        models = initiate_models(params_model)

        # set-up visualisation
        data.assign_data(models)
        visualisation = Visualize(models)

        # run models
        for t in data.daterange():
            print("Timestep %i" % data.get_timestep(t))
            for model in models:
                if t in data.measurements:
                    model.collect_data_fit()
                model.step(visualize=True)
            if data.get_timestep(t) % vis_steps == 0:
                visualisation.update()
                res = input(
                    "Press enter to continue simulation, type 'q' to stop current run:\n\t"
                )
                if res == 'q':
    artificial = False
    prob_range = (.01, .04)
    N = 6
    probs = np.linspace(*prob_range, N)

    errors = []
    for value in probs:
        print("Running model for seed probability ", value)
        these_params, _ = data.get_params()
        these_params['seed_prob'] = value

        # Initiate and run models
        models = initiate_models(these_params)
        run_models(models)

        # Assign data to patches
        patches = data.assign_data(models, artificial)

        error = 0
        for patch in patches:
            if len(patch.BR_original) != 0:
                model_size = get_size(patch)
                # TODO: Use also other diameters than the last one?
                error += abs(model_size - patch.size[-1])

        errors.append(error)
        np.savetxt(fname, errors)

    results = np.loadtxt(fname)
    plt.plot(results)
    plt.show()