コード例 #1
0
    opt_prob = Optimization(args.logfile, optproblem.evalObjCon)

    # Add functions
    opt_prob.addObj('weight')
    opt_prob.addCon('buckling-bar1', type='i')
    opt_prob.addCon('failure-bar1', type='i')
    opt_prob.addCon('failure-bar2', type='i')

    # Add variables
    opt_prob.addVar('area-1', type='c', value=1.0, lower=1.0e-3, upper=2.0)
    opt_prob.addVar('area-2', type='c', value=1.0, lower=1.0e-3, upper=2.0)
    opt_prob.addVar('height', type='c', value=4.0, lower=4.0, upper=10.0)

    # Optimization algorithm
    if args.algorithm == 'ALGENCAN':
        opt = ALGENCAN()
        opt.setOption('iprint', 2)
        opt.setOption('epsfeas', 1e-6)
        opt.setOption('epsopt', 1e-6)
    else:
        opt = SLSQP(pll_type='POA')
        opt.setOption('MAXIT', 999)

    opt(opt_prob,
        sens_type=optproblem.evalObjConGradient,
        disp_opts=True,
        store_hst=True,
        hot_start=False)

    if optproblem.comm.Get_rank() == 0:
        print opt_prob.solution(0)
コード例 #2
0
        jac = np.zeros([self.m, self.n])
        jac[0][0] = 0.
        jac[0][1] = 2.
        jac[1][0] = 3.
        jac[1][1] = 0.

        return jac


# Translate this NLPy problem in a pyOpt problem
nlp = example()
opt_prob = PyOpt_From_NLPModel(nlp)
print opt_prob

# Instantiate Optimizer (ALGENCAN) & Solve Problem
algencan = ALGENCAN()
algencan.setOption('iprint', 0)
algencan(opt_prob)
print opt_prob.solution(0)


# Call the imported solver SNOPT
snopt = SNOPT()

# Choose sensitivity type for computing gradients with :
#  opt_prob.grad_func : NLP.py is computing the gradients through the grad_func
#                       function
#  'FD' : finite difference
#  'CS' : complex step
SensType = opt_prob.grad_func
コード例 #3
0
ファイル: pyoptex.py プロジェクト: NathanWycoff/PrototypeSNN
# Instantiate Optimizer (SOLVOPT) & Solve Problem
solvopt = SOLVOPT()
solvopt.setOption('iprint', -1)
solvopt(opt_prob, sens_type='FD')
print(opt_prob.solution(4))

# Instantiate Optimizer (KSOPT) & Solve Problem
ksopt = KSOPT()
ksopt.setOption('IPRINT', 0)
ksopt(opt_prob, sens_type='FD')
print(opt_prob.solution(5))

# Instantiate Optimizer (NSGA2) & Solve Problem
nsga2 = NSGA2()
nsga2.setOption('PrintOut', 0)
nsga2(opt_prob)
print(opt_prob.solution(6))

# Instantiate Optimizer (ALGENCAN) & Solve Problem
algencan = ALGENCAN()
algencan.setOption('iprint', 0)
algencan(opt_prob)
print(opt_prob.solution(7))

# Instantiate Optimizer (FILTERSD) & Solve Problem
filtersd = FILTERSD()
filtersd.setOption('iprint', 0)
filtersd(opt_prob)
print(opt_prob.solution(8))
コード例 #4
0
def optimize(k, w1, w2):
    
    # Physical problem
    rho = 0.2836  # lb/in^3
    L   = 5.0     # in
    P   = 25000.0 # lb
    E   = 30.0e6  # psi
    ys  = 36260.0 # psi
    fs  = 1.5
    dtruss = TwoBarTruss(rho, L, P, E, ys, fs)
    struss = StochasticTwoBarTruss(dtruss)

    # Optimization Problem
    optproblem = TwoBarTrussOpt(MPI.COMM_WORLD, struss, k, w1, w2)
    opt_prob = Optimization(args.logfile, optproblem.evalObjCon)
    
    # Add functions
    opt_prob.addObj('weight')
    opt_prob.addCon('buckling-bar1', type='i')
    opt_prob.addCon('failure-bar1' , type='i')
    opt_prob.addCon('failure-bar2' , type='i')
    
    # Add variables
    opt_prob.addVar('area-1', type='c', value= 1.5, lower= 1.5, upper= 1.5)
    opt_prob.addVar('area-2', type='c', value= 1.5, lower= 1.5, upper= 1.5)
    opt_prob.addVar('height', type='c', value= 4.0, lower= 4.0, upper= 10.0)
    
    # Optimization algorithm
    if args.algorithm == 'ALGENCAN':
        opt = ALGENCAN()
        opt.setOption('iprint',2)
        opt.setOption('epsfeas',1e-6)
        opt.setOption('epsopt',1e-6)
    else:
        opt = SLSQP(pll_type='POA')
        opt.setOption('MAXIT',999)
    
    opt(opt_prob,
        sens_type=optproblem.evalObjConGradient,
        disp_opts=True,
        store_hst=True,
        hot_start=False)
    
    if optproblem.comm.Get_rank() ==0:   
        print opt_prob.solution(0)
        opt_prob.write2file(disp_sols=True)
        x = optproblem.x_hist[-1]
        f = optproblem.fvals[0]
        print 'x', x
        print 'f', f

    return x, f