def test_simple_pwa(): pwa = fs.PiecewiseAffine((1, 1.5, 2), name='aoeu') prob = fs.Problem() prob.objective = fs.Minimize(pwa.func([3, 2, 4])) solution = default_solver.solve(prob) print(solution) for var in pwa.variables: var.take_value(solution) assert (approx(pwa.arg.value, 1.5))
def run_model(p, c, cl, times): times = tuple(times) prob = fs.Problem() prob.add_constraints(chain(*(cl.constraints(t) for t in times))) prob.objective = fs.Minimize(sum(p.cost(t) for t in times)) solution = default_solver.solve(prob) for t in times: c.activity(t).take_value(solution) p.activity(t).take_value(solution)
def test_balance_simple(): time = 0 consumption = lambda t: 1 c = Consumer(consumption, name='Consumer') prob = fs.Problem() prob.add_constraints(c.constraints(time)) prob.objective = fs.Minimize(c.consumption[RESOURCE](time)) # Raises SolverError because the consumer wants to consume but noone delivers solution = default_solver.solve(prob)
def advance(self): if self.horizon < self.step: raise RuntimeError() opt_times = [self.t + delta_t for delta_t in range(self.horizon)] step_times = [self.t + delta_t for delta_t in range(self.step)] parts = self.parts() | {self} problem = fs.Problem() problem.objective = fs.Minimize(sum(p.cost(t) for p, t in product(parts, opt_times))) problem.add_constraints(chain(*(p.constraints(t) for p, t in product(parts, opt_times)))) solution = self.solver.solve(problem) for p, t in product(parts, step_times): for v in p.state_variables(t): v.take_value(solution) self.t += self.step
def check_variant(variant): times = list(range(5)) consumption = lambda t: t * 1.5 p = Producer(name='Producer') c = Consumer(consumption, variant) cl = Cluster(p, c, resource=RESOURCE, name='Cluster') prob = fs.Problem() prob.add_constraints(chain(*(cl.constraints(t) for t in times))) prob.objective = fs.Minimize(sum(p.cost(t) for t in times)) solution = default_solver.solve(prob) for t in times: c.activity(t).take_value(solution) p.activity(t).take_value(solution) for t in times: assert approx(p.production[RESOURCE](t).value, consumption(t)) assert approx(c.consumption[RESOURCE](t).value, consumption(t))
def solve(self): opt_times = self.times_between(self.time, self.time_end) parts = self.descendants if self.require_cost is True: cost_contributors = parts else: cost_contributors = filter(self.require_cost, parts) running_cost = [ p.cost(t) for p, t in product(cost_contributors, opt_times) ] investment_cost = [ p.investment_cost for p in parts if ('investment_cost' in dir(p)) ] costs = running_cost + investment_cost system_cost = fs.Sum(c for c in costs) problem = fs.Problem() problem.objective = fs.Minimize(system_cost) problem += (p.constraints.make(t) for p, t in product(parts, opt_times)) if self.timeindependent_constraint: problem += self.timeindependent_constraint solver = fs.get_solver() solution = solver.solve(problem) for p, t in product(parts, self.iter_times_between(self.time, self.time_end)): for v in p.state_variables(t): v.take_value(solution) for p in parts: if 'static_variables' in dir(p): for v in p.static_variables: v.take_value(solution)
def test_state_vars(): times = list(range(1, 4)) consumption = lambda t: t * 1.5 p = Producer(name='Producer') c = Consumer(consumption, name='Consumer') cl = Cluster(p, c, resource=RESOURCE, name='Cluster') prob = fs.Problem() prob.add_constraints(chain(*(cl.constraints(t) for t in times))) prob.objective = fs.Minimize(sum(p.cost(t) for t in times)) solution = default_solver.solve(prob) for t, part in product(times, cl.parts()): for v in part.state_variables(t): v.take_value(solution) for t in times: assert approx(p.production[RESOURCE](t).value, consumption(t)) assert approx(c.consumption[RESOURCE](t).value, consumption(t))
def test_simple_SOS1(): n = 4 index = 2 sum_val = 3. vs = [fs.Variable(lb=-1, domain=fs.Domain.integer) for i in range(n)] #vs[0] = fs.Variable(lb=-1, domain=fs.Domain.integer) weights = [1 for i in range(n)] weights[index] = 0.5 prob = fs.Problem() prob.add_constraint(fs.SOS1(vs)) prob.add_constraint(Constraint(sum(vs) == sum_val)) #prob.constraints.update(Constraint(v >= 0) for v in vs) prob.objective = fs.Minimize(sum(v * w for v, w in zip(vs, weights))) solution = default_solver.solve(prob) print(solution) for v in vs: v.take_value(solution) for i in range(n): if i == index: assert (approx(vs[i].value, sum_val)) else: assert (approx(vs[i].value, 0))