Usage of user specific matrix A in LP as an input for GLPK solve

41 views
Skip to first unread message

Tim Prokopenko

unread,
Aug 22, 2024, 8:23:14 AM8/22/24
to Pyomo Forum
Hello all,
I need to use GLPK for solving linear programming problems in my research. I already have the matrix . I would be interested to know if there is a way to input the matrix A into Pyomo directly. If not, I would be very grateful to the developers who could implement such a feature, as it would double my efficiency in working with the code and likely benefit many people in science and industry. What are your thoughts on this?

Ahmad Heidari

unread,
Mar 17, 2025, 5:09:50 PMMar 17
to Pyomo Forum
Hello Tim,

I hope you are doing well.

What do you mean by matrix? 

If you are going to import some input data, you can directly use the pandas library through Pyomo. For example, the first code below imports the data for the generating units' parameters. If you are importing the matrix, say,  A=[1 2 3;4 5 6;7 8 9], you can use numpy library as the second example, as well.

#----------------------------------------First Example---------------------------------------------------

# Load the Excel file into a pandas DataFrame --> Generators' Input Data

excel_file = 'your_excel_file.xlsx'  # Replace with the path to your Excel file
df              = pd.read_excel(excel_file, sheet_name='your_sheet_name', index_col=0)

# Strip any extra spaces from the index
df.index = df.index.str.strip()

# Extract parameters from the DataFrame
data = {
    'A'      : df['A'].to_dict(),
    'B'      : df['B'].to_dict(),
    'C'      : df['C'].to_dict(),
    'costSD' : df['costSD'].to_dict(),
    'costST' : df['costST'].to_dict(),
    'RU'     : df['RU'].to_dict(),
    'RD'     : df['RD'].to_dict(),
    'UT'     : df['UT'].to_dict(),
    'DT'     : df['DT'].to_dict(),
    'Pmin'   : df['Pmin'].to_dict(),
    'Pmax'   : df['Pmax'].to_dict(),
    'Uini'   : df['Uini'].to_dict(),
    'FP'     : df['FP'].to_dict()
}

mp.A      = Param(mp.I, initialize=data['A'])
mp.B      = Param(mp.I, initialize=data['B'])
mp.C      = Param(mp.I, initialize=data['C'])
mp.costSD = Param(mp.I, initialize=data['costSD'])
mp.costST = Param(mp.I, initialize=data['costST'])
mp.RU     = Param(mp.I, initialize=data['RU'])
mp.RD     = Param(mp.I, initialize=data['RD'])
mp.UT     = Param(mp.I, initialize=data['UT'])
mp.DT     = Param(mp.I, initialize=data['DT'])
mp.Pmin   = Param(mp.I, initialize=data['Pmin'])
mp.Pmax   = Param(mp.I, initialize=data['Pmax'])
mp.Uini   = Param(mp.I, initialize=data['Uini'])
mp.FP     = Param(mp.I, initialize=data['FP'])

#---------------------------------Second Example --------------------------------------------

import numpy as np
from pyomo.environ import *

# Your matrix
A = np.array([[1, 2, 3],
              [4, 5, 6],
              [7, 8, 9]])

model = ConcreteModel()

# Define index sets for rows and columns
model.I = RangeSet(A.shape[0])
model.J = RangeSet(A.shape[1])

# Import matrix into a Pyomo Param
model.A = Param(model.I, model.J, initialize=lambda model, i, j: A[i-1, j-1])

model.A.pprint()
Reply all
Reply to author
Forward
0 new messages