Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Modify imported Model

    Posted 07/17/15 04:37 AM

    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


  • 2.  Re: Modify imported Model

    Posted 07/17/15 07:58 AM

    I am not a Visual Basic expert but at first glance your code looks correct. In particular, I don't see why AddEq() would create new variables. Could you export to *.lp instead of *.mps and then post the input (without pwls) and the output (with the added pwl) LP files here? Maybe reduce ntime to 1 if that is possible to keep the files smaller.

    Piecewise linear functions are not supported in the LP file format. Therefore, when you export your model to LP then pwls are transformed into a sequence of additional variables and constraints (potentially including some SOS constraints). So you cannot write a pwl directly into an LP file. Instead you have to write the "transformed" constraint to the LP file.

    How do you actually create the input LP file? Is that an application that does not use CPLEX? Would it be possible to change that application so that it uses CPLEX to produce the LP file? That would make things considerably simpler.


    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: Modify imported Model

    Posted 07/17/15 08:54 AM

    Originally posted by: a.teuffel


    Thx a lot for your response. Now I know for sure I can't add PWL straight to a *.lp-file without using "transformed" constraints. Loading he model and modifying seems to be the right way at the moment.

    I use a GUI in Visual Basic which is used to create models. The user can add model-components by entering equations to .txt-files. Visual Basic  reads those *.txt-files, combines them into one file and formats all the equations, adds bounds etc. so I can save it as an *.lp-file.

    It is quite inconvenient since I know one could create the model in Visual Basic using CPLEX but it has been this way for ages using other optimizers and I don't know how much effort I would have to expend to redesign the application.

     

    I created a very simple model and attached the .*lp-files with and without pwls. They both seem to be okay and I get the expected solutions. The only thing which concerns me is the repeating rows...

    Do you have any idea how to avoid this?

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: Modify imported Model

    Posted 07/18/15 06:51 AM

    Do you need meaningful variable/constraint names in the final exported LP/MPS file? If not then the easiest thing would be to just call IloCplex.DeleteNames() right before exporting. This will implicitly rename all objects for exporting and this implicit renaming will avoid any duplicates.


    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: Modify imported Model

    Posted 07/20/15 05:04 AM

    Originally posted by: a.teuffel


    Yes, I need meaningful variable names. Constraint names are not important


    #CPLEXOptimizers
    #DecisionOptimization


  • 6.  Re: Modify imported Model

    Posted 08/11/15 02:27 AM

    I have looked deeper into this issue and actually there is a bug in the CPLEX code that creates the names for those transformed PWL constraints. This bug (which will be fixed in the next release/fixpack) implies that unfortunately you will always have duplicate names in your model files.

    The best workaround would be to use SAV files instead of LP files. SAV files are better anyway since they don't change order of variables/constraints or truncate numbers on input/output, as text files do. Moreover, when reading SAV files, CPLEX does not need to look up variables or constraints by names, hence duplicate names are not an issue. Not sure if you can switch your application to SAV files since that is a binary format.

    When reading LP files, duplicate constraint names should not matter (unless you code later attempts to look up constraints by name) and warnings about duplicate row names can safely be ignored.

    Finally, one thing you could do is post-process the LP file and remove any constraint name from it (constraints names in LP files are optional).


    #CPLEXOptimizers
    #DecisionOptimization


  • 7.  Re: Modify imported Model

    Posted 08/14/15 09:21 AM

    Originally posted by: a.teuffel


    Thx for your reply! I think I'll go with post-processing the .lp-files.


    #CPLEXOptimizers
    #DecisionOptimization