Ejemplo n.º 1
0
    # Parse the variable dictionary
    yaw = varDict["yaw"]

    # Compute the objective function
    funcs = {}
    funcs["obj"] = -1 * fi.get_farm_power_for_yaw_angle(yaw) / 1e5

    fail = False
    return funcs, fail


# Setup the optimization problem
optProb = pyoptsparse.Optimization("yaw_opt", objective_function)

# Add the design variables to the optimization problem
optProb.addVarGroup("yaw", 4, "c", lower=0, upper=20, value=2.0)

# Add the objective to the optimization problem
optProb.addObj("obj")

# Setup the optimization solver
# Note: pyOptSparse has other solvers available; some may require additional
#   licenses/installation. See https://github.com/mdolab/pyoptsparse for more
#   information. When ready, they can be invoked by changing 'SLSQP' to the
#   solver name, for example: 'opt = pyoptsparse.SNOPT(fi=fi)'.
opt = pyoptsparse.SLSQP(fi=fi)

# Run the optimization with finite-differencing
solution = opt(optProb, sens="FD")
print(solution)
Ejemplo n.º 2
0
def Pyoptsparse_Solve(problem,
                      solver='SNOPT',
                      FD='single',
                      sense_step=1.0E-6,
                      nonderivative_line_search=False):
    """ This converts your SUAVE Nexus problem into a PyOptsparse optimization problem and solves it.
        Pyoptsparse has many algorithms, they can be switched out by using the solver input. 

        Assumptions:
        None

        Source:
        N/A

        Inputs:
        problem                   [nexus()]
        solver                    [str]
        FD (parallel or single)   [str]
        sense_step                [float]
        nonderivative_line_search [bool]

        Outputs:
        outputs                   [list]

        Properties Used:
        None
    """

    # Have the optimizer call the wrapper
    mywrap = lambda x: PyOpt_Problem(problem, x)

    inp = problem.optimization_problem.inputs
    obj = problem.optimization_problem.objective
    con = problem.optimization_problem.constraints

    if FD == 'parallel':
        from mpi4py import MPI
        comm = MPI.COMM_WORLD
        myrank = comm.Get_rank()

    # Instantiate the problem and set objective

    try:
        import pyoptsparse as pyOpt
    except:
        raise ImportError('No version of pyOptsparse found')

    opt_prob = pyOpt.Optimization('SUAVE', mywrap)
    for ii in range(len(obj)):
        opt_prob.addObj(obj[ii, 0])

    # Set inputs
    nam = inp[:, 0]  # Names
    ini = inp[:, 1]  # Initials
    bnd = inp[:, 2]  # Bounds
    scl = inp[:, 3]  # Scale
    typ = inp[:, 4]  # Type

    # Pull out the constraints and scale them
    bnd_constraints = help_fun.scale_const_bnds(con)
    scaled_constraints = help_fun.scale_const_values(con, bnd_constraints)
    x = ini / scl

    for ii in range(0, len(inp)):
        lbd = (bnd[ii][0] / scl[ii])
        ubd = (bnd[ii][1] / scl[ii])
        #if typ[ii] == 'continuous':
        vartype = 'c'
        #if typ[ii] == 'integer':
        #vartype = 'i'
        opt_prob.addVar(nam[ii], vartype, lower=lbd, upper=ubd, value=x[ii])

    # Setup constraints
    for ii in range(0, len(con)):
        name = con[ii][0]
        edge = scaled_constraints[ii]

        if con[ii][1] == '<':
            opt_prob.addCon(name, upper=edge)
        elif con[ii][1] == '>':
            opt_prob.addCon(name, lower=edge)
        elif con[ii][1] == '=':
            opt_prob.addCon(name, lower=edge, upper=edge)

    # Finalize problem statement and run
    print(opt_prob)

    if solver == 'SNOPT':
        opt = pyOpt.SNOPT()
        CD_step = (sense_step**2.)**(1. / 3.
                                     )  #based on SNOPT Manual Recommendations
        opt.setOption('Function precision', sense_step**2.)
        opt.setOption('Difference interval', sense_step)
        opt.setOption('Central difference interval', CD_step)

    elif solver == 'SLSQP':
        opt = pyOpt.SLSQP()

    elif solver == 'FSQP':
        opt = pyOpt.FSQP()

    elif solver == 'PSQP':
        opt = pyOpt.PSQP()

    elif solver == 'NSGA2':
        opt = pyOpt.NSGA2(pll_type='POA')

    elif solver == 'ALPSO':
        #opt = pyOpt.pyALPSO.ALPSO(pll_type='DPM') #this requires DPM, which is a parallel implementation
        opt = pyOpt.ALPSO()

    elif solver == 'CONMIN':
        opt = pyOpt.CONMIN()

    elif solver == 'IPOPT':
        opt = pyOpt.IPOPT()

    elif solver == 'NLPQLP':
        opt = pyOpt.NLQPQLP()

    elif solver == 'NLPY_AUGLAG':
        opt = pyOpt.NLPY_AUGLAG()

    if nonderivative_line_search == True:
        opt.setOption('Nonderivative linesearch')
    if FD == 'parallel':
        outputs = opt(opt_prob, sens='FD', sensMode='pgc')

    elif solver == 'SNOPT' or solver == 'SLSQP':
        outputs = opt(opt_prob, sens='FD', sensStep=sense_step)

    else:
        outputs = opt(opt_prob)

    return outputs
Ejemplo n.º 3
0
optProb.addVarGroup('modes', 14, 'c', lower=None, upper=None, value=.5)
optProb.addVar('alpha', 'c', lower=low_alpha, upper=up_alpha, value=.5)

optProb.addCon('thick_0.1', lower=thickness_constraint[0], upper=None)
optProb.addCon('thick_0.3', lower=thickness_constraint[1], upper=None)
optProb.addCon('thick_0.5', lower=thickness_constraint[2], upper=None)
optProb.addCon('thick_0.7', lower=thickness_constraint[3], upper=None)
optProb.addCon('thick_0.9', lower=thickness_constraint[4], upper=None)
optProb.addCon('Cl', lower=Cl_req, upper=Cl_req)

optProb.addObj('obj')

print(optProb)
#%%
opt = pyoptsparse.SLSQP()
sol = opt(optProb, sens=sens)
print(sol)
#%%
modes_opt = sol.xStar['modes']
alpha_opt = sol.xStar['alpha']
Cd_opt = denormalize_y(sol.fStar, 1)
x0 = np.zeros((1, x_dim))
x0[0, 0:14] = airfoil
x0[0, 14] = Mach
x0_n = normalize_x(x0)
Cd_0 = Cd(x0_n)
print(Cd_opt, Cd_0)
x_opt = np.zeros((1, x_dim))
x_opt[0, 0:14] = modes_opt
x_opt[0, 15] = alpha_opt
Ejemplo n.º 4
0
                       lower=1,
                       upper=500,
                       value=start_solar)
        optProb.addVar("battery_storage_mwh",
                       type="c",
                       lower=0,
                       upper=1000,
                       value=start_battery_mwh)
        optProb.addVar("battery_storage_mw",
                       type="c",
                       lower=0,
                       upper=1000,
                       value=start_battery_mw)

        optProb.addObj("h_lcoe")
        optimize = pyoptsparse.SLSQP()
        optimize.setOption("MAXIT", value=5)
        # optimize.setOption("ACC",value=1E-6)
        # optimize = pyoptsparse.SNOPT()

        print("start GB optimization")
        solution = optimize(optProb, sens="FD")
        print("******************************************")
        print("finished optimization")

        opt_DVs = solution.getDVs()
        opt_electrolyzer = opt_DVs["electrolyzer_size_mw"]
        opt_wind = opt_DVs["wind_capacity_mw"]
        opt_solar = opt_DVs["solar_capacity_mw"]
        opt_battery_mwh = opt_DVs["battery_storage_mwh"]
        opt_battery_mw = opt_DVs["battery_storage_mw"]