File size: 5,884 Bytes
70b153e
 
 
 
9881e2f
70b153e
4102ac2
 
 
 
 
 
 
70b153e
4102ac2
70b153e
4102ac2
 
 
70b153e
4102ac2
70b153e
4102ac2
 
 
 
70b153e
4102ac2
70b153e
 
4102ac2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70b153e
4102ac2
70b153e
4102ac2
 
 
70b153e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import streamlit as st
import pandas as pd
import io
from pyomo.environ import ConcreteModel, Var, Objective, Constraint, SolverFactory, NonNegativeReals, RangeSet, Param, minimize, value, Reals,Set
from pyomo.environ import *

def get_output(df, df1, df2):
    df.fillna(0, inplace=True)
    df1.fillna(0, inplace=True)
    df2.fillna(0, inplace=True)
    n = df['ID projet'].nunique()
    task = df.groupby('ID projet').count()['Nom projet']
    project = df.groupby('ID projet').count().index

    J_sizes = {i: task[i-1] for i in range(1, n+1)}

    Months = ['Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre']
    months = 12  # Number of months in set M
    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)}

    df1.fillna(0, inplace=True)

    h = df1['Ressource'].nunique()
    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)}
    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]
    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)}

    p_data = {i: df2.loc[df2.index[i-1], 'Pond'] for i in range(1, n + 1)}

    # Define model
    model = ConcreteModel()

    # Sets
    model.I = RangeSet(1, n)
    model.M = RangeSet(1, months)
    model.K = RangeSet(1, h)
    model.J = Set(model.I, initialize=lambda model, i: RangeSet(1, J_sizes[i]))

    # Flatten J for use in parameter definition
    flat_J = [(i, j) for i in model.I for j in model.J[i]]

    # Parameters
    model.H = Param(flat_J, model.M, initialize=H_data)
    model.A = Param(flat_J, model.K, initialize=A_data)
    model.C = Param(model.K, model.M, initialize=C_data)
    model.p = Param(model.I, initialize=p_data)

    # Variables
    model.x = Var(flat_J, model.K, model.M, domain=NonNegativeReals)
    model.y = Var(flat_J, model.K, domain=Binary)
    model.s = Var(flat_J, domain=NonNegativeReals)

    # Objective function
    def objective_rule(model):
        return sum(model.p[i] * model.s[i, j] for i in model.I for j in model.J[i])
    model.objective = Objective(rule=objective_rule, sense=minimize)

    # Capacity constraint
    def capacity_constraint(model, k, month):
        return sum(model.x[i, j, k, month] for (i, j) in flat_J) <= model.C[k, month]
    model.capacity_constraint = Constraint(model.K, model.M, rule=capacity_constraint)

    # Constraint to ensure each task is assigned to exactly one resource
    def single_resource_constraint(model, i, j):
        return sum(model.y[i, j, k] for k in model.K) == 1
    model.single_resource_constraint = Constraint(flat_J, rule=single_resource_constraint)

    # Linking x and y
    def linking_constraint(model, i, j, k, month):
        return model.x[i, j, k, month] <= 1000 * model.y[i, j, k]
    model.linking_constraint = Constraint(flat_J, model.K, model.M, rule=linking_constraint)

    # Ensure glissement plus capacité allouée égale à planifiée
    def glissement_constraint(model, i, j):
        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)
    model.glissement_constraint = Constraint(flat_J, rule=glissement_constraint)

    # Ensure glissement is non-negative
    def non_negative_glissement_constraint(model, i, j):
        return model.s[i, j] >= 0
    model.non_negative_glissement_constraint = Constraint(flat_J, rule=non_negative_glissement_constraint)

    # Ensure x is less than or equal to H
    def x_less_than_H_constraint(model, i, j, k, m):
        return model.x[i, j, k, m] <= model.H[i, j, m]
    model.x_less_than_H_constraint = Constraint(flat_J, model.K, model.M, rule=x_less_than_H_constraint)

    # Solver
    solver = SolverFactory('glpk')
    result = solver.solve(model, tee=True)

    Months = ['Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre']
    results = []
    for (i, j) in flat_J:
        for k in model.K:
            result = {}
            result['i'] = project[i-1]
            result['j'] = j
            result['k'] = df1.loc[df1.index[k-1], 'Ressource']
            for month in model.M:
                result[Months[month-1]] = value(model.x[i, j, k, month])
            results.append(result)

    output_df = pd.DataFrame(results)
    df_finall = output_df.loc[output_df[Months].sum(axis=1) > 0]
    return df_finall



def main():
    st.title("XLSX Upload and Download")

    # File upload section
    uploaded_file = st.file_uploader("Choose an XLSX file to upload", type="xlsx")

    if uploaded_file is not None:
        # Load the uploaded file into a Pandas DataFrame
        df = pd.read_excel(uploaded_file, sheet_name='PMC1')
        df1 = pd.read_excel(uploaded_file, sheet_name ='Base de ressource1')
        df2 =  pd.read_excel(uploaded_file, sheet_name ='Priorisation')

        df_out = get_output(df,df1,df2)
        # Display the uploaded DataFrame
        st.write("Estimation")
        st.dataframe(df_out)

        # Download section
        excel_file = io.BytesIO()
        df_out.to_excel(excel_file, index=False)

        st.download_button(
            label="Download XLSX",
            data=excel_file.getvalue(),
            file_name="downloaded_file.xlsx",
            mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
        )

if __name__ == "__main__":
    main()