Decision Optimization

 View Only
  • 1.  Trouble passing in a float object (PyCPLEX)

    Posted Thu January 12, 2023 02:23 PM
    Edited by System Fri January 20, 2023 04:10 PM

    Hello! I'm trying to develop a facility location model where I have to optimize the assignment of agents to a specific site by minimizing the distance between the agents and site locations. I attached my script and the data set. When I try running my model I receive the following traceback message. Any help is appreciated, thanks!

    Traceback (most recent call last):
    File "/Users/manny/Documents/Operations Research/Model.py", line 31, in <module>
    x = [[model.variables.add(obj=distance[i][j], lb=0, ub=1, types='B') for j in range(m)] for i in range(n)]
    File "/Users/manny/Documents/Operations Research/Model.py", line 31, in <listcomp>
    x = [[model.variables.add(obj=distance[i][j], lb=0, ub=1, types='B') for j in range(m)] for i in range(n)]
    File "/Users/manny/Documents/Operations Research/Model.py", line 31, in <listcomp>
    x = [[model.variables.add(obj=distance[i][j], lb=0, ub=1, types='B') for j in range(m)] for i in range(n)]
    File "/opt/anaconda3/lib/python3.9/site-packages/cplex/_internal/_subinterfaces.py", line 458, in add
    return self._add_iter(self.get_num, self._add,
    File "/opt/anaconda3/lib/python3.9/site-packages/cplex/_internal/_baseinterface.py", line 41, in _add_iter
    addfun(*args, **kwargs)
    File "/opt/anaconda3/lib/python3.9/site-packages/cplex/_internal/_subinterfaces.py", line 373, in _add
    num_new_cols = max_arg_length(arg_list)
    File "/opt/anaconda3/lib/python3.9/site-packages/cplex/_internal/_aux_functions.py", line 77, in max_arg_length
    return max([len(x) for x in arg_list])
    File "/opt/anaconda3/lib/python3.9/site-packages/cplex/_internal/_aux_functions.py", line 77, in <listcomp>
    return max([len(x) for x in arg_list])
    TypeError: object of type 'float' has no len()


    ------------------------------
    Manuel Lopez-Mejia
    EPM Data Engineer
    IBM
    NC
    ------------------------------
    #DecisionOptimization


  • 2.  RE: Trouble passing in a float object (PyCPLEX)

    Posted Thu January 12, 2023 05:13 PM
    Hello,
    For information, the attached python model attempts to load a `capacity.csv` that is missing from the attachements, to be able to replicate the issue.
    Also, line numbers in `Model.py` do not match exactly the ones in the log that is reported in the message. The attached files may not correspond to the latest version.

    The exception reported in the logs is raised because the `model.variables.add(...)` method expects lists as parameters, but primitive types are provided instead.

    Also, have you considered using the docplex libraries instead of the Cplex python API for developping your model ?
    docplex provides a richer and more user-friendly API.

    Best regards,

    Hugues

    ------------------------------
    Hugues Juille
    ------------------------------



  • 3.  RE: Trouble passing in a float object (PyCPLEX)

    Posted Fri January 13, 2023 11:59 AM

    Thank you @Hugues Juille, I rewrote my script in DOcplex and it was too large! (I'm using the free version) but I'm assuming it does work now. I attached it below along with the capacity csv.



    ------------------------------
    Manuel Lopez-Mejia
    Fulfillment Contract and Solutions Professional
    IBM
    NC
    ------------------------------