def test_sp_relaxation(self): w = Variable('w') x = Variable('x') y = Variable('y') z = Variable('z') with SignomialsEnabled(): m = Model(x, [x+y >= w, x+y <= z/2, y <= x, y >= 1], {z: 3, w: 3}) r1 = ConstantsRelaxed(m) self.assertEqual(len(r1.varkeys), 8) with self.assertRaises(ValueError): mr1 = Model(x*r1.relaxvars, r1) # no 'prod' mr1 = Model(x*r1.relaxvars.prod()**10, r1) cost1 = mr1.localsolve(verbosity=0)["cost"] self.assertAlmostEqual(cost1/1024, 1, self.ndig) m.debug(verbosity=0) with SignomialsEnabled(): m = Model(x, [x+y >= z, x+y <= z/2, y <= x, y >= 1], {z: 3}) if self.solver != "cvxopt": m.debug(verbosity=0) r2 = ConstraintsRelaxed(m) self.assertEqual(len(r2.varkeys), 7) mr2 = Model(x*r2.relaxvars.prod()**10, r2) cost2 = mr2.localsolve(verbosity=0)["cost"] self.assertAlmostEqual(cost2/1024, 1, self.ndig) with SignomialsEnabled(): m = Model(x, [x+y >= z, x+y <= z/2, y <= x, y >= 1], {z: 3}) if self.solver != "cvxopt": m.debug(verbosity=0) r3 = ConstraintsRelaxedEqually(m) self.assertEqual(len(r3.varkeys), 4) mr3 = Model(x*r3.relaxvar**10, r3) cost3 = mr3.localsolve(verbosity=0)["cost"] self.assertAlmostEqual(cost3/(32*0.8786796585), 1, self.ndig)
def test_sp_relaxation(self): w = Variable("w") x = Variable("x") y = Variable("y") z = Variable("z") with SignomialsEnabled(): m = Model(x, [x + y >= w, x + y <= z / 2, y <= x, y >= 1], { z: 3, w: 3 }) r1 = ConstantsRelaxed(m) self.assertEqual(len(r1.varkeys), 8) with self.assertRaises(ValueError): _ = Model(x * r1.relaxvars, r1) # no "prod" sp = Model(x * r1.relaxvars.prod()**10, r1).sp(use_pccp=False) cost = sp.localsolve(verbosity=0, solver=self.solver)["cost"] self.assertAlmostEqual(cost / 1024, 1, self.ndig) m.debug(verbosity=0, solver=self.solver) with SignomialsEnabled(): m = Model(x, [x + y >= z, x + y <= z / 2, y <= x, y >= 1], {z: 3}) m.debug(verbosity=0, solver=self.solver) r2 = ConstraintsRelaxed(m) self.assertEqual(len(r2.varkeys), 7) sp = Model(x * r2.relaxvars.prod()**10, r2).sp(use_pccp=False) cost = sp.localsolve(verbosity=0, solver=self.solver)["cost"] self.assertAlmostEqual(cost / 1024, 1, self.ndig) with SignomialsEnabled(): m = Model(x, [x + y >= z, x + y <= z / 2, y <= x, y >= 1], {z: 3}) m.debug(verbosity=0, solver=self.solver) r3 = ConstraintsRelaxedEqually(m) self.assertEqual(len(r3.varkeys), 4) sp = Model(x * r3.relaxvar**10, r3).sp(use_pccp=False) cost = sp.localsolve(verbosity=0, solver=self.solver)["cost"] self.assertAlmostEqual(cost / (32 * 0.8786796585), 1, self.ndig)
def test_relax_list(self): x = Variable("x") x_max = Variable("x_max", 1) x_min = Variable("x_min", 2) constraints = [x_min <= x, x <= x_max] ConstraintsRelaxed(constraints) ConstantsRelaxed(constraints) ConstraintsRelaxedEqually(constraints)
def test_equality_relaxation(self): x = Variable("x") m = Model(x, [x == 3, x == 4]) rc = ConstraintsRelaxed(m) m2 = Model(rc.relaxvars.prod() * x**0.01, rc) self.assertAlmostEqual(m2.solve(verbosity=0)(x), 3, 5)
print # m.solve() # raises a RuntimeWarning! print "With constraints relaxed equally" print "================================" from gpkit.constraints.relax import ConstraintsRelaxedEqually allrelaxed = ConstraintsRelaxedEqually(m) mr1 = Model(allrelaxed.relaxvar, allrelaxed) print mr1 print mr1.solve(verbosity=0).table() # solves with an x of 1.414 print print "With constraints relaxed individually" print "=====================================" from gpkit.constraints.relax import ConstraintsRelaxed constraintsrelaxed = ConstraintsRelaxed(m) mr2 = Model( constraintsrelaxed.relaxvars.prod() * m.cost**0.01, # add a bit of the original cost in constraintsrelaxed) print mr2 print mr2.solve(verbosity=0).table() # solves with an x of 1.0 print print "With constants relaxed individually" print "===================================" from gpkit.constraints.relax import ConstantsRelaxed constantsrelaxed = ConstantsRelaxed(m) mr3 = Model( constantsrelaxed.relaxvars.prod() * m.cost**0.01, # add a bit of the original cost in