1 | |
---|

2 | from coopr.pyomo import * |
---|

3 | import math |
---|

4 | |
---|

5 | model = Model() |
---|

6 | |
---|

7 | model.N = Param(within=PositiveIntegers) |
---|

8 | |
---|

9 | model.Locations = RangeSet(1,model.N) |
---|

10 | |
---|

11 | model.P = Param(within=RangeSet(1,model.N)) |
---|

12 | |
---|

13 | model.M = Param(within=PositiveIntegers) |
---|

14 | |
---|

15 | model.Customers = RangeSet(1,model.M) |
---|

16 | |
---|

17 | def d_rule(n, m, model): |
---|

18 | return math.sin(n*2.33333+m*7.99999) |
---|

19 | model.d = Param(model.Locations, model.Customers, rule=d_rule, within=Reals) |
---|

20 | |
---|

21 | model.x = Var(model.Locations, model.Customers, bounds=(0.0,1.0)) |
---|

22 | |
---|

23 | model.y = Var(model.Locations, within=Binary) |
---|

24 | |
---|

25 | def rule(model): |
---|

26 | return sum( [model.d[n,m]*model.x[n,m] for n in model.Locations for m in model.Customers] ) |
---|

27 | model.obj = Objective(rule=rule) |
---|

28 | |
---|

29 | def rule(m, model): |
---|

30 | return (sum( [model.x[n,m] for n in model.Locations]), 1.0) |
---|

31 | model.single_x = Constraint(model.Customers, rule=rule) |
---|

32 | |
---|

33 | def rule(n,m,model): |
---|

34 | return (None, model.x[n,m] - model.y[n], 0.0) |
---|

35 | model.bound_y = Constraint(model.Locations, model.Customers, rule=rule) |
---|

36 | |
---|

37 | def rule(model): |
---|

38 | return (sum( [model.y[n] for n in model.Locations] ) - model.P, 0.0) |
---|

39 | model.num_facilities = Constraint(rule=rule) |
---|

40 | |
---|