Decision Optimization

Decision Optimization

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

 View Only
Expand all | Collapse all

LP Optimization problem with time dependent variables

  • 1.  LP Optimization problem with time dependent variables

    Posted Thu March 30, 2017 10:31 AM

    Originally posted by: a.teuffel


    Hi everyone,

    I have a LP optimizing problem which I create using the lp-file format and then I optimize in an .NET application.

    The problem has man different variables, of which some are time dependent. Let's say one variable T is the temperature, which varies over time.

    Assuming I try to formulate an optimization problem over a time period of 3 hours and I use time steps of 1 hour, I get 3 variables T1, T2, T3 in the .lp-file.

     

    Now if I have many different time dependent variables and long time periods I get many many variables and big lp-files.

     

    Is there any possibility to get CPLEX to handle these variables as array etc? So in the end I only have one array for each time dependent variable T(t) with T(1), T(2), T(3) etc?

     

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: LP Optimization problem with time dependent variables

    Posted Fri March 31, 2017 12:35 AM

    The LP file format does not have any notion of arrays.

    I am not clear what your want to do. In the LP file it does not really matter whether you write T1 or T(1). Are you looking for a convenient way to store the variables in an array when reading the model into your .NET application? Or are you looking for a way to state your constraints in a more abstract way like "forall (t in periods) T[t] + T[+1] <= r"?

    In any case, wouldn't it be better to build the models directly in .NET?


    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: LP Optimization problem with time dependent variables

    Posted Fri March 31, 2017 04:50 AM

    Originally posted by: a.teuffel


    Hi Daniel,

    thanks for your repy! Maybe I don't know how to exactly describe what I need. You are right for the lp-file it doesn't make any difference if I have T1 or T(1).

    The main problem I encounter is when I try to extract all variable values of the solution in .net. I guess I am not aware of the full functionalities of CPLEX in .net. I only worked through the existing examples and copied some code to suit my needs.

    What I do right now:

    ...
    cplex.ImportModel("problem.lp")
    If cplex.Solve() Then
       Dim matrixEnum1 As IEnumerator = cplex.GetLPMatrixEnumerator() 'not sure what this does
       matrixEnum1.MoveNext() 'not sure what this does
       Dim lp1 As ILPMatrix = CType(matrixEnum1.Current, ILPMatrix) 'not sure what this does
       Dim variables As INumVar() = lp1.NumVars
       solvalues = cplex.GetValues(variables)
    End If
    ...
    

    Now this gives me one array "variables" where I can extract the variable names und one array "solvalues" where I can extract the values of the variables.
    This is where I came up with the question. Instead of having a lot of entries in these arrays with T1, T2,... wouldn't it be nice to have an array for each time dependent variable, T() etc.?

    Now I understand that this doesn't work if I build the models using lp-files.

     

    The reason why I dont build the models directly in .net is beacause, at least with my capabilities, it is faster with .lp-files. Let me try to explain why. I build an application which is used as a kind of modelling environment. The user defines a model in a textfile with a specific langauge. For time dependent problem the user enters a loop statement (for the time steps) and he/she only needs to type in each equation/constraint/bound once in the loop. The application then interprets the loop and creates equations/bounds with T1, T2, etc for as many iterations as needed and finally converts into the lp-file format. The loops can be up to 40000 iterations long. Additionally, sums of time dependent variables from the loops may need to be created. If I have T1, T2,... T40000 this means I need  equations with 40000 summands (Tsum = T1+T2+...T40000).

     

    I tried building the models with cplex in .net. I used the populate by row method from one of the examples:

    First I extract all variables and bounds from the user input text file and then create:

    Dim x As INumVar() = model.NumVarArray(variablesCount, lowerBounds, upperBounds, varnames)
    

    If I then try to build equations with 40000 summands with cplex in .net through

    expr = model.Sum(expr, model.Prod(1, x(v)))
    

    in a loop and finally add the equation with model.AddEq(0, expr) to the model, this takes a lot of time...

    So maybe my question should be: Is there a simple way, when using cplex in .net, to create variable arrays T() and sums of these arrays without having to use the expr = model.Sum(expr, model.Prod(1, x(v))) in loops with 40000 iterations? If yes, how can I do this?

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: LP Optimization problem with time dependent variables

    Posted Fri March 31, 2017 05:48 AM

    Many questions :-) Let me try to clarify/answer.

    First start with this code snippet (I reordered your code a little):

    cplex.ImportModel(...);
    Dim e As IEnumerator = cplex.GetLPMatrixEnumerator();
    e.MoveNext();
    Dim lp As ILPMatrix = CType(e.Current, ILPMatrix);
    Dim v As INumVar() = lp.NumVars

    If cplex imports a model from a file then it stores the problem matrix defined in that file into an instance of ILPMatrix. After calling importModel() the Cplex instance holds exactly one instance of ILPMatrix and that in turn contains the mode read from the file. In theory a Cplex instance can hold more than one ILPMatrix, that is why querying these LP matrices is implemented by getting an enumerator for the list of ILPMatrix instances (GetLPMatrixEnumerator). In this specific case there is however only one instance. You fetch this instance via MoveNext()/Current. The type of the object returned by Current is just a generic object type, hence you need to downcast to ILPMatrix via CType. Once you have a reference to the ILPMatrix instance you use its NumVars field to get a list of all variables in the model.

    It is of course impossible for the Cplex instance to recognize that there was some sort of array relation in the variables. You could reconstruct these arrays in your .NET code by going through the variables in the long array and look at the variable names - provided in the LP file you assigned names that reflect the array.

    For example, if your variables are named T(0), T(1), ..., you could filter all variables that match the pattern "T([0-9]+)" and put them into an appropriate array in your code.

     

    In general, it should be faster to build up the model in your application instead of taking a detour through LP files. Assuming you want to build an expression

    a*x + b*y + c*z

    with a, b, c being numbers and x, y, z being variables, then the slowest way to do this is

    expr = model.Prod(a, x);
    expr = model.Sum(expr, model.Prod(b, y));
    expr = model.Sum(expr, model.Prod(c, z));

    Something that is faster (especially for large expressions) is

    expr = model.LinearNumExpr();
    expr.AddTerm(a, x);
    expr.AddTerm(b, y);
    expr.AddTerm(c, z);

    If you have coefficients and variables in an array then you can also use model.ScalProd(). You may check whether model building works any faster with these methods.


    #CPLEXOptimizers
    #DecisionOptimization