def pickStartCountry(): ## Move refs from all countries: if graph.vs[0]['NumRefs']<10000: coIDs = range( 38 ) numRefsCos = [] for coid in coIDs: # graph.vs[coid]['NumRefs'] numRefsCos.append( graph.vs[coid]['NumRefs'] ) originCoWeight = [ i/float(sum(numRefsCos)) for i in numRefsCos ] # print originCoWeight return np.random.choice(coIDs, p=originCoWeight) ## Move Refs only from Origin and Transition countries else: coList = COS.originCoList() + COS.transitionCoList() coIDs = []#[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 18, 19, 30, 31, 32, 33, 34, 35, 36, 37, 38] numRefsCos = [] for vertex in graph.vs: if vertex['label'] in coList: numRefsCos.append( vertex['NumRefs'] ) coIDs.append( vertex.index ) # for coid in coIDs: # graph.vs[coid]['NumRefs'] # numRefsCos.append( graph.vs[coid]['NumRefs'] ) originCoWeight = [ i/float(sum(numRefsCos)) for i in numRefsCos ] # print originCoWeight return np.random.choice(coIDs, p=originCoWeight)
def costFuncMSIMCalculate(edge, update=True): ''' Calculate the cost for a given edge using a heavily modified Spacial Interaction Model ("Jessie" model). Option to automatically update the "Cost" attribute for the edge. Default behavior. INPUT: edge : indivigual igraph edge object. *update : Boolian. If True, execution will update the Cost attribute of the given edge. RETURNS: cost : The evaluated value of the cost function. ''' source = edge['Source'] target = edge['Target'] ## Set land/sea toggle value if edge['TransitMethod']=='land': landval = 0.75 elif edge['TransitMethod']=='sea': landval = 1 else: print 'WARNING: No transit method defined for: ', edge if graph.vs[target]['NumRefs'] > graph.vs[target]['RefCap']: val = 0.00000001 if update==True: edge['Cost'] = val edge['HMDCost'] = 10000/float(val) return val if edge['TargetCo'] in COS.endCountryList(): ## Equation D case (end countries): pt1 = edge['Safety'] * \ graph.vs[target]['SafetyCo'] * \ graph.vs[target]['Resources'] * \ graph.vs[target]['NumRefs'] * \ graph.vs[target]['natPop'] * \ landval den = edge['Distance']**.5 *\ edge['MoneyCost'] * \ graph.vs[source]['Resources'] *\ graph.vs[source]['natPop'] * \ graph.vs[source]['SafetyCo'] endmult = 1 - \ ( graph.vs[target]['NumRefs'] / \ float( graph.vs[target]['RefCap'] ) ) val = pt1 * endmult / den # if graph.vs[target]['NumRefs'] > graph.vs[target]['RefCap']: # val = 0.00000001 if update==True: edge['Cost'] = val edge['HMDCost'] = 10000/float(val) return val elif edge['TargetCo'] in COS.transitionCoList(): ## Equation C case (Transition countries): pt1 = edge['Safety'] * \ graph.vs[target]['SafetyCo'] * \ graph.vs[target]['Resources'] * \ graph.vs[target]['NumRefs'] * \ landval den = edge['Distance'] * \ edge['MoneyCost'] * \ resourcesCalculate( graph.vs[source], update=False ) * \ graph.vs[source]['SafetyCo'] endmult = 1 - \ ( graph.vs[target]['NumRefs'] / \ float( graph.vs[target]['RefCap'] ) ) val = pt1 * endmult / den # # if graph.vs[target]['NumRefs'] > graph.vs[target]['RefCap']: # val = 0.00000001 if update==True: edge['Cost'] = val if val < .0000001: edge['HMDCost'] = .0000001 else: edge['HMDCost'] = 10000/float(val) return val