def get_IR(M,G,R,s,t,vdim,B): #Solve the nominal model R0={} vdim0=0 M_nom=create_asymmetric_uncertainty_shortest_path_interdiction_nx(G,R0, s,t,vdim0,B) opt.solve(M_nom) #(paths,length)=return_paths(M_nom,G,R0,s,t) #print('Nominal SPI: Path') #print(paths) #print(length) #Solve a model that has the fixed interdictions from the nominal model but adds in robustness M_nom_rob=create_asymmetric_uncertainty_shortest_path_interdiction_nx(G,R, s,t,vdim,B) M_nom_rob.mods=ConstraintList() #print('Nominal SPI: Interdicted Arcs') for (i,j) in G.edges: if value(M_nom.x[(i,j)]) >= 0.9: M_nom_rob.mods.add(M_nom_rob.x[(i,j)]==1) #print(f'({i},{j})') else: M_nom_rob.mods.add(M_nom_rob.x[(i,j)]==0) opt.solve(M_nom_rob) #M_nom_rob.x.pprint() #(paths,length)=return_paths(M_nom_rob,G,R,s,t) #print('Robust Path Given Nominal Interdictions') #print(paths) #print(length) #Make comparisons #print(value(M.Obj)) #print(value(M_nom.Obj)) #print(value(M_nom_rob.Obj)) Regret=100*(exp(-value(M.Obj))-exp(-value(M_nom.Obj)))/exp(-value(M.Obj)) Improvement=100*(exp(-value(M_nom_rob.Obj))-exp(-value(M.Obj)))/exp(-value(M_nom_rob.Obj)) return (Improvement,Regret)
print(f'Percentage Complete: {100*loop/2700}') start = time.time() paths = nx.shortest_path(G, S, T, weight='length') end = time.time() djikstras_time = end - start M_SP = create_shortest_path_nx(G, S, T) start = time.time() opt.solve(M_SP) end = time.time() SP_time = end - start M_interdiction = create_shortest_path_interdiction_nx(G, S, T, B) start = time.time() opt.solve(M_interdiction) end = time.time() interdiction_time = end - start M = create_asymmetric_uncertainty_shortest_path_interdiction_nx( G, R, S, T, vdim, B) start = time.time() opt.solve(M) end = time.time() (paths, lengths) = return_paths(M, G, R, S, T) #generator #To do once we determine how shortest path returns paths #if len(paths) > 1: ''' path_cell=[] length_cell=[] number_cell=[] for path in paths: path_str=[] for i in path: path_str.append(str(i)) path_str="-".join(path_str)
raise Exception('Original Sioux Falls value out of range 2-10') G=nx.DiGraph() for (i,j) in Prob.keys(): (p,q)=Prob[(i,j)] G.add_edge(i,j,length=-np.log(p),interdicted_length=-np.log(q)+np.log(p)) #r=0.25*q #G.add_edge(i,j,length=-np.log(p),interdicted_length=-np.log(q)+np.log(p), robust=-np.log(r)+np.log(q)) vdim=len(set(G.edges())) R={} for (i,j) in set(G.edges): r=G[i][j]['interdicted_length'] k=0 for (u,v) in set(G.edges): if i==u and j==v: R[(i,j,k)]=r else: R[(i,j,k)]=0 k=k+1 vdim=0 R={} B=5 M_ref=create_asymmetric_uncertainty_shortest_path_interdiction_nx(G,R,s,t,vdim,B) opt.solve(M_ref) arcs=return_interdicted_arcs(M_ref,G) (path,length)=return_paths(M_ref,G,R,s,t) print(path) print(exp(-length)) print(arcs)
opt = SolverFactory('gurobi') opt.options['TimeLimit'] = 1200 for loop in range(1, 21): print(f'Starting loop {loop}') table = PrettyTable() table.hrules = ALL table.field_names = ["Number of Nodes", "Model Build Time", "Solve Time"] G_10 = create_connected_graph(10, 30) R_10 = create_R(G_10) print('Finished creating the graph') s = random.choice(list(G_10.nodes)) t = random.choice(list(set(G_10.nodes) - {s})) start = time.time() M_10 = create_asymmetric_uncertainty_shortest_path_interdiction_nx( G_10, R_10, s, t, vdim_10, B_10) end = time.time() model_creation_time = end - start print(f'Finished creating the model in {model_creation_time} seconds') start = time.time() opt.solve(M_10) end = time.time() solve_time = end - start print(f'Finished solving 10 node model in {solve_time} seconds') table.add_row([10, model_creation_time, solve_time]) time_10.append(solve_time) G_100 = create_connected_graph(100, 300) R_100 = create_R(G_100) print('Finished creating the graph') s = random.choice(list(G_100.nodes))
vdim_SS = len(set(G_SS.edges())) R_SS = {} for (i, j) in set(G_SS.edges): r = G[i][j]['interdicted_length'] k = 0 for (u, v) in set(G_SS.edges): if i == u and j == v: R_SS[(i, j, k)] = r else: R_SS[(i, j, k)] = 0 k = k + 1 S = 100 T = 10 M_SSSPIAU = create_asymmetric_uncertainty_shortest_path_interdiction_nx( G_SS, R_SS, S, T, vdim_SS, B) opt.solve(M_SSSPIAU) interdicted = return_interdicted_arcs(M_SSSPIAU, G_SS) (paths, lengths) = return_paths(M_SSSPIAU, G_SS, R_SS, S, T) (Regret_Avoided, Objective_Paid) = get_IR(M_SSSPIAU, G_SS, R_SS, S, T, vdim_SS, B) print('SS-SPIAU') print('Interdict') print(interdicted) print('Path') print(paths) print('Adjusted Length') print(np.exp(-lengths)) print('Evader Length') print(np.exp(-M_SSSPIAU.d[T].value)) print('Regret Avoided')