Package: pysmps
pysmpsThis is a utility script for parsing MPS and SMPS file formats. It offers two main functionsload_mpsfor loading mps files andload_smpsfor loading smps file directory.load_mpsTheload_mps(path)method takes apathvariable as input. It should be a .cor or .mps file.
It opens the file with read-permissions and parses the described linear program into the following format:name: The name given to the linear program (can't be blank)objective_name: The name of the objective function valuerow_names: list of row namescol_names: list of column namestypes: list of constraint type indicators, i.e. either "E", "L" or "G" for equality, lower/equal or greater/equal constraint respectively.c: the objective function coefficientsA: the constraint matrixrhs_names: list of names of right hand sides (there can be multiple right hand side components be defined, seldom more than one though)rhs: dictionary(rhs_name) => b, wherebis the vector of constraint values for that given right hand side name.bnd_names: list of names of box-bounds (seldom more than one)bnd: dictionary(bnd_name) => {"LO": v_l, "UP": v_u}wherev_lis the vector of lower bounds andv_uis the vector of upper bound values (defaults tov_l = 0andv_u = +inf).Finally this corresponds to the linear programminc*xs.t.foreachrhs_namewithcorrespondingb:A[types=="E",:]*x=b[types=="E"]A[types=="L",:]*x<=b[types=="L"]A[types=="G",:]*x>=b[types=="G"]foreachbnd_namewithcorrespondingv_landv_u:v_l<=x<v_uload_smpsThis function makes use of theload_mpsfunction for parsing the .cor file. The SMPS file format consists of three files, a .cor, .tim and .sto file. The .cor file is in MPS format. Further the function expects a parameterpathto be such thatpath + ".cor"is the core file,path + ".tim"the time file andpath + ".sto"is the stochastic file.
Itdoes notsupport scenarios yet!
It returns a stochastic multi-stage problem in the following formatname: name of the program (must be the same in all 3 files)objective_name: name of the objective function valueconstraints: list of tuples(name, period, type)for each constraint. It gives a name, a period in which the constraints appears and a type, i.e. "E", "L" or "G" as in MPS.variables: list of tuples(name, period, type)for each variable. It defines a name and a period in which the variable joins the program.typedenotes a string that is either integral or continuous depending on whether the INTORG MARKER was set when instantiating the variable.c: vector of objective function coefficients (of all periods)A: matrix of constraint coefficients (of all periods)rhs_names: list of rhs names as in MPSrhs: dictionary as in MPSbounds: dictionary as in MPSperiods: list of all periods appearing.len(periods)is the number stages.blocks: dictionary ofBlock,LinearTransformorSubRoutineobjects. Dependent on what the .sto file defined.Blocksare independent random variables (every case of aBlockmust be combined with each case of anotherBlockto get all possible appearences; the probabilities multiply),LinearTransformare linear transformations of continuous random variables. The user needs to write the sample script on his own.SubRoutineis a left-out in the file; it presupposes the user to know what to do with these values.independent_variables: dictionary((i,j)) => {position, period, distrib}, where(i,j)is the tuple of row/column indices. If one of them is-1this means that it's either an objective value or a rhs-value respectively.positionis a dictionary adapting to where the entry is (objective value, rhs value or matrix value),perioddefines the period in which this variable is stochastic,distribis either a definition of a continuous random variablesdistrib:{type:"N(mu, sigma**2)"/"U(a, b)"/"B(p, q)"/"G(p, b)"/"LN(mu, sigma**2)",parameters}where parameters is a dictionary defining the required parameters. In the discrete case it is a list of tuples(v,p), wherevis the value of this position andpis the probability of it appearing.For an example on how to use this format i recommand looking at the code forload_2stage_problem.load_2stage_problemLoads a SMPS directory and tries to bring it into a 2-staged stochastic linear program with fixed recourse. Output is a dictionary containing the valuesc: first stage objective function valueA: first stage (equality) constraint coefficient matrixb: first stage constraint valuesq: list of second stage objective function coefficients (each case one entry)h: list of second stage constraint values (each case one entry)T: list of second stage constraint values for deterministic variables (each case one entry)W: recourse matrix (since it's fixed recourse this is not a list)p: list of probabilities for each caseThe constellations in which(q,h,T,W)appear are the realizations given by(q[k], h[k], T[k], W).
The problem then resembles one of the formminc*x+E_p[q*y]s.t.A*x=bT*x+W*y=hx,y>=0which is a formal expression since T and h are also stochastic. In fact this notation means we assert the stochastic constraints inside of the expectation, making it a function of x only.For casting the SMPS files into such a form we need to make certain assertments:The upper right matrix needs to be zeroes only.We only have one righthand side defined (len(rhs_names) == 1).There are no boundaries or if we defined some they are the default values.The first period parsed from the time file is the deterministic one, the other one is the stochastic one (especially there can only be two periods).AandWare not stochastic.This script however doesconvert inequality constraints (deterministic and stochastic) into equality constraints by adding slack variables at the right placescalculate all combinations of independent accurances of stochastic components (BLOCKS and INDEP)calculate the probabilities as products of independent elementary probabilities alongside.