Decision Optimization

Decision Optimization

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

 View Only
  • 1.  error in writing simple linear expressions, docplex

    Posted Sun February 24, 2019 06:47 PM

    Originally posted by: BahmanBornay


    Hey everybody,

    I am new into writing Optimization codes using docplex library.

    I happened to have an issue in defining the objective and constraints. Considering the simple LP example from documentation (link: https://ibmdecisionoptimization.github.io/tutorials/html/Linear_Programming.html), if we write it this way:

    import numpy as np
    from docplex.mp.model import Model
    
    mdl = Model("Tekephon_Production")
    
    n = 2
    N = [1, n]
    p = [12, 20]
    a = np.array([(0.2, 0.4), (0.5, 0.4)])
    b = np.array([400, 490])
    lb = np.array([100, 100])
    
    
    x = mdl.continuous_var_dict(N, name='x')
    mdl.maximize(12 * x[1] + 20 * x[2])
    
    
    mdl.add_constraint(0.2 * x[1] + 0.4 * x[2] <= 400)
    
    
    
    mdl.add_constraint(0.5 * x[1] + 0.4 * x[2] <= 490)
    
    
    
    mdl.add_constraint(x[1] >= 100)
    mdl.add_constraint(x[2] >= 100)
    
    
    solution = mdl.solve(log_output=True)
    print(solution)
    

    this block of code is solved as expected. But, as we run the following code, errors are popping up:

    
    x = mdl.continuous_var_dict(N, name='x')
    
    
    
    mdl.maximize(mdl.sum(p[i] * x[i] for i in N))
    
    
    mdl.add_constraint(mdl.sum(p[i] * x[i]) <= 400 for i in N)
    
    
    
    mdl.add_constraint(mdl.sum(a[i] * x[i]) <= 490 for i in N)
    
    
    
    mdl.add_constraint(x[1] >= 100)
    mdl.add_constraint(x[2] >= 100)
    
    
    solution = mdl.solve(log_output=True)
    print(solution)
    

     

    I would appreciate your comments. 

     

    Regards,
    Bahman


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: error in writing simple linear expressions, docplex

    Posted Mon February 25, 2019 12:07 AM

    It would a lot if you told us what errors are popping up and if it is syntax errors then from which statement they come.

    This looks suspicious

    
    mdl.maximize(mdl.sum(p[i] * x[i]) for i in N)
    

    I guess what you intended to was instead (note the place of closing parens)

    mdl.maximize(mdl.sum(p[i] * x[i] for i in N))

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: error in writing simple linear expressions, docplex

    Posted Mon February 25, 2019 12:10 AM

    Originally posted by: BahmanBornay


    Hey Daniel,

    I appreciate your prompt reply. However, it doesn't seem to be due to parenthesis since I got these errors

     

     

    Regards,
    Bahman


    #DecisionOptimization


  • 4.  Re: error in writing simple linear expressions, docplex

    Posted Mon February 25, 2019 12:21 AM

    The problem seems unrelated to docplex but looks a basic Python problem. Consider this

    n = 2
    N = [1, n]
    p = [12, 20]
    ...
    x = mdl.continuous_var_dict(N, name='x')
    mdl.maximize(mdl.sum(p[i] * x[i] for i in N))

    The lists p and x both have size 2, that is, valid indices are 0 and 1. Now in your code you reference p[i] and x[i] and you have 'for i in N'. Since i ranges through N it will be 1 and 2. The first value is a valid index, the second is not. I guess what you want is something like 'for i in range(len(N))'.


    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: error in writing simple linear expressions, docplex

    Posted Mon February 25, 2019 01:16 AM

    Originally posted by: BahmanBornay


    Hey Daniel,

     

    I found the issue: the range(len(N)) does not work, since p is indexed from 0 to 1, but x is indexed from 1 to 2. Thus: the following code worked:

    mdl.maximize(mdl.sum(p[i] * x[i+1] for i in range(len(N))))
    

     

    Regards,
    Bahman


    #CPLEXOptimizers
    #DecisionOptimization


  • 6.  Re: error in writing simple linear expressions, docplex

    Posted Mon February 25, 2019 01:56 AM

    Ah, yes, you are right. x is a dictionary rather than a list and the keys are the values from N.

    A more robust way to write your objective would be this

    mdl.maximize(mdl.sum(p[i] * x[n] for i, n in enumerate(N))

    This does not depend on a particular relationship (like 'i' and 'i+1') between the values in N and their indices. It only assumes that the k-th variable is given by x[N[k]].


    #CPLEXOptimizers
    #DecisionOptimization