Skip to content

Commit

Permalink
Apply black
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmundt committed Jan 18, 2024
1 parent 7fa02de commit efb1eee
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 25 deletions.
13 changes: 5 additions & 8 deletions pyomo/contrib/solver/ipopt.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ def __init__(


class ipoptSolutionLoader(SolSolutionLoader):
def get_reduced_costs(self, vars_to_load: Sequence[_GeneralVarData] | None = None) -> Mapping[_GeneralVarData, float]:
def get_reduced_costs(
self, vars_to_load: Sequence[_GeneralVarData] | None = None
) -> Mapping[_GeneralVarData, float]:
if self._nl_info.scaling is None:
scale_list = [1] * len(self._nl_info.variables)
else:
Expand Down Expand Up @@ -348,11 +350,7 @@ def solve(self, model, **kwds):
if config.tee:
ostreams.append(sys.stdout)
if config.log_solver_output:
ostreams.append(
LogStream(
level=logging.INFO, logger=logger
)
)
ostreams.append(LogStream(level=logging.INFO, logger=logger))
with TeeStream(*ostreams) as t:
timer.start('subprocess')
process = subprocess.run(
Expand Down Expand Up @@ -484,8 +482,7 @@ def _parse_solution(
res.solution_loader = SolutionLoader(None, None, None)
else:
res.solution_loader = ipoptSolutionLoader(
sol_data=sol_data,
nl_info=nl_info,
sol_data=sol_data, nl_info=nl_info
)

return res
8 changes: 4 additions & 4 deletions pyomo/contrib/solver/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,7 @@ def __init__(
visibility=visibility,
)

self.solution_loader = self.declare(
'solution_loader', ConfigValue()
)
self.solution_loader = self.declare('solution_loader', ConfigValue())
self.termination_condition: TerminationCondition = self.declare(
'termination_condition',
ConfigValue(
Expand Down Expand Up @@ -244,7 +242,9 @@ def __init__(
ConfigValue(domain=str, default=None, visibility=ADVANCED_OPTION),
)

def display(self, content_filter=None, indent_spacing=2, ostream=None, visibility=0):
def display(
self, content_filter=None, indent_spacing=2, ostream=None, visibility=0
):
return super().display(content_filter, indent_spacing, ostream, visibility)


Expand Down
8 changes: 6 additions & 2 deletions pyomo/contrib/solver/sol_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,9 @@ def parse_sol_file(
for cnt in range(nvalues):
suf_line = sol_file.readline().split()
var_ndx = int(suf_line[0])
sol_data.var_suffixes[suffix_name][var_ndx] = convert_function(suf_line[1])
sol_data.var_suffixes[suffix_name][var_ndx] = convert_function(
suf_line[1]
)
elif kind == 1: # Con
sol_data.con_suffixes[suffix_name] = dict()
for cnt in range(nvalues):
Expand All @@ -181,7 +183,9 @@ def parse_sol_file(
for cnt in range(nvalues):
suf_line = sol_file.readline().split()
obj_ndx = int(suf_line[0])
sol_data.obj_suffixes[suffix_name][obj_ndx] = convert_function(suf_line[1])
sol_data.obj_suffixes[suffix_name][obj_ndx] = convert_function(
suf_line[1]
)
elif kind == 3: # Prob
sol_data.problem_suffixes[suffix_name] = list()
for cnt in range(nvalues):
Expand Down
30 changes: 21 additions & 9 deletions pyomo/contrib/solver/solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def load_vars(
----------
vars_to_load: list
The minimum set of variables whose solution should be loaded. If vars_to_load is None, then the solution
to all primal variables will be loaded. Even if vars_to_load is specified, the values of other
to all primal variables will be loaded. Even if vars_to_load is specified, the values of other
variables may also be loaded depending on the interface.
"""
for v, val in self.get_primals(vars_to_load=vars_to_load).items():
Expand Down Expand Up @@ -187,21 +187,27 @@ def load_vars(
scale_list = [1] * len(self._nl_info.variables)
else:
scale_list = self._nl_info.scaling.variables
for v, val, scale in zip(self._nl_info.variables, self._sol_data.primals, scale_list):
v.set_value(val/scale, skip_validation=True)
for v, val, scale in zip(
self._nl_info.variables, self._sol_data.primals, scale_list
):
v.set_value(val / scale, skip_validation=True)

for v, v_expr in self._nl_info.eliminated_vars:
v.set_value(value(v_expr), skip_validation=True)

StaleFlagManager.mark_all_as_stale(delayed=True)

def get_primals(self, vars_to_load: Sequence[_GeneralVarData] | None = None) -> Mapping[_GeneralVarData, float]:
def get_primals(
self, vars_to_load: Sequence[_GeneralVarData] | None = None
) -> Mapping[_GeneralVarData, float]:
if self._nl_info.scaling is None:
scale_list = [1] * len(self._nl_info.variables)
else:
scale_list = self._nl_info.scaling.variables
val_map = dict()
for v, val, scale in zip(self._nl_info.variables, self._sol_data.primals, scale_list):
for v, val, scale in zip(
self._nl_info.variables, self._sol_data.primals, scale_list
):
val_map[id(v)] = val / scale

for v, v_expr in self._nl_info.eliminated_vars:
Expand All @@ -211,13 +217,17 @@ def get_primals(self, vars_to_load: Sequence[_GeneralVarData] | None = None) ->

res = ComponentMap()
if vars_to_load is None:
vars_to_load = self._nl_info.variables + [v for v, _ in self._nl_info.eliminated_vars]
vars_to_load = self._nl_info.variables + [
v for v, _ in self._nl_info.eliminated_vars
]
for v in vars_to_load:
res[v] = val_map[id(v)]

return res

def get_duals(self, cons_to_load: Sequence[_GeneralConstraintData] | None = None) -> Dict[_GeneralConstraintData, float]:

def get_duals(
self, cons_to_load: Sequence[_GeneralConstraintData] | None = None
) -> Dict[_GeneralConstraintData, float]:
if self._nl_info.scaling is None:
scale_list = [1] * len(self._nl_info.constraints)
else:
Expand All @@ -227,7 +237,9 @@ def get_duals(self, cons_to_load: Sequence[_GeneralConstraintData] | None = None
else:
cons_to_load = set(cons_to_load)
res = dict()
for c, val, scale in zip(self._nl_info.constraints, self._sol_data.duals, scale_list):
for c, val, scale in zip(
self._nl_info.constraints, self._sol_data.duals, scale_list
):
if c in cons_to_load:
res[c] = val * scale
return res
Expand Down
3 changes: 1 addition & 2 deletions pyomo/contrib/solver/tests/unit/test_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def test_declared_items(self):
'termination_condition',
'timing_info',
'solver_log',
'solver_configuration'
'solver_configuration',
}
actual_declared = res._declared
self.assertEqual(expected_declared, actual_declared)
Expand Down Expand Up @@ -166,4 +166,3 @@ def test_results(self):
rc2 = res.solution_loader.get_reduced_costs([m.y])
self.assertNotIn(m.x, rc2)
self.assertAlmostEqual(rc[id(m.y)][1], rc2[m.y])

0 comments on commit efb1eee

Please sign in to comment.