def test_model_composition_units(self): class Above(Model): """A simple upper bound on x Lower Unbounded --------------- x """ def setup(self): x = self.x = Variable("x", "ft") x_max = Variable("x_{max}", 1, "yard") self.cost = 1 / x return [x <= x_max] class Below(Model): """A simple lower bound on x Upper Unbounded --------------- x """ def setup(self): x = self.x = Variable("x", "m") x_min = Variable("x_{min}", 1, "cm") self.cost = x return [x >= x_min] a, b = Above(), Below() concatm = Model(a.cost * b.cost, [a, b]) concat_cost = concatm.solve(verbosity=0)["cost"] almostequal = self.assertAlmostEqual yard, cm = gpkit.ureg("yard"), gpkit.ureg("cm") ft, meter = gpkit.ureg("ft"), gpkit.ureg("m") if not isinstance(a["x"].key.units, str): almostequal(a.solve(verbosity=0)["cost"], ft / yard, 5) almostequal(b.solve(verbosity=0)["cost"], cm / meter, 5) almostequal(cm / yard, concat_cost, 5) NamedVariables.reset_modelnumbers() a1, b1 = Above(), Below() self.assertEqual(a1["x"].key.lineage, (("Above", 0), )) m = Model(a1["x"], [a1, b1, b1["x"] == a1["x"]]) sol = m.solve(verbosity=0) if not isinstance(a1["x"].key.units, str): almostequal(sol["cost"], cm / ft, 5) a1, b1 = Above(), Below() self.assertEqual(a1["x"].key.lineage, (("Above", 1), )) m = Model(b1["x"], [a1, b1, b1["x"] == a1["x"]]) sol = m.solve(verbosity=0) if not isinstance(b1["x"].key.units, str): almostequal(sol["cost"], cm / meter, 5) self.assertIn(a1["x"], sol["variables"]) self.assertIn(b1["x"], sol["variables"]) self.assertNotIn(a["x"], sol["variables"]) self.assertNotIn(b["x"], sol["variables"])
def monte_carlo_results(m, progress=None, out=None, sol=None, quiet=False, seed=246): NamedVariables.reset_modelnumbers() np.random.seed(seed=seed) monte_up = [{ k.name: stats.truncnorm.rvs(-3, 3, loc=1, scale=(k.key.orig_pr / 300.)) for k in m.substitutions if k.pr } for _ in range(N)] try: if sol is None: sol = m.localsolve(verbosity=0) if out: with out: print("Fuel consumption: %i lbs" % sol("W_f").to("lbf").magnitude) elif not quiet: print("Fuel consumption: %i lbs" % sol("W_f").to("lbf").magnitude) except Exception: return (None, None) else: failures = 0 for var in m.varkeys: if var.fix: m.substitutions[var] = sol["variables"][var.name] if var.margin: m.substitutions[var] = 1 m.pop() for i, subs in enumerate(monte_up): m.substitutions.update(subs) try: # assert not does_it_fail(sol, W_W_coeff1) # UNCOMMENT THE ABOVE AND COMMENT OUT THE BELOW TO SPEED UP m.solve(verbosity=0) except Exception: failures += 1 if progress: progress.value = i / N if out: with out: print(" Failure rate: % 2.1f%% " % (100 * failures / float(N))) elif not quiet: print(" Failure rate: % 2.1f%% " % (100 * failures / float(N))) return (sol("W_f").to("lbf").magnitude, 100 * failures / float(N))
def test_small_named_signomial(self): x = Variable('x') z = Variable('z') local_ndig = 4 nonzero_adder = 0.1 # TODO: support reaching zero, issue #348 with SignomialsEnabled(): J = 0.01 * (x - 1)**2 + nonzero_adder with NamedVariables("SmallSignomial"): m = Model(z, [z >= J]) sol = m.localsolve(verbosity=0) self.assertAlmostEqual(sol['cost'], nonzero_adder, local_ndig) self.assertAlmostEqual(sol('x'), 0.98725425, self.ndig)
def test_small_named_signomial(self): x = Variable("x") z = Variable("z") local_ndig = 4 nonzero_adder = 0.1 with SignomialsEnabled(): J = 0.01 * (x - 1)**2 + nonzero_adder with NamedVariables("SmallSignomial"): m = Model(z, [z >= J]) sol = m.localsolve(verbosity=0, solver=self.solver) self.assertAlmostEqual(sol["cost"], nonzero_adder, local_ndig) self.assertAlmostEqual(sol("x"), 0.98725425, self.ndig)