Hello everyone,
I am currently learning how to use Python to solve linear programming problems.
I wrote an algorithm to calculate the quantities produced Qij of a production of glasses and the level of the Sij stock with i models and j weeks. The aim being to minimize the total cost of production and storage while meeting customer demand (in the Demande_table.csv file) and respecting initial, final stock constraints as well as the work capacities of machines, labor staff and maximum storage (in the Donnees_prod_table.csv files).
I managed to operate the algorithm only by removing the constraint which allows the consistency of stocks between the weeks. The result is therefore not concluded. I don't understand why this constraint makes my problem "Undifined" :
The entire code :
The result :
My "Donnees_prod_table.csv" file :
My "Demande_table_table.csv" file :
The theorical answer should be 185899.3€.
I am currently learning how to use Python to solve linear programming problems.
I wrote an algorithm to calculate the quantities produced Qij of a production of glasses and the level of the Sij stock with i models and j weeks. The aim being to minimize the total cost of production and storage while meeting customer demand (in the Demande_table.csv file) and respecting initial, final stock constraints as well as the work capacities of machines, labor staff and maximum storage (in the Donnees_prod_table.csv files).
I managed to operate the algorithm only by removing the constraint which allows the consistency of stocks between the weeks. The result is therefore not concluded. I don't understand why this constraint makes my problem "Undifined" :
1 2 3 |
for i in range ( 1 , n + 1 ): for j in range ( 1 , m + 1 ): pb + = S[(i, j)] = = S[(i, j - 1 )] + Q[(i, j)] - Demande_table.iloc[(i - 1 ,j - 1 )] |
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 |
import numpy as np import pandas as pd import xlsxwriter import pulp import time # Enregistrer l'heure de début start_time = time.time() pb = pulp.LpProblem( "Production_de_verres" , pulp.LpMinimize) n = 6 # Nombre de modèles de verre m = 12 # Nombre de périodes # lecture des données du problème Demande_table = pd.read_csv( 'Demande_table.csv' , index_col = [ 'Modèle' ], sep = ';' ) Donnees_prod_table = pd.read_csv( 'Donnees_prod_table.csv' , index_col = [ 'Modèle' ], sep = ';' ) # Arrondir les variables à une seule décimale def float_1d(value): return round (value, 1 ) # Création des variables Qij : quantité de lots du verre i à produire en période j Q = pulp.LpVariable.dicts( "Q" , [(i,j) for i in range ( 1 ,n + 1 ) for j in range ( 1 ,m + 1 )], lowBound = 0 , cat = 'Float' ) # Création des variables Sij : Niveau du stock du verre i en fin de période j S = pulp.LpVariable.dicts( "S" , [(i,j) for i in range ( 1 ,n + 1 ) for j in range ( 0 ,m + 1 )], lowBound = 0 , cat = 'Float' ) # Définition des paramètres du problème CAT = 390 # Capacité de travail maximal des hommes par semaine CAM = 850 # Capacité maximale de travail des machines par semaine CAZ = 1000 # Capacité maximale de stockage C = Donnees_prod_table[ 'Ci' ].tolist() # Coûts de production du modèle i Cs = Donnees_prod_table[ 'Csi' ].tolist() # Coûts de stockage du modèle i SI = Donnees_prod_table[ 'SI' ].tolist() # Stock initial du modèle i SF = Donnees_prod_table[ 'SF' ].tolist() # Stock final du modèle i Tut = Donnees_prod_table[ 'Tut' ].tolist() # Temps unitaire de travail pour un lot du modèle i Tum = Donnees_prod_table[ 'Tum' ].tolist() # Temps unitaire d'utilisation des machines pour un lot du modèle i Tzs = Donnees_prod_table[ 'Tzs' ].tolist() # Taille unitaire dans le stockage pour un lot du modèle i # Définir la fonction objectif Obj = pulp.lpSum(C[i - 1 ] * Q[(i,j)] + Cs[i - 1 ] * S[(i,j)] for i in range ( 1 ,n + 1 ) for j in range ( 1 ,m + 1 )) pb + = Obj # Ajout des contraintes au modèle for i in range ( 1 , n + 1 ): pb + = S[(i, 0 )] = = SI[i - 1 ] pb + = S[(i, m)] > = SF[i - 1 ] pb + = pulp.lpSum(Tut[i - 1 ] * Q[(i,j)] for i in range ( 1 , n + 1 ) for j in range ( 1 , m + 1 )) < = CAT pb + = pulp.lpSum(Tum[i - 1 ] * Q[(i,j)] for i in range ( 1 , n + 1 ) for j in range ( 1 , m + 1 )) < = CAM pb + = pulp.lpSum(Tzs[i - 1 ] * S[(i,j)] for i in range ( 1 , n + 1 ) for j in range ( 1 , m + 1 )) < = CAZ #for i in range(1, n+1): #for j in range(1, m+1): #pb+= S[(i, j)] == S[(i, j-1)] + Q[(i, j)] - Demande_table.iloc[(i-1,j-1)] # Résolution du problème pb.solve() # Affichage des résultats print ( "Statut : " , pulp.LpStatus[pb.status]) print ( "Coût total = " , pulp.value(pb.objective), "€" ) print ( "\n" ) print ( "Quantité produite du modèle i en période j :" ) print ( "\n" ) Q_values = np.array([[Q[(i,j)].varValue for j in range ( 1 , m + 1 )] for i in range ( 1 , n + 1 )]) Q_df = pd.DataFrame(Q_values, index = [ f 'Modèle {i}' for i in range ( 1 , n + 1 )], columns = [ f 'Période {j}' for j in range ( 1 , m + 1 )]) print (Q_df) print ( "\n\n" ) print ( "Niveau de stock du modèle i en période en fin j :" ) print ( "\n" ) S_values = np.array([[S[(i,j)].varValue for j in range (m + 1 )] for i in range ( 1 , n + 1 )]) S_df = pd.DataFrame(S_values, index = [ f 'Modèle {i}' for i in range ( 1 , n + 1 )], columns = [ f 'Période {j}' for j in range (m + 1 )]) print (S_df) # Création d'un fichier Excel avec les résultats du problème writer = pd.ExcelWriter( 'Résultats_ExoProdVerres.xlsx' , engine = 'xlsxwriter' ) Q_df.to_excel(writer, sheet_name = 'Qij' ) S_df.to_excel(writer, sheet_name = 'Sij' ) writer.save() # Enregistrer l'heure de fin et calculer le temps écoulé end_time = time.time() elapsed_time = end_time - start_time print ( "\n\n" ) print ( "Temps écoulé:" , elapsed_time, "secondes" ) |
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 |
Statut : Optimal Coût total = 1350.0 € Quantité produite du modèle i en période j : Période 1 Période 2 Période 3 Période 4 Période 5 Période 6 \ Modèle 1 0.0 0.0 0.0 0.0 0.0 0.0 Modèle 2 0.0 0.0 0.0 0.0 0.0 0.0 Modèle 3 0.0 0.0 0.0 0.0 0.0 0.0 Modèle 4 0.0 0.0 0.0 0.0 0.0 0.0 Modèle 5 0.0 0.0 0.0 0.0 0.0 0.0 Modèle 6 0.0 0.0 0.0 0.0 0.0 0.0 Période 7 Période 8 Période 9 Période 10 Période 11 Période 12 Modèle 1 0.0 0.0 0.0 0.0 0.0 0.0 Modèle 2 0.0 0.0 0.0 0.0 0.0 0.0 Modèle 3 0.0 0.0 0.0 0.0 0.0 0.0 Modèle 4 0.0 0.0 0.0 0.0 0.0 0.0 Modèle 5 0.0 0.0 0.0 0.0 0.0 0.0 Modèle 6 0.0 0.0 0.0 0.0 0.0 0.0 Niveau de stock du modèle i en période en fin j : Période 0 Période 1 Période 2 Période 3 Période 4 Période 5 \ Modèle 1 50.0 0.0 0.0 0.0 0.0 0.0 Modèle 2 20.0 0.0 0.0 0.0 0.0 0.0 Modèle 3 0.0 0.0 0.0 0.0 0.0 0.0 Modèle 4 15.0 0.0 0.0 0.0 0.0 0.0 Modèle 5 0.0 0.0 0.0 0.0 0.0 0.0 Modèle 6 10.0 0.0 0.0 0.0 0.0 0.0 Période 6 Période 7 Période 8 Période 9 Période 10 Période 11 \ Modèle 1 0.0 0.0 0.0 0.0 0.0 0.0 Modèle 2 0.0 0.0 0.0 0.0 0.0 0.0 Modèle 3 0.0 0.0 0.0 0.0 0.0 0.0 Modèle 4 0.0 0.0 0.0 0.0 0.0 0.0 Modèle 5 0.0 0.0 0.0 0.0 0.0 0.0 Modèle 6 0.0 0.0 0.0 0.0 0.0 0.0 Période 12 Modèle 1 10.0 Modèle 2 10.0 Modèle 3 10.0 Modèle 4 10.0 Modèle 5 10.0 Modèle 6 10.0 Temps écoulé: 0.13071727752685547 secondes |
1 2 3 4 5 6 7 |
Modèle;Ci;Csi;SI;SF;Tut;Tum;Tzs V1; 100 ; 25 ; 50 ; 10 ; 3 ; 2 ; 4 V2; 80 ; 28 ; 20 ; 10 ; 3 ; 1 ; 5 V3; 110 ; 25 ; 0 ; 10 ; 3 ; 4 ; 5 V4; 90 ; 27 ; 15 ; 10 ; 2 ; 8 ; 6 V5; 200 ; 10 ; 0 ; 10 ; 4 ; 11 ; 4 V6; 140 ; 20 ; 10 ; 10 ; 4 ; 9 ; 9 |
1 2 3 4 5 6 7 |
Modèle;Période 1 ;Période 2 ;Période 3 ;Période 4 ;Période 5 ;Période 6 ;Période 7 ;Période 8 ;Période 9 ;Période 10 ;Période 11 ;Période 12 V1; 20 ; 22 ; 18 ; 35 ; 17 ; 19 ; 23 ; 20 ; 29 ; 30 ; 28 ; 32 V2; 17 ; 19 ; 23 ; 20 ; 11 ; 10 ; 12 ; 34 ; 21 ; 23 ; 30 ; 12 V3; 18 ; 35 ; 17 ; 10 ; 9 ; 21 ; 23 ; 15 ; 10 ; 0 ; 13 ; 17 V4; 31 ; 45 ; 24 ; 38 ; 41 ; 20 ; 19 ; 37 ; 28 ; 7 ; 15 ; 10 V5; 23 ; 20 ; 23 ; 15 ; 10 ; 22 ; 18 ; 30 ; 28 ; 7 ; 15 ; 10 V6; 22 ; 18 ; 20 ; 19 ; 18 ; 35 ; 0 ; 28 ; 12 ; 30 ; 21 ; 23 |
Attached Files

