Nechba commited on
Commit
70b153e
1 Parent(s): d4cd9d5
Files changed (3) hide show
  1. app.py +134 -0
  2. dockerfile +24 -0
  3. requirments.txt +3 -0
app.py ADDED
@@ -0,0 +1,134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ 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
+ n = df['ID projet'].nunique()
8
+ task = df.groupby('ID projet').count()['Nom projet']
9
+ project =df.groupby('ID projet').count().index
10
+
11
+ J_sizes = {i:task[i-1] for i in range(1,n+1)}
12
+
13
+ Months = ['Janvier', 'F茅vrier', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Ao没t', 'Septembre', 'Octobre', 'Novembre', 'D茅cembre']
14
+ months = 12 # Number of months in set M
15
+ 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)}
16
+
17
+ df1.fillna(0, inplace=True)
18
+
19
+ h = df1['Ressource'].nunique()
20
+ 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)}
21
+ 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]
22
+ 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)}
23
+
24
+ p_data = {i:df2.loc[df2.index[i-1],'Pond'] for i in range(1, n + 1)}
25
+
26
+ # Define model
27
+ model = ConcreteModel()
28
+
29
+ # Sets
30
+ model.I = RangeSet(1, n)
31
+ model.M = RangeSet(1, months)
32
+ model.K = RangeSet(1, h)
33
+ model.J = Set(model.I, initialize=lambda model, i: RangeSet(1, J_sizes[i]))
34
+
35
+ # Flatten J for use in parameter definition
36
+ flat_J = [(i, j) for i in model.I for j in model.J[i]]
37
+ 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]
38
+
39
+ # Parameters
40
+ model.H = Param(flat_J, model.M, initialize=H_data)
41
+ model.A = Param(flat_J, model.K, initialize=A_data)
42
+ model.C = Param(model.K, model.M, initialize=C_data)
43
+ model.p = Param(model.I, initialize=p_data)
44
+
45
+ # Variables
46
+ model.x = Var(flat_J, model.K, model.M, domain=NonNegativeReals)
47
+
48
+ # Auxiliary variables for max(0, ...)
49
+ model.max_0_terms = Var(flat_J, model.M, domain=NonNegativeReals)
50
+ model.max_0_terms_2 = Var(flat_J_pairs, model.M, domain=NonNegativeReals)
51
+
52
+ # Objective function
53
+ def objective_rule(model):
54
+ return sum(model.p[i] * (
55
+ sum(model.max_0_terms[i, j, month] for j in model.J[i]) +
56
+ 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)
57
+ ) for i in model.I for month in model.M)
58
+ model.objective = Objective(rule=objective_rule, sense=minimize)
59
+
60
+ # Constraints to handle max(0, ...)
61
+ def max_0_term_constraint_1(model, i, j, month):
62
+ 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)
63
+ model.max_0_term_constraint_1 = Constraint(flat_J, model.M, rule=max_0_term_constraint_1)
64
+
65
+ def max_0_term_constraint_2(model, i, j, month):
66
+ return model.max_0_terms[i, j, month] >= 0
67
+ model.max_0_term_constraint_2 = Constraint(flat_J, model.M, rule=max_0_term_constraint_2)
68
+
69
+ def max_0_term_2_constraint_1(model, i, j, l, month):
70
+ 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)
71
+ model.max_0_term_2_constraint_1 = Constraint(flat_J_pairs, model.M, rule=max_0_term_2_constraint_1)
72
+
73
+ def max_0_term_2_constraint_2(model, i, j, l, month):
74
+ return model.max_0_terms_2[i, j, l, month] >= 0
75
+ model.max_0_term_2_constraint_2 = Constraint(flat_J_pairs, model.M, rule=max_0_term_2_constraint_2)
76
+
77
+ # Capacity constraint
78
+ def capacity_constraint(model, k, month):
79
+ return sum(model.x[i, j, k, month] for (i, j) in flat_J) <= model.C[k, month]
80
+ model.capacity_constraint = Constraint(model.K, model.M, rule=capacity_constraint)
81
+
82
+ # Solver
83
+ solver = SolverFactory('glpk') # Example using GLPK
84
+ result = solver.solve(model, tee=True)
85
+
86
+ Months= ['Janvier', 'F茅vrier', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Ao没t', 'Septembre', 'Octobre', 'Novembre', 'D茅cembre']
87
+ results = []
88
+ for (i, j) in flat_J:
89
+ for k in model.K:
90
+ result = {}
91
+ result['i']=project[i-1]
92
+ result['j']= j
93
+ result['k']=df1.loc[df1.index[k-1],'Ressource']
94
+ for month in model.M:
95
+
96
+ result[Months[month-1]] = value(model.x[i, j, k, month])
97
+ results.append(result)
98
+
99
+
100
+ output_df = pd.DataFrame(results)
101
+ return output_df
102
+
103
+
104
+
105
+ def main():
106
+ st.title("XLSX Upload and Download")
107
+
108
+ # File upload section
109
+ uploaded_file = st.file_uploader("Choose an XLSX file to upload", type="xlsx")
110
+
111
+ if uploaded_file is not None:
112
+ # Load the uploaded file into a Pandas DataFrame
113
+ df = pd.read_excel(uploaded_file, sheet_name='PMC1')
114
+ df1 = pd.read_excel(uploaded_file, sheet_name ='Base de ressource1')
115
+ df2 = pd.read_excel(uploaded_file, sheet_name ='Priorisation')
116
+
117
+ df_out = get_output(df,df1,df2)
118
+ # Display the uploaded DataFrame
119
+ st.write("Estimation")
120
+ st.dataframe(df_out)
121
+
122
+ # Download section
123
+ excel_file = io.BytesIO()
124
+ df_out.to_excel(excel_file, index=False)
125
+
126
+ st.download_button(
127
+ label="Download XLSX",
128
+ data=excel_file.getvalue(),
129
+ file_name="downloaded_file.xlsx",
130
+ mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
131
+ )
132
+
133
+ if __name__ == "__main__":
134
+ main()
dockerfile ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Start with a builder image
2
+ FROM python:3.9.13-slim as builder
3
+
4
+ # Copy only requirements.txt initially to leverage Docker cache
5
+ COPY requirements.txt .
6
+ RUN python3 -m pip install --upgrade pip
7
+ RUN python3 -m pip install --no-cache-dir -r requirements.txt
8
+
9
+
10
+ # sentence transformers deps
11
+ # ----------------------
12
+
13
+
14
+
15
+ # Set working directory
16
+ WORKDIR /app
17
+ # Copy the rest of the application from the current directory to /app inside the container
18
+ COPY . .
19
+
20
+ RUN sudo apt-get install -y glpk-utils
21
+ # Expose port 80 to the outside world
22
+ EXPOSE 8501
23
+ # Command to run the Uvicorn server
24
+ CMD ["streamlit", "run", "app.py"]
requirments.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ streamlit
2
+ openpyxl
3
+ pyomo