Hi all, I am a newbie to Python and even more in Optimization procedures, libraries and solvers like Pulp and Gurobi. Although, I succeeded to set up an optimization algorithm in Pulp, which minimizes the sum of certain Numpy array values and I'm trying to import some spatial constraints. In particular:
I have a 2D numpy array with the following values e.g:
[[ 1 -2 -4 -3 1]
[ 1 0 -1 -2 3]
[-100 2 4 3 0]
[ 1 -2 4 -3 3]
[ 1 2 1 3 1]
[ 2 6 0 3 1]]
I use Pulp's minimization function (pulp.LpMinimize) to allocate 1 or more Min values. The total amount of cells that are required is pre-defined as TOTAL_CELLS = 1,2,3....n. A small sample of my code is:
#Dictionary with all cell indexes (1-30) and all cell values
dictionary = dict(zip(index_list, values))
#MIP Model
prob = plp.LpProblem("Cells_Allocation", plp.LpMinimize)
#The problem variables are created in binary mode
use_vars = plp.LpVariable.dicts("UseCell", index_list, 0, 1, plp.LpBinary)
#Objective
prob += plp.lpSum(dictionary[i][1] * use_vars[i] for i in index_list)
#Constraint of total cells required (use_var[].varValue is 0 or 1 for every single cell)
prob += plp.lpSum(use_vars[i] for i in index_list) == TOTAL_CELLS
Until now, my code returns just one or more Min values (e.g for TOTAL_CELLS = 2 values -100 and -4 are optimal).
My question is how can I add a spatial connectivity constraint in order to be able to identify Minimum connected or interconnected values?
For example, if requested TOTAL_CELLS is 2, optimization procedure must return values -100 and -2 that are connected and even more difficult if TOTAL_CELLS is 3 or more. In particular, if TOTAL_CELLS = 3, optimal values are -100, 0 and -4 (that are interconnected) etc. My initial thought was to create a neighbors connectivity dictionary where for every dict key (1-30) i have in sub-lists all neighbors available for every cell but i "stucked" as i cannot realize how can i add it as a constraint (if it is possible) through Pulp and Gurobi?
Thank you in advance for your help