How to load data as a matrix (using csv file) into python?

Viewed 26

I would like to solve an LPP (Max CX s.to. AX<=B, X>=0) using pyomo, I have loaded the data for C, and B. However I tried to load the matrix A but after solving the model it does not show the correct answer. The code that I tried is in the following lines. Please suggest how to solve this type of problem in pyomo.

from pathlib import Path
from pyomo.environ import *
import pandas as pd
import numpy as np

path_model_in_set = Path('tmp/input/set')
path_model_in_par = Path('tmp/input/par')
path_model_out = Path('tmp/output')
path_results = Path('tmp/output')

m = AbstractModel('LPP')

#Set
m.a = Set(ordered=True, doc="indices for row")
m.b = Set(ordered=True, doc="indices for column")
#Parameters
m.A = Param(m.a, m.b, mutable=True, initialize=0, doc="Matrix")
m.B = Param(m.a, doc="RHS")
m.C = Param(m.b, doc="cost coefficients")
#variable
m.x = Var(m.b, within=NonNegativeReals)

def obj_rule(m):
    return sum(m.C[t]*m.x[t] for t in m.b)
m.object_f = Objective(rule=obj_rule, sense=maximize, doc="Objective function")

def cons_rule(m,i):
    return sum(m.A[i,j]*m.x[j] for j in m.b) <= m.B[i]
m.cons1 = Constraint(m.a, rule=cons_rule, doc="constraints")

data = DataPortal()

data.load(filename=str(path_model_in_set.joinpath('a.csv')), format='set', set='a')
data.load(filename=str(path_model_in_set.joinpath('b.csv')), format='set', set='b')

#the problem is in this line. 
data. Load(filename=str(path_model_in_par.joinpath('Matrix_info.csv')), format='array', index=['A'], param=['1','2','3'])


data. Load(filename=str(path_model_in_par.joinpath('B.csv')), index=['a'], param=['B'])
data.load(filename=str(path_model_in_par.joinpath('C.csv')), index=['b'], param=['C'])

instance = m.create_instance(data)
print(instance.A)
optimizer = 'gurobi'
solver = SolverFactory(optimizer)
instance.write('LPP.lp', io_options={'symbolic_solver_labels': True})
solver_results = solver.solve(instance, tee=True)                               
solver_results.write()                                                            
instance.solutions.load_from(solver_results)

CSV files for matrix A is

A 1 2 3

1 2 1 1

2 1 2 1

3 0 0 1

0 Answers
Related