Minimize distance and Maximaze Gain in an adjacency matrix

  adjacency-matrix, list, matrix, python

I’m working with an adjacency matrix were all my node are connected and each one of them have a different ‘Demand’ to cover and I’m trying to choose one point were the distance between them is minimized and the gain is maximazed, currently when the scripts stops the final distance and Demand always changes.

I’m using this points to test the code where the last element of each list is the node’s demand:

('I MUNICIPALIDAD DE PICHILEMU', -34.387141099999994, -72.0044445, 12000000.0)
('Hospital Las Higueras', -36.74052385, -73.10956056654197, 135000000.0)
('DIRECCION DE ABASTECIMIENTO DE LA ARMADA', -33.0248063, -71.6401181, 18720000.0)
('Hospital de Lolol', -34.727836, -71.6440839, 2023000.0)
('Municipalidad de Gorbea', -39.0989346, -72.6724288, 1344538.0)
('HOSPITAL CLINICO HERMINDA MARTIN', -36.609593700000005, -72.09120225984806, 25536000.0)
('SERVICIO SALUD DEL RELONCAVI', -41.4720085, -72.9354126, 220000000.0)
('Ilustre Municipalidad de Los Sauces', -37.9830552, -72.8316749, 3000000.0)
('INSTITUTO DE SEGURIDAD LABORAL', -36.828233049999994, -73.04807725260355, 14285714.0)
('I MUNICIPALIDAD VALDIVIA', -39.81285145, -73.24694649329606, 3100000.0)
('Ilustre Municipalidad de Negrete', -37.5852623, -72.5283418, 10000000.0)
('Hospital Doctor Hernán Henríquez Aravena', -38.73637135, -72.59904533257668, 104794994.0)

This is my adjMAtrix:

[0.0, 145.7, 62.2, 41.7, 175.3, 76.7, 258.6, 151.7, 141.7, 226.8, 122.9, 161.1]
[145.7, 0.0, 205.7, 176.4, 91.1, 113.3, 154.7, 50.9, 7.4, 100.0, 70.3, 86.6]
[62.2, 205.7, 0.0, 59.6, 236.5, 133.7, 319.6, 214.0, 202.3, 289.1, 184.5, 222.1]
[41.7, 176.4, 59.6, 0.0, 187.6, 81.9, 269.3, 172.0, 171.4, 246.5, 138.6, 173.1]
[175.3, 91.1, 236.5, 187.6, 0.0, 105.7, 83.3, 40.8, 85.3, 67.9, 52.8, 14.5]
[76.7, 113.3, 133.7, 81.9, 105.7, 0.0, 187.5, 94.3, 106.6, 166.6, 58.7, 91.2]
[258.6, 154.7, 319.6, 269.3, 83.3, 187.5, 0.0, 114.7, 151.5, 63.8, 136.0, 97.5]
[151.7, 50.9, 214.0, 172.0, 40.8, 94.3, 114.7, 0.0, 44.7, 75.1, 36.2, 35.8]
[141.7, 7.4, 202.3, 171.4, 85.3, 106.6, 151.5, 44.7, 0.0, 98.7, 62.9, 80.1]
[226.8, 100.0, 289.1, 246.5, 67.9, 166.6, 63.8, 75.1, 98.7, 0.0, 108.1, 80.1]
[122.9, 70.3, 184.5, 138.6, 52.8, 58.7, 136.0, 36.2, 62.9, 108.1, 0.0, 39.1]
[161.1, 86.6, 222.1, 173.1, 14.5, 91.2, 97.5, 35.8, 80.1, 80.1, 39.1, 0.0]

there’s must be something wrong with my logic, what i’m trying to do is compare each row of the matrix , check how much distance that point covers and how much demand it has, here is my code:

con_points = {}
for row,i in enumerate(adjMatrix):
    actual_points = {'Distance' : 0, 'Nodes' : 0, 'Demand' : 0}
    print("************************************************")
    for column, j in enumerate(adjMatrix):
        print(adjMatrix[row][column])
        actual_points['Distance'] += adjMatrix[row][column]
        if adjMatrix[row][column] != 0:
            actual_points['Nodes'] += 1
            actual_points['Demand'] += final_list[column][3]
    # print(actual_points)
    if not con_points:
        con_points = actual_points.copy()
    elif con_points['Distance'] > actual_points['Distance'] and con_points['Demand'] < actual_points['Demand']:
        con_points = actual_points.copy()
    else:
        con_points = actual_points.copy()        

    print(con_points)


print("Final node: ",con_points)   

Source: Python Questions

LEAVE A COMMENT