Skip to content

Commit

Permalink
replace polynomial_degree condition
Browse files Browse the repository at this point in the history
  • Loading branch information
ZedongPeng committed Oct 29, 2020
1 parent 6e6d1ce commit 0721373
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 44 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ gurobi.log

# Jupyterhub/Jupyterlab checkpoints
.ipynb_checkpoints
cplex.log
7 changes: 5 additions & 2 deletions pyomo/contrib/gdpopt/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,14 +290,17 @@ def build_ordered_component_lists(model, solve_data):
model.component_data_objects(
ctype=Constraint, active=True,
descend_into=(Block, Disjunct))))
# print(util_blk.constraint_list)
setattr(
util_blk, 'linear_constraint_list', list(c for c in model.component_data_objects(
ctype=Constraint, active=True, descend_into=(Block, Disjunct))
if c.body.polynomial_degree in (0, 1)))
if c.body.polynomial_degree() in (0, 1)))
# print(util_blk.linear_constraint_list)
setattr(
util_blk, 'nonlinear_constraint_list', list(c for c in model.component_data_objects(
ctype=Constraint, active=True, descend_into=(Block, Disjunct))
if c.body.polynomial_degree not in (0, 1)))
if c.body.polynomial_degree() not in (0, 1)))
# print(util_blk.nonlinear_constraint_list)
setattr(
util_blk, 'disjunct_list', list(
model.component_data_objects(
Expand Down
3 changes: 1 addition & 2 deletions pyomo/contrib/mindtpy/MindtPy.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,7 @@ def solve(self, model, **kwds):
lin.mip_iters = Set(dimen=1)

if config.feasibility_norm == 'L1' or config.feasibility_norm == 'L2':
feas.nl_constraint_set = Set(initialize=[i for i, constr in enumerate(MindtPy.constraint_list, 1) if
constr.body.polynomial_degree() not in (1, 0)],
feas.nl_constraint_set = Set(initialize=[i for i, constr in enumerate(MindtPy.nonlinear_constraint_list, 1)],
doc="Integer index set over the nonlinear constraints."
"The set corresponds to the index of nonlinear constraint in constraint_set")
# Create slack variables for feasibility problem
Expand Down
11 changes: 2 additions & 9 deletions pyomo/contrib/mindtpy/cut_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,7 @@ def add_ecp_cuts(target_model, solve_data, config,
linearized constraint has been violated
"""
with time_code(solve_data.timing, 'ECP cut generation'):
for constr in target_model.MindtPy_utils.constraint_list:

if constr.body.polynomial_degree() in (0, 1):
continue

for constr in target_model.MindtPy_utils.nonlinear_constraint_list:
constr_vars = list(identify_variables(constr.body))
jacs = solve_data.jacobians

Expand Down Expand Up @@ -236,10 +232,7 @@ def add_affine_cuts(solve_data, config):
config.logger.info("Adding affine cuts")
counter = 0

for constr in m.MindtPy_utils.constraint_list:
if constr.body.polynomial_degree() in (1, 0):
continue

for constr in m.MindtPy_utils.nonlinear_constraint_list:
vars_in_constr = list(
identify_variables(constr.body))
if any(var.value is None for var in vars_in_constr):
Expand Down
9 changes: 4 additions & 5 deletions pyomo/contrib/mindtpy/initialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,8 @@ def init_max_binaries(solve_data, config):
config.logger.info(
"MILP %s: maximize value of binaries" %
(solve_data.mip_iter))
for c in MindtPy.constraint_list:
if c.body.polynomial_degree() not in (1, 0):
c.deactivate()
for c in MindtPy.nonlinear_constraint_list:
c.deactivate()
objective = next(m.component_data_objects(Objective, active=True))
objective.deactivate()
binary_vars = (
Expand Down Expand Up @@ -238,11 +237,11 @@ def init_max_binaries(solve_data, config):
'MILP master problem is infeasible. '
'Problem may have no more feasible '
'binary configurations.')
elif subprob_terminate_cond is tc.maxTimeLimit:
elif solve_terminate_cond is tc.maxTimeLimit:
config.logger.info(
'NLP subproblem failed to converge within time limit.')
solve_data.results.solver.termination_condition = tc.maxTimeLimit
elif subprob_terminate_cond is tc.maxIterations:
elif solve_terminate_cond is tc.maxIterations:
config.logger.info(
'NLP subproblem failed to converge within iteration limit.')
else:
Expand Down
3 changes: 1 addition & 2 deletions pyomo/contrib/mindtpy/iterate.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,7 @@ def algorithm_should_terminate(solve_data, config, check_cycling):
if config.strategy == 'ECP':
# check to see if the nonlinear constraints are satisfied
MindtPy = solve_data.working_model.MindtPy_utils
nonlinear_constraints = [c for c in MindtPy.constraint_list if
c.body.polynomial_degree() not in (1, 0)]
nonlinear_constraints = [c for c in MindtPy.nonlinear_constraint_list]
for nlc in nonlinear_constraints:
if nlc.has_lb():
try:
Expand Down
5 changes: 2 additions & 3 deletions pyomo/contrib/mindtpy/mip_solve.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ def solve_master(solve_data, config, feas_pump=False):
MindtPy = solve_data.mip.MindtPy_utils

# Set up MILP
for c in MindtPy.constraint_list:
if c.body.polynomial_degree() not in (1, 0):
c.deactivate()
for c in MindtPy.nonlinear_constraint_list:
c.deactivate()

MindtPy.MindtPy_linear_cuts.activate()
main_objective = next(
Expand Down
4 changes: 1 addition & 3 deletions pyomo/contrib/mindtpy/single_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,7 @@ def add_lazy_affine_cuts(self, solve_data, config, opt):
config.logger.info("Adding affine cuts")
counter = 0

for constr in m.MindtPy_utils.constraint_list:
if constr.body.polynomial_degree() in (1, 0):
continue
for constr in m.MindtPy_utils.nonlinear_constraint_list:

vars_in_constr = list(
identify_variables(constr.body))
Expand Down
33 changes: 15 additions & 18 deletions pyomo/contrib/mindtpy/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ def calc_jacobians(solve_data, config):
mode = differentiate.Modes.reverse_symbolic
elif config.differentiate_mode == "sympy":
mode = differentiate.Modes.sympy
for c in solve_data.mip.MindtPy_utils.constraint_list:
if c.body.polynomial_degree() in (1, 0):
continue # skip linear constraints
for c in solve_data.mip.MindtPy_utils.nonlinear_constraint_list:
vars_in_constr = list(EXPR.identify_variables(c.body))
jac_list = differentiate(
c.body, wrt_list=vars_in_constr, mode=mode)
Expand Down Expand Up @@ -160,21 +158,20 @@ def var_bound_add(solve_data, config):
"""
m = solve_data.working_model
MindtPy = m.MindtPy_utils
for c in MindtPy.constraint_list:
if c.body.polynomial_degree() not in (1, 0):
for var in list(EXPR.identify_variables(c.body)):
if var.has_lb() and var.has_ub():
continue
elif not var.has_lb():
if var.is_integer():
var.setlb(-config.integer_var_bound - 1)
else:
var.setlb(-config.continuous_var_bound - 1)
elif not var.has_ub():
if var.is_integer():
var.setub(config.integer_var_bound)
else:
var.setub(config.continuous_var_bound)
for c in MindtPy.nonlinear_constraint_list:
for var in list(EXPR.identify_variables(c.body)):
if var.has_lb() and var.has_ub():
continue
elif not var.has_lb():
if var.is_integer():
var.setlb(-config.integer_var_bound - 1)
else:
var.setlb(-config.continuous_var_bound - 1)
elif not var.has_ub():
if var.is_integer():
var.setub(config.integer_var_bound)
else:
var.setub(config.continuous_var_bound)


def generate_norm2sq_objective_function(model, setpoint_model, discrete_only=False):
Expand Down

0 comments on commit 0721373

Please sign in to comment.