def lp_model_1(): model = flp.Model() v1 = flp.variable.Continuous('v1', 10, 20) v2 = flp.variable.Continuous('v2', 10, 20) model += v1, v2 model += flp.Objective('max', sum([v1 + v2])) model += flp.Constraint(v2, '<=', -2 * v1 + 50) return model
def infeasible_ip_model_1(): model = flp.Model() v1 = flp.variable.Binary('v1') v2 = flp.variable.Binary('v2') model += v1, v2 model += flp.Constraint(v1 + v2, '<=', 1) model += flp.Constraint(v1, '>=', 0.1) model += flp.Constraint(v2, '>=', 0.1) return model
def infeasible_lp_model_1(): model = flp.Model() v1 = flp.variable.Continuous('v1') v2 = flp.variable.Continuous('v2') model += v1, v2 model += flp.Constraint(v1 + v2, '<=', 1) model += flp.Constraint(v1, '>=', 2) model += flp.Constraint(v2, '>=', 2) return model
def ip_model_1(): model = flp.Model() v1 = flp.variable.Binary('v1') v2 = flp.variable.Integer('v2') model += v1, v2 model += flp.Objective('min', v2) model += flp.Constraint(v2, '>=', -1.5 * v1 - 1) model += flp.Constraint(v2, '>=', 2.5 * v1 - 5) return model
def unbounded_lp_model_1(): model = flp.Model() v1 = flp.variable.Continuous('v1') v2 = flp.variable.Continuous('v2') model += v1, v2 model += flp.Objective( 'max', v1 + 2 * v2) ### TODO: ERROR CHECKING REQUIRED ON INPUT ARGUMENTS model += flp.Constraint(v1 + v2, '<=', 1) return model
def test_model_1(self): model = flp.Model() v1 = flp.variable.Continuous('v1', 10, 20) v2 = flp.variable.Integer('v2', -10, 0) v3 = flp.variable.Binary('v3') model.add_variables(*[v1, v2, v3]) obj = flp.Objective('max', 3 * v1) model.add_objective(obj) c = flp.Constraint(v1 + 3, '<=', v3) model.add_constraints(c) self.assertIsInstance(model, flp.Model) self.assertEqual(3, model.num_vars()) self.assertEqual(1, model.num_cons())
def test_write_to_file(self): model = flp.Model() v1 = flp.variable.Continuous('v1', 10, 20) v2 = flp.variable.Integer('v2', -10) v3 = flp.variable.Binary('v3') v4 = flp.variable.Continuous('v4') model.add_variables(v1, v2, v3, v4) model += flp.Objective('max', 3 * v1) model += flp.Constraint(v1, '<=', v3) model += flp.Constraint(-v1 + v2, '>=', v3 / 3 - 7.5) full_lp_filename = flp.write_lp_file(model, 'test.lp') p = Path(full_lp_filename) self.assertEqual(p.is_file(), True) p.unlink()
def big_ip_model_1(solver): # Knapsack class Item: def __init__(self, name, value, size): self.name = str(name) self.value = value self.size = size class Bag: def __init__(self, name, size): self.name = str(name) self.size = size random.seed(0) N_ITEMS = 100 N_BAGS = int(N_ITEMS / 4) items = [ Item(i, random.randint(1, 100), random.randint(1, 50)) for i in range(N_ITEMS) ] bags = [Bag(i, random.randint(50, 100)) for i in range(N_BAGS)] t = time.time() model = flp.Model() vars = {} for item in items: for bag in bags: v = flp.variable.Binary(f'v{item.name}_{bag.name}') vars[(item.name, bag.name)] = v model += v print('Vars added', time.time() - t) # obj_expr = sum(item.value * model.variables[f'v{item.name}_{bag.name}'] \ # for item in items for bag in bags) # obj_expr = flp.Expression.from_var_dict(dict(model.variables[f'v{item.name}_{bag.name}'], item.value) \ # for item in items for bag in bags) obj_expr = flp.tsum((item.value, model.variables[f'v{item.name}_{bag.name}']) \ for item in items for bag in bags) model += flp.Objective('max', obj_expr) print('Objective added', time.time() - t) # Assign each item at most once for item in items: lhs_expr = flp.tsum( (1, model.variables[f'v{item.name}_{bag.name}']) for bag in bags) model += flp.Constraint(lhs_expr, '<=', 1) # Fill bag at most to size for bag in bags: lhs_expr = flp.tsum( (item.size, model.variables[f'v{item.name}_{bag.name}']) for item in items) model += flp.Constraint(lhs_expr, '<=', bag.size) print('Constraints added', time.time() - t) solver.set_params({'time_limit': 10}) # solver.set_mipstart(soln) soln, run = solver.solve(model, keep_log_file=True, keep_lp_file=True, keep_sol_file=True) print('Solved', time.time() - t) print(flp.util.run_summary(run, soln, model))
def test_error_on_nonexistent_var_1(self): model = flp.Model() v1 = flp.variable.Continuous('v1', 10, 20) with self.assertRaises(RuntimeError): model += flp.Objective('max', v1)