Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
|
| 14 |
-
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
|
| 20 |
-
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
|
| 27 |
-
|
| 28 |
|
| 29 |
# Define model
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
|
|
|
| 99 |
result[Months[month-1]] = value(model.x[i, j, k, month])
|
| 100 |
-
|
| 101 |
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 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 |
|