Esempio n. 1
0
    def test_opt(self):
        # Optimization Object
        optProb = Optimization("Paraboloid", objfunc)

        # Design Variables
        optProb.addVarGroup("x", 1, varType="c", lower=-50.0, upper=50.0, value=0.0)
        optProb.addVarGroup("y", 1, varType="c", lower=-50.0, upper=50.0, value=0.0)

        # Objective
        optProb.addObj("obj")

        # Equality Constraint
        optProb.addConGroup("con", 1, lower=-15.0, upper=-15.0, wrt=["x", "y"], linear=True, jac=con_jac)

        # Check optimization problem:
        print(optProb)
        test_name = "bugfix_SNOPT_test_opt"
        optOptions = {
            "Major feasibility tolerance": 1e-1,
            "Print file": "{}.out".format(test_name),
            "Summary file": "{}_summary.out".format(test_name),
        }

        # Optimizer
        try:
            opt = SNOPT(options=optOptions)
        except Error:
            raise unittest.SkipTest("Optimizer not available: SNOPT")

        sol = opt(optProb, sens=sens)

        # Check Solution 7.166667, -7.833334
        tol = 1e-6
        assert_allclose(sol.variables["x"][0].value, 7.166667, atol=tol, rtol=tol)
        assert_allclose(sol.variables["y"][0].value, -7.833333, atol=tol, rtol=tol)
Esempio n. 2
0
    def test_opt_bug1(self):
        # Due to a new feature, there is a TypeError when you optimize a model without a constraint.
        optProb = Optimization("Paraboloid", objfunc_no_con)

        # Design Variables
        optProb.addVarGroup("x", 1, varType="c", lower=-50.0, upper=50.0, value=0.0)
        optProb.addVarGroup("y", 1, varType="c", lower=-50.0, upper=50.0, value=0.0)

        # Objective
        optProb.addObj("obj")

        test_name = "bugfix_SNOPT_bug1"
        optOptions = {
            "Major feasibility tolerance": 1e-1,
            "Print file": f"{test_name}.out",
            "Summary file": f"{test_name}_summary.out",
        }

        # Optimizer
        try:
            opt = SNOPT(options=optOptions)
        except Error as e:
            if "There was an error importing" in e.message:
                raise unittest.SkipTest("Optimizer not available: SNOPT")
            raise e

        opt(optProb, sens=sens)
Esempio n. 3
0
    def test_opt(self):
        # Optimization Object
        optProb = Optimization('Paraboloid', objfunc)

        # Design Variables
        optProb.addVarGroup('x', 1, type='c', lower=-50.0, upper=50.0, value=0.0)
        optProb.addVarGroup('y', 1, type='c', lower=-50.0, upper=50.0, value=0.0)
        optProb.finalizeDesignVariables()

        # Objective
        optProb.addObj('obj')

        # Equality Constraint
        optProb.addConGroup('con', 1, lower=-15.0, upper=-15.0, wrt=['x', 'y'], linear=True, jac=con_jac)

        # Check optimization problem:
        print(optProb)


        # Optimizer
        try:
            opt = SNOPT(optOptions = {'Major feasibility tolerance' : 1e-1})
        except:
            raise unittest.SkipTest('Optimizer not available: SNOPT')

        sol = opt(optProb, sens=sens)

        # Check Solution 7.166667, -7.833334
        self.assertAlmostEqual(sol.variables['x'][0].value, 7.166667, places=6)
        self.assertAlmostEqual(sol.variables['y'][0].value, -7.833333, places=6)
    def test_sens(self):
        termcomp = TerminateComp(max_sens=3)
        optProb = Optimization("Paraboloid", termcomp.objfunc)

        optProb.addVarGroup("x", 1, type="c", lower=-50.0, upper=50.0, value=0.0)
        optProb.addVarGroup("y", 1, type="c", lower=-50.0, upper=50.0, value=0.0)
        optProb.finalizeDesignVariables()

        optProb.addObj("obj")

        optProb.addConGroup("con", 1, lower=-15.0, upper=-15.0, wrt=["x", "y"], linear=True, jac=con_jac)

        test_name = "SNOPT_user_termination_sens"
        optOptions = {
            "Print file": "{}.out".format(test_name),
            "Summary file": "{}_summary.out".format(test_name),
        }
        try:
            opt = SNOPT(options=optOptions)
        except Error:
            raise unittest.SkipTest("Optimizer not available: SNOPT")

        sol = opt(optProb, sens=termcomp.sens)

        self.assertEqual(termcomp.sens_count, 4)

        # Exit code for user requested termination.
        self.assertEqual(sol.optInform["value"], 71)
    def test_obj(self):
        termcomp = TerminateComp(max_obj=2)
        optProb = Optimization("Paraboloid", termcomp.objfunc)

        optProb.addVarGroup("x",
                            1,
                            varType="c",
                            lower=-50.0,
                            upper=50.0,
                            value=0.0)
        optProb.addVarGroup("y",
                            1,
                            varType="c",
                            lower=-50.0,
                            upper=50.0,
                            value=0.0)

        optProb.addObj("obj")

        optProb.addConGroup("con",
                            1,
                            lower=-15.0,
                            upper=-15.0,
                            wrt=["x", "y"],
                            linear=True,
                            jac=con_jac)

        test_name = "SNOPT_user_termination_obj"
        optOptions = {
            "Print file": f"{test_name}.out",
            "Summary file": f"{test_name}_summary.out",
        }
        try:
            opt = SNOPT(options=optOptions)
        except Error as e:
            if "There was an error importing" in e.message:
                raise unittest.SkipTest("Optimizer not available: SNOPT")
            raise e

        sol = opt(optProb, sens=termcomp.sens)

        self.assertEqual(termcomp.obj_count, 3)

        # Exit code for user requested termination.
        self.assertEqual(sol.optInform["value"], 71)
Esempio n. 6
0
    def test_opt_bug_print_2con(self):
        # Optimization Object
        optProb = Optimization("Paraboloid", objfunc_2con)

        # Design Variables
        optProb.addVarGroup("x", 1, varType="c", lower=-50.0, upper=50.0, value=0.0)
        optProb.addVarGroup("y", 1, varType="c", lower=-50.0, upper=50.0, value=0.0)

        # Objective
        optProb.addObj("obj")

        con_jac2 = {}
        con_jac2["x"] = -np.ones((2, 1))
        con_jac2["y"] = np.ones((2, 1))

        con_jac3 = {}
        con_jac3["x"] = -np.ones((3, 1))
        con_jac3["y"] = np.ones((3, 1))

        # Equality Constraint
        optProb.addConGroup("con", 2, lower=-15.0, upper=-15.0, wrt=["x", "y"], linear=True, jac=con_jac2)
        optProb.addConGroup("con2", 3, lower=-15.0, upper=-15.0, wrt=["x", "y"], linear=True, jac=con_jac3)

        # Check optimization problem:
        print(optProb)

        test_name = "bugfix_SNOPT_bug_print_2con"
        optOptions = {
            "Major feasibility tolerance": 1e-1,
            "Print file": f"{test_name}.out",
            "Summary file": f"{test_name}_summary.out",
        }

        # Optimizer
        try:
            opt = SNOPT(options=optOptions)
        except Error as e:
            if "There was an error importing" in e.message:
                raise unittest.SkipTest("Optimizer not available: SNOPT")
            raise e

        sol = opt(optProb, sens=sens)

        print(sol)
Esempio n. 7
0
    def test_opt_bug1(self):
        # Due to a new feature, there is a TypeError when you optimize a model without a constraint.
        optProb = Optimization('Paraboloid', objfunc_no_con)

        # Design Variables
        optProb.addVarGroup('x', 1, type='c', lower=-50.0, upper=50.0, value=0.0)
        optProb.addVarGroup('y', 1, type='c', lower=-50.0, upper=50.0, value=0.0)
        optProb.finalizeDesignVariables()

        # Objective
        optProb.addObj('obj')

        # Optimizer
        try:
            opt = SNOPT(optOptions = {'Major feasibility tolerance' : 1e-1})
        except:
            raise unittest.SkipTest('Optimizer not available: SNOPT')

        sol = opt(optProb, sens=sens)
Esempio n. 8
0
    def test_obj(self):
        termcomp = TerminateComp(max_obj=2)
        optProb = Optimization('Paraboloid', termcomp.objfunc)

        optProb.addVarGroup('x',
                            1,
                            type='c',
                            lower=-50.0,
                            upper=50.0,
                            value=0.0)
        optProb.addVarGroup('y',
                            1,
                            type='c',
                            lower=-50.0,
                            upper=50.0,
                            value=0.0)
        optProb.finalizeDesignVariables()

        optProb.addObj('obj')

        optProb.addConGroup('con',
                            1,
                            lower=-15.0,
                            upper=-15.0,
                            wrt=['x', 'y'],
                            linear=True,
                            jac=con_jac)

        try:
            opt = SNOPT()
        except:
            raise unittest.SkipTest('Optimizer not available: SNOPT')

        sol = opt(optProb, sens=termcomp.sens)

        self.assertEqual(termcomp.obj_count, 3)

        # Exit code for user requested termination.
        self.assertEqual(sol.optInform['value'][0], 71)
Esempio n. 9
0
    def test_opt_bug_print_2con(self):
        # Optimization Object
        optProb = Optimization('Paraboloid', objfunc_2con)

        # Design Variables
        optProb.addVarGroup('x', 1, type='c', lower=-50.0, upper=50.0, value=0.0)
        optProb.addVarGroup('y', 1, type='c', lower=-50.0, upper=50.0, value=0.0)
        optProb.finalizeDesignVariables()

        # Objective
        optProb.addObj('obj')

        con_jac2 = {}
        con_jac2['x'] = -np.ones((2, 1))
        con_jac2['y'] = np.ones((2, 1))

        con_jac3 = {}
        con_jac3['x'] = -np.ones((3, 1))
        con_jac3['y'] = np.ones((3, 1))

        # Equality Constraint
        optProb.addConGroup('con', 2, lower=-15.0, upper=-15.0, wrt=['x', 'y'], linear=True, jac=con_jac2)
        optProb.addConGroup('con2', 3, lower=-15.0, upper=-15.0, wrt=['x', 'y'], linear=True, jac=con_jac3)

        # Check optimization problem:
        print(optProb)

        # Optimizer
        try:
            opt = SNOPT(optOptions = {'Major feasibility tolerance' : 1e-1})
        except:
            raise unittest.SkipTest('Optimizer not available: SNOPT')

        sol = opt(optProb, sens=sens)

        print(sol)
Esempio n. 10
0
    if optimize == True:
        # optimization setup
        optProb = Optimization('VAWT_Power', obj_func)
        optProb.addObj('obj')

        n = np.size(x0)
        optProb.addVarGroup('xvars', n, 'c', lower=xlow, upper=xupp, value=x0)
        optProb.addVarGroup('yvars', n, 'c', lower=ylow, upper=yupp, value=y0)

        num_cons_sep = (n - 1) * n / 2
        optProb.addConGroup('sep', num_cons_sep, lower=0, upper=None)
        num_cons_obs = nobs * nwind
        optProb.addConGroup('SPL', num_cons_obs, lower=0, upper=SPLlim / 10.)

        opt = SNOPT()
        opt.setOption('Scale option', 0)
        if rotdir_spec == 'cn':
            opt.setOption(
                'Print file',
                basepath + path.sep + 'optimization_results/SNOPT_print_SPL' +
                str(SPLlim) + '_turb' + str(n) + '_counterrot.out')
            opt.setOption(
                'Summary file', basepath + path.sep +
                'optimization_results/SNOPT_summary_SPL' + str(SPLlim) +
                '_turb' + str(n) + '_counterrot.out')
        elif rotdir_spec == 'co':
            opt.setOption(
                'Print file',
                basepath + path.sep + 'optimization_results/SNOPT_print_SPL' +
                str(SPLlim) + '_turb' + str(n) + '_corot.out')
Esempio n. 11
0
    jdist = cp.MvNormal(initial_seed, std_dev)
    collocation = StochasticCollocation(3, "Normal")
    collocation_grad = StochasticCollocation(3,
                                             "Normal",
                                             QoI_dimensions=rv_systemsize)
    threshold_factor = 0.9
    dominant_space = DimensionReduction(threshold_factor,
                                        n_arnoldi_sample=3,
                                        exact_Hessian=False)
    dominant_space.getDominantDirections(QoI, jdist)

    # Setup the problem
    optProb = Optimization('Paraboloid', objfunc)
    lower_bound = -20 * np.ones(rv_systemsize)
    upper_bound = 20 * np.ones(rv_systemsize)
    optProb.addVarGroup('xvars',
                        rv_systemsize,
                        'c',
                        lower=lower_bound,
                        upper=upper_bound,
                        value=10 * np.ones(rv_systemsize))
    optProb.addObj('obj')
    # Optimizer
    opt = SNOPT(optOptions={'Major feasibility tolerance': 1e-6})
    sol = opt(optProb, sens=sens)

    # Check Solution
    import inspect
    print(sol.fStar)
    print(sol.getDVs()['xvars'])
Esempio n. 12
0
optProb.addConGroup("con", len(lower), lower=lower, upper=upper)

# And the 2 linear constriants
if USE_LINEAR:
    jac = np.zeros((1, 9))
    jac[0, 3] = 1.0
    jac[0, 2] = -1.0
    optProb.addConGroup("lin_con", 1, lower=-0.55, upper=0.55, wrt=["xvars"], jac={"xvars": jac}, linear=True)

# Objective
optProb.addObj("obj")

# Check optimization problem:
print(optProb)
optProb.printSparsity()

# Global Optimizer: ALPSO
opt1 = ALPSO()

# Get first Solution
sol1 = opt1(optProb)
print(sol1)

# Now run the previous solution with SNOPT
opt2 = SNOPT()
sol2 = opt2(sol1)

# Check Solution
print(sol2)
Esempio n. 13
0
    omega_r = 1500  # rpm
    R_rotor = 0.705  # m
    fuel_cap = 1.0  # kg
    theta_hover = 20  # deg
    x0 = scale(
        [eng_displace, fuel_rate, omega_r, R_rotor, fuel_cap, theta_hover])
    # x0 = [ 4.3441622,   0.16620041,  0.10885692,  2.78460696,  6.76453586, 0.85782237]
    x0 = [4.61280557, 0.17376677, 0.08200489, 1.5, 7.32432873, 0.86800068]

    lb = [0, 0, 0, 0, 0, 0]
    ub = scale([1200.0, 1.0, 7500.0, 3.0, 12.0, 30.0])
    setPayload(5.0)

    #    print obj_func_print(x0)

    optimizer = SNOPT()
    # optimizer = NSGA2()
    # optimizer.setOption('maxGen', 200)

    xopt, fopt, info = optimize(obj_func, x0, lb, ub, optimizer)
    print 'SNOPT:', xopt, fopt, info

    out = obj_func_print(xopt)

    # Create the pareto frunt

    steps = 20
    payloads = np.linspace(0.0, 10.0, num=steps)
    flight_times = np.zeros(steps)
    engine_size = np.zeros(steps)
    for i in range(steps):
Esempio n. 14
0
    def optimize(self,
                 sparse=True,
                 tol=None,
                 optOptions={},
                 storeHistory=False):
        # set N
        if sparse:
            self.N = 50000
        else:
            self.N = 500
        # Optimization Object
        optProb = Optimization("large and sparse", self.objfunc)

        # Design Variables
        optProb.addVar("x", lower=-100, upper=150, value=0)
        optProb.addVarGroup("y",
                            self.N,
                            lower=-10 - np.arange(self.N),
                            upper=np.arange(self.N),
                            value=0)
        optProb.addVarGroup("z",
                            2 * self.N,
                            upper=np.arange(2 * self.N),
                            lower=-100 - np.arange(2 * self.N),
                            value=0)

        # Constraints
        optProb.addCon("con1", upper=100, wrt=["x"])
        optProb.addCon("con2", upper=100)
        optProb.addCon("con3", lower=4, wrt=["x", "z"])
        xJac = np.ones((self.N, 1))
        if sparse:
            yJac = scipy.sparse.spdiags(np.ones(self.N), 0, self.N, self.N)
        else:
            yJac = np.eye(self.N)
        optProb.addConGroup(
            "lincon",
            self.N,
            lower=2 - 3 * np.arange(self.N),
            linear=True,
            wrt=["x", "y"],
            jac={
                "x": xJac,
                "y": yJac
            },
        )
        optProb.addObj("obj")

        # Optimizer
        try:
            opt = SNOPT(options=optOptions)
        except Error:
            raise unittest.SkipTest("Optimizer not available: SNOPT")

        sol = opt(optProb, sens=self.sens)

        # Check Solution
        if tol is not None:
            if opt.version != "7.7.7":
                assert_allclose(sol.objectives["obj"].value,
                                10.0,
                                atol=tol,
                                rtol=tol)
            else:
                assert_allclose(sol.fStar, 10.0, atol=tol, rtol=tol)
            assert_allclose(sol.variables["x"][0].value,
                            2.0,
                            atol=tol,
                            rtol=tol)
        return sol
Esempio n. 15
0
# Optimization Object
optProb = Optimization("Paraboloid", objfunc)

# Design Variables
optProb.addVarGroup("x", 1, type="c", lower=-50.0, upper=50.0, value=0.0)
optProb.addVarGroup("y", 1, type="c", lower=-50.0, upper=50.0, value=0.0)
optProb.finalizeDesignVariables()

# Objective
optProb.addObj("obj")

# Equality Constraint
optProb.addConGroup("con",
                    1,
                    lower=-15.0,
                    upper=-15.0,
                    wrt=["x", "y"],
                    linear=True,
                    jac=con_jac)

# Check optimization problem:
print(optProb)

# Optimizer
opt = SNOPT(optOptions={"Major feasibility tolerance": 1e-1})
sol = opt(optProb, sens=sens)

# Check Solution
print(sol)
print("Solution shoud be (x, y) = (7.166667, -7.833334)\n")
Esempio n. 16
0
        optProb.addVarGroup('me',
                            2,
                            lower=np.array([-1.0, 0.0]),
                            upper=np.array([0.0, 0.9]),
                            value=np.array([-0.5, 0.3]))  # , scalar=1E-1)
        optProb.addVarGroup('MU',
                            2,
                            lower=np.array([0.0, 1.5]),
                            upper=np.array([1.0, 20.0]),
                            value=np.array([0.5, 5.5]))  # , scalar=1E-1)
        optProb.addVarGroup('aU', 1, lower=0.0, upper=20.0,
                            value=5.0)  # , scalar=1E-1)
        optProb.addVarGroup('bU', 1, lower=0.0, upper=5.0,
                            value=1.66)  # , scalar=1E-1)
        optProb.addVarGroup('cos_spread', 1, lower=0.0, upper=10.0,
                            value=2.0)  # , scalar=1E-1)

    # add objective
    optProb.addObj('obj', scale=1E0)

    # initialize optimizer
    snopt = SNOPT(options={'Print file': 'SNOPT_print_tune_all.out'})

    # run optimizer
    sol = snopt(optProb, sens=None)

    # print solution
    print sol

    # plot fit
    tuning_obj_function(xdict=sol.xStar, plot=True)