TransWikia.com

Error when trying to minimize a function with scipy

Stack Overflow Asked on December 28, 2020

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],0)
    p2 = (0,pointsandflow[1])
    cost_p1 = np.array([alpha*xi[i][0]*np.sqrt( (z[0]-p1[0])**2 + (z[1]-p1[1])**2) for z in clientes for i in range(2,puntosyflujos)]).sum()
    cost_p2 = np.array([alpha*xi[i][1]*np.sqrt( (z[0]-p2[0])**2 + (z[1]-p2[1])**2) for z in clientes for i in range(2,puntosyflujos)]).sum()
    total_cost = cost_p1 + cost_p2
    return total_cost

''' 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[0]**2 + x[1]**2 - M**2 }, #first restriction
        {'type': 'ineq', 'fun': supply[0]+oferta[1]-sum(demand)}, #second restriction
        {'type': 'eq','fun': lambda x: x[2][0]+x[2][1]-demand[0]},
        {'type': 'eq','fun': lambda x: x[3][0]+x[3][1]-demand[1]},
        {'type': 'eq','fun': lambda x: x[4][0]+x[4][1]-demand[2]},
        {'type': 'eq','fun': lambda x: x[5][0]+x[5][1]-demand[3]},
        {'type': 'eq','fun': lambda x: x[6][0]+x[6][1]-demand[4]},
        {'type': 'eq','fun': lambda x: x[7][0]+x[7][1]-demand[5]},
        {'type': 'eq','fun': lambda x: x[8][0]+x[8][1]-demand[6]},
        {'type': 'eq','fun': lambda x: x[9][0]+x[9][1]-demand[7]},
        {'type': 'eq','fun': lambda x: x[10][0]+x[10][1]-demand[8]},
        {'type': 'eq','fun': lambda x: x[11][0]+x[11][1]-demand[9]},
        {'type': 'eq','fun': lambda x: x[12][0]+x[12][1]-demand[10]},
        {'type': 'eq','fun': lambda x: x[13][0]+x[13][1]-demand[11]},
        {'type': 'eq','fun': lambda x: x[14][0]+x[14][1]-demand[12]},
        {'type': 'eq','fun': lambda x: x[15][0]+x[15][1]-demand[13]},
        {'type': 'eq','fun': lambda x: x[16][0]+x[16][1]-demand[14]},
        {'type': 'eq','fun': lambda x: x[17][0]+x[17][1]-demand[15]},
        {'type': 'eq','fun': lambda x: x[18][0]+x[18][1]-demand[16]},
        {'type': 'eq','fun': lambda x: x[19][0]+x[19][1]-demand[17]},
        {'type': 'eq','fun': lambda x: x[20][0]+x[20][1]-demand[18]},
        {'type': 'eq','fun': lambda x: x[21][0]+x[21][1]-demand[19]}) #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 '''
----> 3 sol = resultados(clientes,demanda,oferta,M,alpha,params_0)

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.
Thanks in advance.

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP