1 | # _________________________________________________________________________ |
---|

2 | # |
---|

3 | # Coopr: A COmmon Optimization Python Repository |
---|

4 | # Copyright (c) 2008 Sandia Corporation. |
---|

5 | # This software is distributed under the BSD License. |
---|

6 | # Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation, |
---|

7 | # the U.S. Government retains certain rights in this software. |
---|

8 | # _________________________________________________________________________ |
---|

9 | |
---|

10 | import pyutilib.component.core |
---|

11 | from coopr.pyomo.base import IModelTransformation |
---|

12 | from coopr.pyomo.base import Var |
---|

13 | from coopr.pyomo import BooleanSet, IntegerSet, RangeSet, Reals |
---|

14 | |
---|

15 | |
---|

16 | class RelaxIntegrality(pyutilib.component.core.SingletonPlugin): |
---|

17 | """ |
---|

18 | This plugin relaxes integrality in a Pyomo model. |
---|

19 | """ |
---|

20 | |
---|

21 | pyutilib.component.core.implements(IModelTransformation) |
---|

22 | |
---|

23 | def __init__(self, **kwds): |
---|

24 | kwds['name'] = "relax_integrality" |
---|

25 | pyutilib.component.core.Plugin.__init__(self, **kwds) |
---|

26 | |
---|

27 | def apply(self, model, **kwds): |
---|

28 | # |
---|

29 | # Clone the model |
---|

30 | # |
---|

31 | M = model.clone() |
---|

32 | # |
---|

33 | # Iterate over all variables, replacing the domain with a real-valued domain |
---|

34 | # and setting appropriate bounds. |
---|

35 | # |
---|

36 | # Note: this technique is not extensible. If a user creates a new integer variable |
---|

37 | # class then we'd need to add it here (unless it was a subset of one of the sets we |
---|

38 | # detect here. Alternatively, we could make the variables responsible for managing |
---|

39 | # the generation of a relaxed variable. But that would pollute the variable objects |
---|

40 | # with logic about generating a specific relaxation. That's probably a worse alternative. |
---|

41 | # |
---|

42 | comp = M.components(Var) |
---|

43 | for var in comp.values(): |
---|

44 | if isinstance(var.domain, BooleanSet): |
---|

45 | var.domain=Reals |
---|

46 | var._bounds = (0.0,1.0) |
---|

47 | elif isinstance(var.domain, IntegerSet): |
---|

48 | var.domain=Reals |
---|

49 | var._bounds = var.domain._bounds |
---|

50 | elif isinstance(var.domain, RangeSet): |
---|

51 | var.domain=Reals |
---|

52 | var._bounds = var.domain._bounds |
---|

53 | M.statistics.number_of_binary_variables = 0 |
---|

54 | M.statistics.number_of_integer_variables = 0 |
---|

55 | M.statistics.number_of_continuous_variables = len(M._var) |
---|

56 | return M |
---|

57 | |
---|