Originally posted by: a.teuffel
Hi,
I have a Visual Basic Application which builds LP models as *.lp-files. Now I want to integrate piecewise linear functions in my model.
Since I didn't find out how to directly implement that in the *.lp-file I tried to use the piecewiselinear()-function after importing the model in visual basic as followed:
Dim cplex As New Cplex()
cplex.ImportModel(TMPDIR & "tmp.lp")
Dim matrixEnum As IEnumerator = cplex.GetLPMatrixEnumerator()
matrixEnum.MoveNext()
Dim lp As ILPMatrix = CType(matrixEnum.Current, ILPMatrix)
Dim vars As INumVar() = lp.NumVars
Dim xpoints() As Double = {0.0, 1.0, 2.0, 3.0}
Dim ypoints() As Double = {0.0, 0.1, 0.5, 1.2}
Dim ntime As Integer = WertPar("NTime") 'ntime=8760 in this example
Dim vari1 As INumVar
Dim expr1(ntime) As INumExpr
Dim expr2(ntime) As INumExpr
Dim n = 1
' Get all variables I want to use in Piecewiselinear(). In this example: BKPe1, BKPe2, BKPe3,...BKPe8760 and BKPf1, BKPf2, BKPf3,...BKPf8760
Dim query = From vari In vars
Where (InStr(vari.Name, "BKPe") And zahlinstring(vari.Name) > 0 And zahlinstring(vari.Name) < ntime + 1)
Select vari
For Each vari1 In query
expr1(n) = vari1
n = n + 1
Next
n = 1
query = From vari In vars
Where (InStr(vari.Name, "BKPf") And zahlinstring(vari.Name) > 0 And zahlinstring(vari.Name) < ntime + 1)
Select vari
For Each vari1 In query
expr2(n) = vari1
n = n + 1
Next
For i = 1 to ntime
cplex.AddEq(expr1(i), cplex.PiecewiseLinear(expr2(i), 0, xpoints, ypoints, 0.4))
Next
If cplex.solve().....
....
Now this doesn't work that well since I seem to add the variables a second time to the model when using the AddEq(...) . When exporting the problem to *.mps for example I have repeating rows.
My questions are:
Is there a way to directly integrate picewise linear functions in *.lp-files (I know it has to do something with SOS...but I am not familiar with the use of SOS)?
Is it possible to modify an imported model (from a *.lp-file)? If yes, how is it done properly without generating repeating rows?
Thx for your help
Regards, a.teuffel
#CPLEXOptimizers#DecisionOptimization