Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Linear Programming Problem
#1
Question 
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" :

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)]
The entire code :

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")
The result :

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
My "Donnees_prod_table.csv" file :

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
My "Demande_table_table.csv" file :

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
The theorical answer should be 185899.3€.

Attached Files

.csv   Demande_table.csv (Size: 375 bytes / Downloads: 90)
.csv   Donnees_prod_table.csv (Size: 172 bytes / Downloads: 95)
.py   OGP_ExoProdVerres.py (Size: 3.68 KB / Downloads: 104)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Can someone help me solve this programming problem? SuchUmami 6 926 Nov-20-2023, 10:01 AM
Last Post: EdwardMatthew
  SOlving LInear Equations in Python(Symoy, NUmpy) - Coefficient Problem quest 3 1,755 Jan-30-2022, 10:53 PM
Last Post: quest
Lightbulb Object Oriented programming (OOP) problem OmegaRed94 6 2,939 May-31-2021, 07:19 PM
Last Post: OmegaRed94
  python 3 raspberry pi 4 dual control motor programming problem yome 0 1,990 Mar-21-2021, 05:17 PM
Last Post: yome
  Linear Programming (PuLP) - loop for constraints littleangel83 1 3,818 Jun-02-2020, 02:39 PM
Last Post: littleangel83
  Basic programming problem darek88 1 2,009 Sep-30-2019, 01:13 PM
Last Post: ichabod801
  Linear programming optimization PuLP new_in_python 0 28,090 Jul-17-2019, 04:56 PM
Last Post: new_in_python
  Linear programming PySou 1 2,262 May-26-2019, 05:24 PM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020