From 49b20dcf411f428403f45ac3f304c48aa848edf9 Mon Sep 17 00:00:00 2001 From: John Siirola Date: Tue, 19 Jan 2021 11:38:55 -0700 Subject: [PATCH] Parse cplex tags; fixes #1766 --- pyomo/core/kernel/block.py | 4 +++- pyomo/solvers/plugins/solvers/CPLEX.py | 14 +++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/pyomo/core/kernel/block.py b/pyomo/core/kernel/block.py index 18190888e10..0be0ef630ad 100644 --- a/pyomo/core/kernel/block.py +++ b/pyomo/core/kernel/block.py @@ -458,7 +458,9 @@ def load_solution(self, raise KeyError("Objective associated with symbol '%s' " "is not found on this block" % (label)) - unseen_var_ids.remove(id(obj)) + # Because of __default_objective__, an objective might + # appear twice in the objective dictionary. + unseen_var_ids.discard(id(obj)) for _attr_key, attr_value in six.iteritems(entry): attr_key = _attr_key[0].lower() + _attr_key[1:] if attr_key in valid_import_suffixes: diff --git a/pyomo/solvers/plugins/solvers/CPLEX.py b/pyomo/solvers/plugins/solvers/CPLEX.py index a599ae20657..f514d40edf1 100644 --- a/pyomo/solvers/plugins/solvers/CPLEX.py +++ b/pyomo/solvers/plugins/solvers/CPLEX.py @@ -776,9 +776,21 @@ def process_soln_file(self,results): break tINPUT.close() - elif tokens[0].startswith("objectiveValue"): + elif tokens[0].startswith("objectiveValue") and tokens[0] != 'objectiveValues': + # prior to 12.10.0, the objective value came back as an + # attribute on the
tag objective_value = (tokens[0].split('=')[1].strip()).lstrip("\"").rstrip("\"") soln.objective['__default_objective__']['Value'] = float(objective_value) + + elif tokens[0] == "objective": + # beginning in 12.10.0, CPLEX supports multiple + # objectives in an tag + fields = {} + for field in tokens[1:]: + k,v = field.split('=') + fields[k] = v.strip('"') + soln.objective.setdefault(fields['name'], {})['Value'] = float(fields['value']) + elif tokens[0].startswith("solutionStatusValue"): pieces = tokens[0].split("=") solution_status = eval(pieces[1])