# Loading environment from analysis notebook
import dill
dill.load_session('BDO_Optimization_env.db')
import numpy as np
import pandas as pd
import re
pd.options.display.max_rows = 6
# https://igraph.org/python/doc/igraph.Graph-class.html#shortest_paths_dijkstra
# https://igraph.org/python/doc/igraph.GraphBase-class.html#get_shortest_paths
<igraph.Graph at 0x185963f5228>
g.vs[0]
igraph.Vertex(<igraph.Graph object at 0x00000185963F5228>, 0, {'name': 'Abandoned Iron Mine', 'area': 'Mediah', 'type': 'Worker Node', 'cp': 2})
# Takes in list as input (i.e. get_node_id(["Abandoned Iron Mine"]) if you want a single node)
def get_node_id(name):
    return([node[node['NAME'] == x].index[0] for x in name])

def get_node_name(idx):
    return(node.iloc[idx]['NAME'])

def get_total_cp(idx):
    return(sum(node.iloc[idx]['CP'].values))

Analyze with vs without weights for shortest paths

# Dijkstra gives shortest path length
g.shortest_paths_dijkstra(get_node_id(["Abandoned Iron Mine"])[0], get_node_id(["Calpheon"])[0])
g.shortest_paths_dijkstra(get_node_id(["Abandoned Iron Mine"])[0], get_node_id(["Calpheon"])[0], weights = "weight")
[[17.0]]
a = g.get_shortest_paths(get_node_id(["Calpheon"])[0], get_node_id(["Abandoned Iron Mine"])[0])[0]
a
[66, 98, 94, 59, 226, 192, 143, 102, 159, 11, 309, 36, 148, 21, 0]
get_node_name(a).values
array(['Calpheon', 'Dias Farm', 'Delphe Knights Castle', 'Biraghi Den',
       'Northern Plain Of Serendia', 'Lynch Farm Ruins', 'Heidel',
       'Eastern Border', 'Kamasylve Temple', 'Ahto Farm',
       'Stonetail Horse Ranch', 'Asula Highland', 'Highland Junction',
       'Altinova Entrance', 'Abandoned Iron Mine'], dtype=object)
get_total_cp(a)

23
# Get all node IDs that edges point to
destinations = [x[1] for x in g.get_edgelist()]
cp_cost = [node.iloc[i, :]["CP"] for i in destinations]
g.es["weight"] = cp_cost
g.es[0].attributes()
{'weight': 1}
b = g.get_shortest_paths(get_node_id(["Calpheon"])[0], get_node_id(["Abandoned Iron Mine"])[0], weights = "weight")[0]
b
[66, 116, 204, 242, 62, 226, 192, 143, 102, 159, 11, 309, 36, 148, 21, 0]
get_node_name(b)
[66                Calpheon
 116       Falres Dirt Farm
 204       Marni Farm Ruins
               ...         
 148      Highland Junction
 21       Altinova Entrance
 0      Abandoned Iron Mine
 Name: NAME, Length: 16, dtype: object]
get_total_cp(b)
20
node_dist
NODE_ID NAME CP AREA TYPE REGION_MOD_PERCENT CONNECTIONS NODE_NAME MIN_DIST MIN_CITY
0 1 Abandoned Iron Mine 2 Mediah Worker Node 0.0 Abandoned Iron Mine Saunil District, Abandoned... Abandoned Iron Mine 1113.0 Altinova
1 2 Abandoned Iron Mine Entrance 1 Mediah Connection Node NaN Highland Junction, Alumn Rock Valley NaN NaN NaN
2 3 Abandoned Iron Mine Rhutum District 1 Mediah Connection Node NaN Abandoned Iron Mine, Abun NaN NaN NaN
... ... ... ... ... ... ... ... ... ... ...
356 357 Yalt Canyon 1 Valencia Connection Node NaN Shakatu, Gahaz Bandit's Lair NaN NaN NaN
357 358 Yianaros's Field 1 Kamasylvia Connection Node NaN Western Valtarra Mountains NaN NaN NaN
358 359 Zagam Island 1 Margoria Connection Node NaN Nada Island NaN NaN NaN

359 rows × 10 columns

node.index
RangeIndex(start=0, stop=359, step=1)