Skip to main content

How can I analyse multiple outputs of a model, and compare the results?

Ongoing

Comments

9 comments

  • Jonasz Staszek
    Community Moderator Community Moderator
    Gurobi-versary
    Thought Leader
    First Question

    Hi Alessandro,

    you would need to use some of Gurobi's APIs to encode the model which you pasted. Then, you could use a loop to change the values of alfa1, alfa2 and Beta in your objective function and optimize; afterward, you could store the solutions under the selected alfa1, alfa2 and Beta values in a dictionary or another data structure.

    Since I don't know which Gurobi API you use, I can't give you more guidance here. If you are unfamiliar with Gurobi APIs, here is an excellent introduction to Gurobi with Python. You can also have a look here.

    Best regards
    Jonasz

     

    0
  • ALESSANDRO VILLA
    Gurobi-versary
    First Comment
    First Question

    Thank you Jonasz,

    I am using Python, I've already encoded the model but I don't know how to store the solutions in a dictionary or in another data structure. Once I used this command:

    model.write('out.sol')

    I don't know how to create dictionary or dataframes out of it.

     

    Thanks.

     

    0
  • Jonasz Staszek
    Community Moderator Community Moderator
    Gurobi-versary
    Thought Leader
    First Question

    Hi Alessandro,

    thanks for the clarification. Assuming your Gurobi model is properly defined as model, there are several approaches you could try:

    1. If you need only optimal values for your sensitivity analysis, you would need to initialize an empty dict before your optimizations start, say:

    solutions = {}

    Then, after each optimization run, you could store the optimal value by:

    solution[(alfa1, alfa2, Beta)] = model.getObjective().getValue()

    2. If you need information about each variable, you can initialize the solutions dict as above and then, after each optimization run:

    solution[(alfa1, alfa2, Beta)] = {v.VarName: v.X for v in model.getVars()}

    3. You can also use

    model.write(f'out_{alfa1}_{alfa2}_{Beta}.sol')

    after each optimization run to store the solutions separately for each set of parameters.

    Happy Easter!
    Jonasz

    1
  • ALESSANDRO VILLA
    Gurobi-versary
    First Comment
    First Question

    Hi Jonasz! Thank you for these info.

    More specifically, if alfa=[0,5,10,15], alfa2=[0,5,10,15] and beta=[1,1.1,1.5], I would like just to run every possible combination and compare the outputs. ( for example just comparing the objective function value between different combinations: let's say alfa1= 5 alfa2= 10 and beta=1.5 with alfa1=0, alfa2=10, and beta=1.

    Using model.getObjective().getValue() I will have just the optimal values for minimizing the function, but that is not my case.

    Thanks!

    0
  • Jonasz Staszek
    Community Moderator Community Moderator
    Gurobi-versary
    Thought Leader
    First Question

    Hi Alessandro,

    I am uncertain as to how to help you further here.

    If you need to check all the combinations, you can use three for loops, for example in the following fashion:

    alfas1 = [0,5,10,15]
    alfas2 = [0,5,10,15]
    betas = [1,1.1,1.5]


    for alfa1 in alfas1:
    for alfa2 in alfas2:
    for beta in betas:
    # ...

    Moreover, in the previous post, I gave you three ideas for storing the output of the models for comparison. I would need more information to better understand what is the "output" you need. I will then gladly help you further.

    Best regards
    Jonasz

    0
  • ALESSANDRO VILLA
    Gurobi-versary
    First Comment
    First Question

    Hi Jonasz. Yes with 3 loops I can obtain all the combinations. I think is the right way of doing. 

    But I would like to store somewhere (for every single model in the for cycle), the .sol file ( that have decision variables with optimal values). If there is not a way to store the .sol file, I need to store separately decision variables and their values?

     

    This is an example of .sol file of my model. x is one of the three decision variables, and has 3 indices. i,j,k

    i represent the origin(O), j the destination (D) and k the hub (FROM). Since I need to analyse these indices for my analysis I cannot use the getAttr('X') because it gives me this: ( in this way I cannot recognize the indices)

    [5, 82, 159, 236, 313, 390, 467, 544, 636, 713, 775, 852, 929, 1006, 1085, 1182, 1329, 1483, 1637, 1791, 1868, 1945, 2022, 2099, 2176, 2253, 2330, 2407, 2561, 2715, 2792, 2869, 2946, 3023, 3100, 3177, 3254, 3331, 3408, 3485, 3562, 3646, 3716, 3793, 3877, 3954, 4031, 4108, 4185, 4262, 4339, 4416, 4493, 4647, 4724, 4805, 4882, 4964, 5041, 5118, 5195, 5272, 5349, 5426, 5503, 5580, 5657, 5734, 5811, 5888, 5965, 6042, 6119, 6196, 6273, 6350, 6427, 6504, 6581, 6658, 6735, 6812, 6889, 6966, 7043, 7120, 7197, 7274, 7351, 7428, 7505, 7582, 7659, 7736, 7813, 7890, 7967, 8044, 8121, 8198, 8275, 8352, 8429, 8506, 8660, 8814, 8968, 9045, 9122, 9199, 9276, 9353, 9430, 9584, 9738, 9892, 10046, 10200, 10277, 10354, 10431, 10508, 10585, 10662, 10816, 10893, 10970, 11124, 11201, 11278, 11509, 11663, 11740, 11817, 11863, 11865, 11878, 11885, 11889, 11894, 11951, 11953, 11955, 11957, 11967, 11969, 11994, 12046, 12048, 12050, 12058, 12060, 12062, 12064, 12066, 12074, 12078, 12082, 12083, 12085]
    0
  • Jonasz Staszek
    Community Moderator Community Moderator
    Gurobi-versary
    Thought Leader
    First Question

    Hi Alessandro,

    take a look at my post from 16.04 above. I suggest there how to write separate .sol file for each of your optimization runs. For your convenience, I will paste it again.

    The following line of code after each

    model.write(f'out_{alfa1}_{alfa2}_{Beta}.sol')

    will output a separate sol file for each set of parameters. Its name will suggest which parameters were used for that particular solution.

    Hope this helps!

    Best regards
    Jonasz

    0
  • ALESSANDRO VILLA
    Gurobi-versary
    First Comment
    First Question

    Hi Jonasz! 

    I need another help, would be really kind from you..

    I need to add an INTEGER DECISION VARIABLE called p=[2,4,5...10], with integer values. More specifically, I am referring to the p present in the image in the third constraint of the model. After introducing in the objective function (hierarchically or blended idk) something ike : min  p*10000  I want my model to decide which value should p assume. How I can add this variable? I know there is m.addVar(vtype=GRB.INTEGER) but I don't know how to specify the values..

    And as I said before I would like to bind this choice in the objective function with the value in the third constraint.

    Thanks,

    Alessandro

    0
  • Jonasz Staszek
    Community Moderator Community Moderator
    Gurobi-versary
    Thought Leader
    First Question

    Hi Alessandro,

    is the set p continuous, or does it contain all the integers between 2 and 10?

    If yes, you can just set the appropriate bounds:

    p = m.addVar(vtype=GRB.INTEGER, lb=2, ub=10)

    If not, you will need to make a small trick, which I described here.

    Best regards
    Jonasz

    0

Please sign in to leave a comment.