Nechba commited on
Commit
4102ac2
·
verified ·
1 Parent(s): c60cc17

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -91
app.py CHANGED
@@ -3,106 +3,106 @@ import pandas as pd
3
  import io
4
  from pyomo.environ import ConcreteModel, Var, Objective, Constraint, SolverFactory, NonNegativeReals, RangeSet, Param, minimize, value, Reals,Set
5
 
6
- def get_output(df,df1,df2):
7
- df.fillna(0, inplace=True)
8
- df1.fillna(0, inplace=True)
9
- df2.fillna(0, inplace=True)
10
- n = df['ID projet'].nunique()
11
- task = df.groupby('ID projet').count()['Nom projet']
12
- project =df.groupby('ID projet').count().index
13
 
14
- J_sizes = {i:task[i-1] for i in range(1,n+1)}
15
 
16
- Months = ['Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre']
17
- months = 12 # Number of months in set M
18
- H_data = {(i, j, month): df.loc[df['ID projet']==project[i-1]].loc[df.loc[df['ID projet']==project[i-1]].index[j-1],Months[month-1]] for i in range(1, n + 1) for j in range(1, J_sizes[i] + 1) for month in range(1, months + 1)}
19
 
20
- df1.fillna(0, inplace=True)
21
 
22
- h = df1['Ressource'].nunique()
23
- A_data = {(i, j, k): int(df.loc[df['ID projet']==project[i-1]].loc[df.loc[df['ID projet']==project[i-1]].index[j-1], 'Equipe'] == df1.loc[df1.index[k-1],'Equipe']) for i in range(1, n + 1) for j in range(1, J_sizes[i] + 1) for k in range(1, h + 1)}
24
- per= [0.08,0.08,0.09,0.09,0.08,0.09,0.07,0.07,0.09,0.09,0.09,0.08]
25
- C_data = {(k, month): df1.loc[df1.index[k-1],'Capacité']*per[month-1] for k in range(1, h + 1) for month in range(1, months + 1)}
26
 
27
- p_data = {i:df2.loc[df2.index[i-1],'Pond'] for i in range(1, n + 1)}
28
 
29
  # Define model
30
- model = ConcreteModel()
31
-
32
- # Sets
33
- model.I = RangeSet(1, n)
34
- model.M = RangeSet(1, months)
35
- model.K = RangeSet(1, h)
36
- model.J = Set(model.I, initialize=lambda model, i: RangeSet(1, J_sizes[i]))
37
-
38
- # Flatten J for use in parameter definition
39
- flat_J = [(i, j) for i in model.I for j in model.J[i]]
40
- flat_J_pairs = [(i, j, l) for i in model.I for j in model.J[i] for l in model.J[i] if j != l]
41
-
42
- # Parameters
43
- model.H = Param(flat_J, model.M, initialize=H_data)
44
- model.A = Param(flat_J, model.K, initialize=A_data)
45
- model.C = Param(model.K, model.M, initialize=C_data)
46
- model.p = Param(model.I, initialize=p_data)
47
-
48
- # Variables
49
- model.x = Var(flat_J, model.K, model.M, domain=NonNegativeReals)
50
-
51
- # Auxiliary variables for max(0, ...)
52
- model.max_0_terms = Var(flat_J, model.M, domain=NonNegativeReals)
53
- model.max_0_terms_2 = Var(flat_J_pairs, model.M, domain=NonNegativeReals)
54
-
55
- # Objective function
56
- def objective_rule(model):
57
- return sum(model.p[i] * (
58
- sum(model.max_0_terms[i, j, month] for j in model.J[i]) +
59
- sum(model.max_0_terms_2[i, j, l, month] for j in model.J[i] for l in model.J[i] if l != j)
60
- ) for i in model.I for month in model.M)
61
- model.objective = Objective(rule=objective_rule, sense=minimize)
62
-
63
- # Constraints to handle max(0, ...)
64
- def max_0_term_constraint_1(model, i, j, month):
65
- return model.max_0_terms[i, j, month] >= model.H[i, j, month] - sum(model.A[i, j, k] * model.x[i, j, k, month] for k in model.K)
66
- model.max_0_term_constraint_1 = Constraint(flat_J, model.M, rule=max_0_term_constraint_1)
67
-
68
- def max_0_term_constraint_2(model, i, j, month):
69
- return model.max_0_terms[i, j, month] >= 0
70
- model.max_0_term_constraint_2 = Constraint(flat_J, model.M, rule=max_0_term_constraint_2)
71
-
72
- def max_0_term_2_constraint_1(model, i, j, l, month):
73
- return model.max_0_terms_2[i, j, l, month] >= model.H[i, l, month] - sum(model.A[i, l, k] * model.x[i, l, k, month] for k in model.K)
74
- model.max_0_term_2_constraint_1 = Constraint(flat_J_pairs, model.M, rule=max_0_term_2_constraint_1)
75
-
76
- def max_0_term_2_constraint_2(model, i, j, l, month):
77
- return model.max_0_terms_2[i, j, l, month] >= 0
78
- model.max_0_term_2_constraint_2 = Constraint(flat_J_pairs, model.M, rule=max_0_term_2_constraint_2)
79
-
80
- # Capacity constraint
81
- def capacity_constraint(model, k, month):
82
- return sum(model.x[i, j, k, month] for (i, j) in flat_J) <= model.C[k, month]
83
- model.capacity_constraint = Constraint(model.K, model.M, rule=capacity_constraint)
84
-
85
- # Solver
86
- solver = SolverFactory('glpk') # Example using GLPK
87
- result = solver.solve(model, tee=True)
88
-
89
- Months= ['Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre']
90
- results = []
91
- for (i, j) in flat_J:
92
- for k in model.K:
93
- result = {}
94
- result['i']=project[i-1]
95
- result['j']= j
96
- result['k']=df1.loc[df1.index[k-1],'Ressource']
97
- for month in model.M:
98
-
 
99
  result[Months[month-1]] = value(model.x[i, j, k, month])
100
- results.append(result)
101
 
102
-
103
- output_df = pd.DataFrame(results)
104
- df_finall = output_df.loc[output_df[Months].sum(axis=1)>0]
105
- return df_finall
106
 
107
 
108
 
 
3
  import io
4
  from pyomo.environ import ConcreteModel, Var, Objective, Constraint, SolverFactory, NonNegativeReals, RangeSet, Param, minimize, value, Reals,Set
5
 
6
+ def get_output(df, df1, df2):
7
+ df.fillna(0, inplace=True)
8
+ df1.fillna(0, inplace=True)
9
+ df2.fillna(0, inplace=True)
10
+ n = df['ID projet'].nunique()
11
+ task = df.groupby('ID projet').count()['Nom projet']
12
+ project = df.groupby('ID projet').count().index
13
 
14
+ J_sizes = {i: task[i-1] for i in range(1, n+1)}
15
 
16
+ Months = ['Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre']
17
+ months = 12 # Number of months in set M
18
+ H_data = {(i, j, month): df.loc[df['ID projet'] == project[i-1]].loc[df.loc[df['ID projet'] == project[i-1]].index[j-1], Months[month-1]] for i in range(1, n + 1) for j in range(1, J_sizes[i] + 1) for month in range(1, months + 1)}
19
 
20
+ df1.fillna(0, inplace=True)
21
 
22
+ h = df1['Ressource'].nunique()
23
+ A_data = {(i, j, k): int(df.loc[df['ID projet'] == project[i-1]].loc[df.loc[df['ID projet'] == project[i-1]].index[j-1], 'Equipe'] == df1.loc[df1.index[k-1], 'Equipe']) for i in range(1, n + 1) for j in range(1, J_sizes[i] + 1) for k in range(1, h + 1)}
24
+ per = [0.08, 0.08, 0.09, 0.09, 0.08, 0.09, 0.07, 0.07, 0.09, 0.09, 0.09, 0.08]
25
+ C_data = {(k, month): df1.loc[df1.index[k-1], 'Capacité'] * per[month-1] for k in range(1, h + 1) for month in range(1, months + 1)}
26
 
27
+ p_data = {i: df2.loc[df2.index[i-1], 'Pond'] for i in range(1, n + 1)}
28
 
29
  # Define model
30
+ model = ConcreteModel()
31
+
32
+ # Sets
33
+ model.I = RangeSet(1, n)
34
+ model.M = RangeSet(1, months)
35
+ model.K = RangeSet(1, h)
36
+ model.J = Set(model.I, initialize=lambda model, i: RangeSet(1, J_sizes[i]))
37
+
38
+ # Flatten J for use in parameter definition
39
+ flat_J = [(i, j) for i in model.I for j in model.J[i]]
40
+
41
+ # Parameters
42
+ model.H = Param(flat_J, model.M, initialize=H_data)
43
+ model.A = Param(flat_J, model.K, initialize=A_data)
44
+ model.C = Param(model.K, model.M, initialize=C_data)
45
+ model.p = Param(model.I, initialize=p_data)
46
+
47
+ # Variables
48
+ model.x = Var(flat_J, model.K, model.M, domain=NonNegativeReals)
49
+ model.y = Var(flat_J, model.K, domain=Binary)
50
+ model.s = Var(flat_J, domain=NonNegativeReals)
51
+
52
+ # Objective function
53
+ def objective_rule(model):
54
+ return sum(model.p[i] * model.s[i, j] for i in model.I for j in model.J[i])
55
+ model.objective = Objective(rule=objective_rule, sense=minimize)
56
+
57
+ # Capacity constraint
58
+ def capacity_constraint(model, k, month):
59
+ return sum(model.x[i, j, k, month] for (i, j) in flat_J) <= model.C[k, month]
60
+ model.capacity_constraint = Constraint(model.K, model.M, rule=capacity_constraint)
61
+
62
+ # Constraint to ensure each task is assigned to exactly one resource
63
+ def single_resource_constraint(model, i, j):
64
+ return sum(model.y[i, j, k] for k in model.K) == 1
65
+ model.single_resource_constraint = Constraint(flat_J, rule=single_resource_constraint)
66
+
67
+ # Linking x and y
68
+ def linking_constraint(model, i, j, k, month):
69
+ return model.x[i, j, k, month] <= 1000 * model.y[i, j, k]
70
+ model.linking_constraint = Constraint(flat_J, model.K, model.M, rule=linking_constraint)
71
+
72
+ # Ensure glissement plus capacité allouée égale à planifiée
73
+ def glissement_constraint(model, i, j):
74
+ return model.s[i, j] >= sum(model.H[i, j, m] for m in model.M) - sum(model.x[i, j, k, m] * model.A[i, j, k] for k in model.K for m in model.M)
75
+ model.glissement_constraint = Constraint(flat_J, rule=glissement_constraint)
76
+
77
+ # Ensure glissement is non-negative
78
+ def non_negative_glissement_constraint(model, i, j):
79
+ return model.s[i, j] >= 0
80
+ model.non_negative_glissement_constraint = Constraint(flat_J, rule=non_negative_glissement_constraint)
81
+
82
+ # Ensure x is less than or equal to H
83
+ def x_less_than_H_constraint(model, i, j, k, m):
84
+ return model.x[i, j, k, m] <= model.H[i, j, m]
85
+ model.x_less_than_H_constraint = Constraint(flat_J, model.K, model.M, rule=x_less_than_H_constraint)
86
+
87
+ # Solver
88
+ solver = SolverFactory('glpk')
89
+ result = solver.solve(model, tee=True)
90
+
91
+ Months = ['Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre']
92
+ results = []
93
+ for (i, j) in flat_J:
94
+ for k in model.K:
95
+ result = {}
96
+ result['i'] = project[i-1]
97
+ result['j'] = j
98
+ result['k'] = df1.loc[df1.index[k-1], 'Ressource']
99
+ for month in model.M:
100
  result[Months[month-1]] = value(model.x[i, j, k, month])
101
+ results.append(result)
102
 
103
+ output_df = pd.DataFrame(results)
104
+ df_finall = output_df.loc[output_df[Months].sum(axis=1) > 0]
105
+ return df_finall
 
106
 
107
 
108