Revert "#71: Fixing statefulness of LpProblem to allow pickling and unpickling."

This reverts commit fdb518b76a.
This commit is contained in:
Ryan J. O'Neil 2015-12-29 08:32:27 -05:00
parent 07a2ea11af
commit 28cea532ba
1 changed files with 14 additions and 22 deletions

View File

@ -100,7 +100,6 @@ import itertools
from .constants import * from .constants import *
from .solvers import * from .solvers import *
from collections import Iterable from collections import Iterable
from operator import attrgetter
import logging import logging
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -1127,32 +1126,21 @@ class LpProblem(object):
self.lastUnused = 0 self.lastUnused = 0
def __repr__(self): def __repr__(self):
s = self.name+":\n" string = self.name+":\n"
if self.sense == 1: if self.sense == 1:
s += "MINIMIZE\n" string += "MINIMIZE\n"
else: else:
s += "MAXIMIZE\n" string += "MAXIMIZE\n"
s += repr(self.objective) +"\n" string += repr(self.objective) +"\n"
if self.constraints: if self.constraints:
s += "SUBJECT TO\n" string += "SUBJECT TO\n"
for n, c in self.constraints.items(): for n, c in self.constraints.items():
s += c.asCplexLpConstraint(n) +"\n" string += c.asCplexLpConstraint(n) +"\n"
s += "VARIABLES\n" string += "VARIABLES\n"
for v in self.variables(): for v in self.variables():
s += v.asCplexLpVariable() + " " + LpCategories[v.cat] + "\n" string += v.asCplexLpVariable() + " " + LpCategories[v.cat] + "\n"
return s return string
def __getstate__(self):
# Remove transient data prior to pickling.
state = self.__dict__.copy()
del state['_variable_ids']
return state
def __setstate__(self, state):
# Update transient data prior to unpickling.
self.__dict__.update(state)
self._variable_ids = {id(v): v for v in self._variables}
def copy(self): def copy(self):
"""Make a copy of self. Expressions are copied by reference""" """Make a copy of self. Expressions are copied by reference"""
@ -1264,8 +1252,12 @@ class LpProblem(object):
self.addVariables(list(self.objective.keys())) self.addVariables(list(self.objective.keys()))
for c in self.constraints.values(): for c in self.constraints.values():
self.addVariables(list(c.keys())) self.addVariables(list(c.keys()))
variables = self._variables
#sort the varibles DSU #sort the varibles DSU
return list(sorted(self._variables, key=attrgetter('name'))) variables = [[v.name, v] for v in variables]
variables.sort()
variables = [v for _, v in variables]
return variables
def variablesDict(self): def variablesDict(self):
variables = {} variables = {}