コード例 #1
0
def optimize(model,
             scenario,
             nfe,
             epsilons,
             sc_name,
             algorithm=EpsNSGAII,
             searchover='levers'):
    '''optimize the model
    
    Parameters
    ----------
    model : a Model instance
    algorith : a valid Platypus optimization algorithm
    nfe : int
    searchover : {'uncertainties', 'levers'}
    
    Returns
    -------
    pandas DataFrame
    
    
    Raises
    ------
    EMAError if searchover is not one of 'uncertainties' or 'levers'
    
    TODO:: constrains are not yet supported
    
    '''
    if searchover not in ('levers', 'uncertainties'):
        raise EMAError(("searchover should be one of 'levers' or"
                        "'uncertainties' not {}".format(searchover)))

    # extract the levers and the outcomes
    decision_variables = [dv for dv in getattr(model, searchover)]
    outcomes = [
        outcome for outcome in model.outcomes
        if outcome.kind != AbstractOutcome.INFO
    ]

    evalfunc = functools.partial(evaluate_function,
                                 model=model,
                                 scenario=scenario,
                                 decision_vars=decision_variables,
                                 searchover=searchover)

    # setup the optimization problem
    # TODO:: add constraints
    problem = Problem(len(decision_variables), len(outcomes))
    problem.types[:] = [
        Real(dv.lower_bound, dv.upper_bound) for dv in decision_variables
    ]
    problem.function = evalfunc
    problem.directions = [outcome.kind for outcome in outcomes]

    # solve the optimization problem
    optimizer = algorithm(problem, epsilons=epsilons)
    optimizer.run(nfe)

    # extract the names for levers and the outcomes
    lever_names = [dv.name for dv in decision_variables]
    outcome_names = [outcome.name for outcome in outcomes]

    solutions = []
    for solution in unique(nondominated(optimizer.result)):
        decision_vars = dict(zip(lever_names, solution.variables))
        decision_out = dict(zip(outcome_names, solution.objectives))
        result = {**decision_vars, **decision_out}
        solutions.append(result)

    #print("fe_result: ", optimizer.algorithm.fe_results)
    #plot_convergence(optimizer.algorithm.hv_results, sc_name)
    results = pd.DataFrame(solutions, columns=lever_names + outcome_names)

    #save the hypervolume output in a csv file
    hv = np.swapaxes(
        np.array(optimizer.algorithm.hv_results), 0, 1
    )  #hv is a 2d list, where hv[0] is the record of nfe's, hv[1] is the record of hypervolume
    df = pd.DataFrame(hv).transpose()
    #df.to_csv("Hypervolume_scenario_{}_v6.csv".format(sc_name))

    return results, df
コード例 #2
0
def cubicTraj(values):
    theta0 = 15
    thetaf = 75
    t0 = 0
    tf = values[0]
    
    a3 = (-2/tf**3)*(thetaf - theta0)
    a2 = (3/tf**2)*(thetaf - theta0)
    a1 = 0
    a0 = theta0
    
    jerk = np.sqrt((6*a3)**2)
    
    return [tf,jerk]

problem = Problem(1,2)
problem.types[:] = Real(3,20)
problem.directions = [-1,-1]
problem.function = cubicTraj

algorithm = NSGAII(problem)
algorithm.run(10000)

tf_opt = [s.objectives[0] for s in algorithm.result]
jerk_opt = [s.objectives[1] for s in algorithm.result]

plt.scatter(tf_opt,jerk_opt)
plt.show()

tf = 5.23705
jerk = 4.96