0

I have an optimization problem with the dat frame as below:

Loc Lattitude  Longtitude  Car  Distance
1   34.30      -36.30      A    23
2   36.06      -67.32      B    32
3   41.35      -22.04      C    12
4   30.49      -14.25      A    41  
5   42.60      -36.60      C    11

The Car column is the variable I try to optimize - each location is assigned with a car, the objective is to minimize the total number of cars, i.e. the unique value of the Car variable.

The constraint is that the max value in the Distance column has to be <=40. The values in the distance column is the distance from the corresponding location to the centroid point of all the locations assigned with the same car. To simplify the code:

df['Distance'] = df.groupby('Car').centroid_distance(['Lattitude','Longtitude'])

How can I do the optimization with cvxopt or cxvpy, please? All I could find from online are simple/linear functions of variable x. I cannot figure how to optimize a variable that involves group by function. I tried to assign the variable(x) as a column of the data frame but it doesn't work.

Grace
  • 75
  • 11
  • I don't know cvxopt or cvxpy but I think the problem is that the centroid point changes depending on the current assignments of the cars. So the centroid coordinates need to be variables as well. And you need different variables for each car. Then the distance is an expression in the variables corresponding to the centroid point of the car that is assigned to the location. – Daniel Junglas Nov 26 '19 at 08:22
  • Right. The Distance is calculated based on $Car, $lattitude and $longtitude. I calculate the centroid point for each cluster (each cluster has all the locations with lattitude and longtitude assigned with the same car) and then the distance of each point to the centroid. I'm good with that part for a regular data frame calculation, but since the calculate involves grouping (groupby) function, I don't know how to implement the 'grouping' of the optimizing variable (Car in this case). – Grace Nov 26 '19 at 17:41
  • Could you elaborate on what exactly the issue is? I am not clear whether you want to do this grouping while or after solving the problem. If the goruping is supposed to be part of the optimization problem then I don't see how it can be done using data frames since the groups itself are subject to optimization. – Daniel Junglas Dec 09 '19 at 08:05
  • You might need an optimizer that work with black-box objective functions like scipy minimize ... – Komov Nov 11 '20 at 04:32

0 Answers0