Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Questions on Python Docplex

    Posted 01/30/19 06:28 PM

    Originally posted by: open_ball


    Hi,

    I have a few questions on modelling the capacitated facility location problem in Python. Since I'm a beginner, some of my questions might be easy to answer. So I read my data in the following way;

    warehouses = [tuple(x) for x in df_warehouses.values]
    customers = [tuple(x) for x in df_customers.values]
    flowcost = dict((tuple((a, b)), c) for a,b,c in df_flowcost.values)

    Then, I create my variables.

    mdl.use = mdl.binary_var_dict(warehouses)

    mdl.ship = mdl.continuous_var_dict(key for key in flowcost)

     

    1) My first question is that if I want to create ship variable by using the lists of warehouses and customers but also want to make sure that warehouse-customer pair exist in flow cost list, how can I do that?

    For instance, in OPL, I can do the following and I'd like to do something similar;

    tuple ship_tuple{

    string warehouse;

    string customer;}

    setof(ship_tuple) shipdf = {<w,c> | w in warehouses, c in customers, f in flowcost : f.warehouse == w && f. customer == c}

     

    2) So, this is question is little more technical. Here how I generate my supply constraints;

    mdl.add_constraints(((mdl.sum(mdl.ship[f] for f in flowcost.keys()) <= w[1]*mdl.use[w]) for w in warehouses)

    If I want to parse warehouse and customer to ship variable as key, how can I accomplish that? I receive an error if I try "mdl.sum(mdl.ship[(w,c)] for c in customers....."

     

    3) Is there any difference (efficiency-wise) creating constraints with an external or internal for loop? So, I am sharing two samples to clarify what I mean by "external" and "internal"

    External (from diet example):

    for n in nutrients:

    amount = mdl.sum(qty[f]*food_nutrients[f.name,n.name] for f in food)

    mdl.add_range(n.qmin, amount,n.qmax)

    Internal (from production example):

    mdl.add_constraints((mdl.inside_vars[prod] + mdl.outside_vars[prod] >= prod[1]) for prod in products)

     

    4) I'd like to write all the constraint into an LP file as we do in OPL. Is there such a thing in docplex or is there a better way to write up the constraints to see how the model is working?

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Questions on Python Docplex

    Posted 02/01/19 10:34 AM

    1. This seems to be a pure Python-related question. If I understand correctly you just want to create a set with all (w,c) pairs that occur in flowcost? That can be done with something like

    shipdf = set((f.warehouse, f.customer) for f in flowcost)

    Or did I miss something here?

    2. At the moment the elements in your index set for ship are of the form ((a, b), c).. That is, they are a tuple, the first element of which is a tuple as well. If you write ship[(w,c)] then w must be a tuple as well, otherwise things will not work. I guess you want to change your index for ship so that it is indexed by pairs (w,c)? It would be good if showed some more code and the actual error you get. Maybe it helps to use classes or objects with properties/attributes instead of tuples. That way you can use names to index things.

    3. I am not sure whether there is a performance difference between the two. I would go with whatever is easier to code/read unless it proves to be a real bottleneck in the process. Having only one function call that adds constraints is probably faster but also more memory-intensive.

    4. Check the reference documentation. There is function Model.export_as_lp() that does exact this.


    #CPLEXOptimizers
    #DecisionOptimization