def test_backmap_deprecated(self): m = ConcreteModel() m.s = RangeSet(3) m.Y = BooleanVar(m.s) TransformationFactory('core.logical_to_linear').apply_to(m) output = StringIO() with LoggingIntercept(output, 'pyomo.core.base', logging.WARNING): y1 = m.Y[1].get_associated_binary() self.assertIn( "DEPRECATED: Relying on core.logical_to_linear to " "transform BooleanVars that do not appear in " "LogicalConstraints is deprecated. Please " "associate your own binaries if you have BooleanVars " "not used in logical expressions.", output.getvalue().replace('\n', ' ')) output = StringIO() with LoggingIntercept(output, 'pyomo.core.base', logging.WARNING): y2 = m.Y[2].get_associated_binary() self.assertIn( "DEPRECATED: Relying on core.logical_to_linear to " "transform BooleanVars that do not appear in " "LogicalConstraints is deprecated. Please " "associate your own binaries if you have BooleanVars " "not used in logical expressions.", output.getvalue().replace('\n', ' ')) y1.value = 1 y2.value = 0 update_boolean_vars_from_binary(m) self.assertTrue(m.Y[1].value) self.assertFalse(m.Y[2].value) self.assertIsNone(m.Y[3].value)
def test_backmap(self): m = _generate_boolean_model(3) TransformationFactory('core.logical_to_linear').apply_to(m) m.Y_asbinary[1].value = 1 m.Y_asbinary[2].value = 0 update_boolean_vars_from_binary(m) self.assertTrue(m.Y[1].value) self.assertFalse(m.Y[2].value) self.assertIsNone(m.Y[3].value)
def test_backmap_noninteger(self): m = _generate_boolean_model(2) TransformationFactory('core.logical_to_linear').apply_to(m) m.Y_asbinary[1].value = 0.9 update_boolean_vars_from_binary(m, integer_tolerance=0.1) self.assertTrue(m.Y[1].value) # Now try it without the tolerance set with self.assertRaisesRegexp(ValueError, r"Binary variable has non-\{0,1\} value"): update_boolean_vars_from_binary(m)
def test_backmap_hierarchical_model(self): m = _generate_boolean_model(3) m.b = Block() m.b.Y = BooleanVar() m.b.lc = LogicalConstraint(expr=m.Y[1].lor(m.b.Y)) TransformationFactory('core.logical_to_linear').apply_to(m) m.Y_asbinary[1].value = 1 m.Y_asbinary[2].value = 0 m.b.Y.get_associated_binary().value = 1 update_boolean_vars_from_binary(m) self.assertTrue(m.Y[1].value) self.assertFalse(m.Y[2].value) self.assertIsNone(m.Y[3].value) self.assertTrue(m.b.Y.value)
m.use4implies6or7 = LogicalConstraint( expr=m.Y[4].implies(lor(m.Y[6], m.Y[7]))) m.use3implies8 = LogicalConstraint(expr=m.Y[3].implies(m.Y[8])) m.use6or7implies4 = LogicalConstraint( expr=lor(m.Y[6], m.Y[7]).implies(m.Y[4])) m.use6or7 = LogicalConstraint(expr=m.Y[6].xor(m.Y[7])) """Profit (objective) function definition""" m.profit = Objective(expr=sum(m.yCF[unit] for unit in m.units) + sum(m.flow[stream] * CV[stream] for stream in m.streams) + CONSTANT, sense=minimize) """Bound definitions""" # x (flow) upper bounds x_ubs = {3: 2, 5: 2, 9: 2, 10: 1, 14: 1, 17: 2, 19: 2, 21: 2, 25: 3} for i, x_ub in x_ubs.item(): m.flow[i].setub(x_ub) # Optimal solution uses units 2, 4, 6, 8 with objective value 68. return m if __name__ == "__main__": m = build_eight_process_flowsheet() from pyomo.environ import TransformationFactory TransformationFactory('core.logical_to_linear').apply_to(m) SolverFactory('gdpopt').solve(m, tee=True) update_boolean_vars_from_binary(m) m.Y.display() m.flow.display()