Decision Optimization

Decision Optimization

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

 View Only
Expand all | Collapse all

Retrieve Solution Values in Batches at DoCplex Python

  • 1.  Retrieve Solution Values in Batches at DoCplex Python

    Posted Fri June 01, 2018 12:11 PM

    Originally posted by: ashley20


    Hi all,

     

    I defined my variables in dictionaries, model.continuous_var_dict(). When I retrieve solution values one by one by using 

    sol = model.solve(url=None, key=None)

    for a in NumA:

          sol.get_value(vars[a]) 

    It works well. 

     

    However, I want to retrieve solution values in batches. I tried to use the function get_values(),

    sol.get_values(vars)

    but it does not work. Probably it only works for vars defined as list. 

    I have some variables defined as 1-dimension, 2-dimension and 3-dimension dictionaries, retrieve values in batches would be good, rather than writing multiple loops. Is there a way I can retrieve solution values in batches for these variables defined as multi-dimension dictionaries?

     

    Thank you!

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Retrieve Solution Values in Batches at DoCplex Python

    Posted Sat June 02, 2018 02:05 AM

    get_values() only works for sequences. From the documentation:

     |  get_values(self, dvars)
     |      Gets the value of a sequence of variables in a solution.
     |      If a variable is not mentioned in the solution,
     |      the method assumes 0 and does not raise an exception.
     |      
     |      Args:
     |          dvars: an ordered sequence of decision variables.
     |      
     |      Returns:
     |          list: A list of float values, in the same order as the variable sequence.

    Can you please try get_value_dict():

     |  get_value_dict(self, var_dict, keep_zeros=True, precision=1e-06)

    It returns a dictionary with the same keys as 'var_dict' and the value for key k in this dictionary is the solution value for var_dict[k]. If 'keep_zeros' is False then the function removes from the result all keys for which the absolute value of the respective value is below 'precision'.


    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: Retrieve Solution Values in Batches at DoCplex Python

    Posted Sun June 03, 2018 02:14 PM

    Originally posted by: ashley20


    Hi Daniel,

     

    Thank you for your quick response! The function get_value_dict() works well.

     

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: Retrieve Solution Values in Batches at DoCplex Python

    Posted Sun June 03, 2018 02:15 PM

    Originally posted by: ashley20


    Hi Daniel,

     

    Thank you for your quick response! The function get_value_dict() works well.

     

     


    #CPLEXOptimizers
    #DecisionOptimization