Error when trying to minimize a function with scipy

I’m trying to solve the following problem:
Problem to minimize, where (pkx,pky) represent the point where the producer k is on the map, alpha is a constant, xki is the flow of product from the producer k to the client i, (ai,bi) is the position of the client i on the map, Qk is the supply from the producer k and Di is the demand from the client i. This is modelled on a cartesian plane, and one supplier has to be on the x axis and the other on the y axis.

So I import numpy, scipy and have the following code:
clients = [(-1000,-200),(-800,500),(-750,200),(-600,300),(-550,600),(-500,-200),(-400,-300),(-400,-400),(-350,-100),(-200,100),(-100,700),(100,-100),(200,100),(300,500),(400,-100),(500,400),(600,700),(700,-50),(800,800),(900,900)] #this is the position of each client on the map

demand= [100, 50, 10, 10, 50, 50, 150, 150, 100, 20, 20, 50, 20, 50, 50, 100, 60, 20, 50, 100] #the demand from each client

supply=[600, 800] #supply from each producer

M=500

alpha= 0.5

I then define a function to solve this problem,

def results(clients, demand, supply, M, alpha, params_0):
''' Function to minimize '''
def total_costs(pointsandflow): #pointsandflow=[point1x, punto2y, (flow from producer 1 to client 1,flow from producer 2 to client 1), (flow from producer 1 to client 2,flow from producer 2 to client 2),etc...]
p1 = (pointsandflow,0)
p2 = (0,pointsandflow)
cost_p1 = np.array([alpha*xi[i]*np.sqrt( (z-p1)**2 + (z-p1)**2) for z in clientes for i in range(2,puntosyflujos)]).sum()
cost_p2 = np.array([alpha*xi[i]*np.sqrt( (z-p2)**2 + (z-p2)**2) for z in clientes for i in range(2,puntosyflujos)]).sum()
total_cost = cost_p1 + cost_p2

''' Restrictions '''
bounds = ((-1000,1000),(-1000,1000),((0,100),(0,100)),((0, 50),(0, 50)),((0,10),(0,10)), ((0,10),(0,10)), ((0, 50),(0, 50)), ((0,150),(0,150)),((0,150),(0,150)), ((0,100),(0,100)), ((0,20),(0,20)), ((0,20),(0,20)),((0, 50),(0, 50)), ((0,20),(0,20)), ((0, 50),(0, 50)), ((0, 50),(0, 50)), ((0,100),(0,100)), ((0,60),(0,60)), ((0,20),(0,20)),((0, 50),(0, 50)),((0,100),(0,100)))
cons = ({'type': 'ineq', 'fun': lambda x: x**2 + x**2 - M**2 }, #first restriction
{'type': 'ineq', 'fun': supply+oferta-sum(demand)}, #second restriction
{'type': 'eq','fun': lambda x: x+x-demand},
{'type': 'eq','fun': lambda x: x+x-demand},
{'type': 'eq','fun': lambda x: x+x-demand},
{'type': 'eq','fun': lambda x: x+x-demand},
{'type': 'eq','fun': lambda x: x+x-demand},
{'type': 'eq','fun': lambda x: x+x-demand},
{'type': 'eq','fun': lambda x: x+x-demand},
{'type': 'eq','fun': lambda x: x+x-demand},
{'type': 'eq','fun': lambda x: x+x-demand},
{'type': 'eq','fun': lambda x: x+x-demand},
{'type': 'eq','fun': lambda x: x+x-demand},
{'type': 'eq','fun': lambda x: x+x-demand},
{'type': 'eq','fun': lambda x: x+x-demand},
{'type': 'eq','fun': lambda x: x+x-demand},
{'type': 'eq','fun': lambda x: x+x-demand},
{'type': 'eq','fun': lambda x: x+x-demand},
{'type': 'eq','fun': lambda x: x+x-demand},
{'type': 'eq','fun': lambda x: x+x-demand},
{'type': 'eq','fun': lambda x: x+x-demand},
{'type': 'eq','fun': lambda x: x+x-demand}) #third restriction

sol = minimize(total_costs,x0=params_0,bounds=bounds,
method='SLSQP',constraints = cons,options={"maxiter":500})

return sol

which gives the following error when I run this:

params_0= [0,0,(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)]
sol = results(clients,demand,supply,M,alpha,params_0)

---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

TypeError: float() argument must be a string or a number, not 'tuple'

The above exception was the direct cause of the following exception:

ValueError                                Traceback (most recent call last)

<ipython-input-27-24abfb09057c> in <module>()
1 params_0= [0,0,(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0...
2 ''' Solucion '''

4 frames

<__array_function__ internals> in asfarray(*args, **kwargs)

/usr/local/lib/python3.6/dist-packages/numpy/core/_asarray.py in asarray(a, dtype, order)
83
84     """
---> 85     return array(a, dtype, copy=False, order=order)
86
87

ValueError: setting an array element with a sequence.

I don’t know how to fix this error or what I have to change in order to code correctly the problem.

Stack Overflow Asked on December 28, 2020

Related Questions

I am getting error while while using “zappa init’

1  Asked on November 26, 2020 by dwipal-shrirao

I have got two (.py) files. When the first program comes to end, it will close itself then open and run the second program file. How can ı do it?

1  Asked on November 26, 2020 by mer-can

What should i select layer for fine-tuning on Xception?

0  Asked on November 26, 2020 by donghwan-shin

Can not put a T in Map

1  Asked on November 25, 2020 by hemant

‘Aspnetuserlogins’ requires a primary key to be defined (interconnected databases)

1  Asked on November 25, 2020 by bubinga

Display multiple same values only once while others are looping using ngFor in Angular

1  Asked on November 24, 2020

Xcrud button to search for predefined word and predefined column

1  Asked on November 24, 2020

How to insert member list with menu driven prolog program?

0  Asked on November 24, 2020 by fury

Pyspark Transpose

1  Asked on November 23, 2020 by sawan-s

How to aggregate data, pass and fail as final result?

1  Asked on November 23, 2020 by user584018

How can I convert an object string to an object

1  Asked on November 22, 2020 by insberr

Comparing two lists for same value

1  Asked on November 22, 2020 by sebaku

Crystal Reports won’t display related table data

2  Asked on November 22, 2020 by rharris

How do I create an AWS Lambda layer for seaborn?

2  Asked on November 22, 2020 by joe-carter

Upgrading spring cloud version from Edgware to Hoxton

3  Asked on November 21, 2020 by ruchira-gayan-ranaweera

Flatlist maintainVisibleContentPosition causes instant crash on iOs

2  Asked on November 21, 2020 by ryan-pergent

How to prevent button form Angular?

5  Asked on November 20, 2020 by user13976004

SQL union seems to fail for me

1  Asked on November 19, 2020 by sasha-peric

My Projectiles Stop Shooting Problem How To Fix?

1  Asked on November 19, 2020 by habib-ismail