Exemple #1
0
model.obj = Objective(expr=(((model.p - model.x)**2) +
                            ((model.q - model.y)**2))**0.5)

# Constraints
model.KeineAhnung = Constraint(expr=((model.x / model.length)**2) +
                               ((model.y / model.width)**2) - 1 >= 0)

model.pprint()

model.skip_canonical_repn = True  # for nonlinear models

instance = model.create()

SolverName = "asl"
so = Bunch()
so.solver = "ipopt"
opt = SolverFactory(SolverName, options=so)

if opt is None:
    print("Could not construct solver %s : %s" % (SolverName, so.solver))
    sys.exit(1)

results = opt.solve(instance)
results.write()
instance.load(results)  # put results in model

# because we know there is a variable named x
x_var = getattr(instance, "x")
x_val = x_var()

print("x was " + str(x_val))
Exemple #2
0
# Mimic the pyomo script
from pyomo.core import *
from pyomo.common.collections import Bunch

# set high level options that mimic pyomo comand line
options = Bunch()
options.model_file = 'DiseaseEstimation.py'
options.data_files = ['DiseaseEstimation.dat']
options.solver = 'ipopt'
options.solver_io = 'nl'
#options.keepfiles = True
#options.tee = True

# mimic the set of function calls done by pyomo command line
scripting.util.setup_environment(options)

# the following imports the model found in options.model_file,
# sets this to options.usermodel, and executes preprocessors
scripting.util.apply_preprocessing(options, parser=None)

# create the wrapper for the model, the data, the instance, and the options
model_data = scripting.util.create_model(options)
instance = model_data.instance

# solve
results, opt = scripting.util.apply_optimizer(options, instance)

# the following simply outputs the final time elapsed
scripting.util.finalize(options)

# load results into instance and print