def test_unary_operators(self): gs = GraphSet([g0, g1, g12, g123, g1234, g134, g14, g4]) self.assertTrue(isinstance(~gs, GraphSet)) self.assertEqual(~gs, GraphSet([g124, g13, g2, g23, g234, g24, g3, g34])) self.assertTrue(isinstance(gs.smaller(3), GraphSet)) self.assertEqual(gs.smaller(3), GraphSet([g0, g1, g12, g14, g4])) self.assertTrue(isinstance(gs.larger(3), GraphSet)) self.assertEqual(gs.larger(3), GraphSet([g1234])) self.assertTrue(isinstance(gs.graph_size(3), GraphSet)) self.assertEqual(gs.graph_size(3), GraphSet([g123, g134])) self.assertTrue(isinstance(gs.len(3), GraphSet)) self.assertEqual(gs.len(3), GraphSet([g123, g134])) gs = GraphSet([g12, g123, g234]) self.assertTrue(isinstance(gs.minimal(), GraphSet)) self.assertEqual(gs.minimal(), GraphSet([g12, g234])) self.assertTrue(isinstance(gs.maximal(), GraphSet)) self.assertEqual(gs.maximal(), GraphSet([g123, g234])) gs = GraphSet([g12, g14, g23, g34]) self.assertTrue(isinstance(gs.blocking(), GraphSet)) self.assertEqual(gs.blocking(), GraphSet([g123, g1234, g124, g13, g134, g234, g24]))
def test_unary_operators(self): gs = GraphSet([g0, g1, g12, g123, g1234, g134, g14, g4]) self.assertTrue(isinstance(~gs, GraphSet)) self.assertEqual(~gs, GraphSet([g124, g13, g2, g23, g234, g24, g3, g34])) self.assertTrue(isinstance(gs.smaller(3), GraphSet)) self.assertEqual(gs.smaller(3), GraphSet([g0, g1, g12, g14, g4])) self.assertTrue(isinstance(gs.larger(3), GraphSet)) self.assertEqual(gs.larger(3), GraphSet([g1234])) self.assertTrue(isinstance(gs.graph_size(3), GraphSet)) self.assertEqual(gs.graph_size(3), GraphSet([g123, g134])) self.assertTrue(isinstance(gs.len(3), GraphSet)) self.assertEqual(gs.len(3), GraphSet([g123, g134])) gs = GraphSet([g12, g123, g234]) self.assertTrue(isinstance(gs.minimal(), GraphSet)) self.assertEqual(gs.minimal(), GraphSet([g12, g234])) self.assertTrue(isinstance(gs.maximal(), GraphSet)) self.assertEqual(gs.maximal(), GraphSet([g123, g234])) gs = GraphSet([g12, g14, g23, g34]) self.assertTrue(isinstance(gs.blocking(), GraphSet)) self.assertEqual( gs.blocking(), GraphSet([g123, g1234, g124, g13, g134, g234, g24]))
def test_large(self): try: import networkx as nx except ImportError: return try: GraphSet.converters['to_graph'] = nx.Graph GraphSet.converters['to_edges'] = nx.Graph.edges g = nx.grid_2d_graph(8, 8) v00, v01, v10 = (0, 0), (0, 1), (1, 0) GraphSet.set_universe(g) self.assertEqual(len(GraphSet.universe().edges()), 112) # self.assertEqual(GraphSet.universe().edges()[:2], [(v00, v01), (v00, v10)]) gs = GraphSet({}) gs -= GraphSet( [nx.Graph([(v00, v01)]), nx.Graph([(v00, v01), (v00, v10)])]) self.assertEqual(gs.len(), 5192296858534827628530496329220094) i = 0 for g in gs: if i > 100: break i += 1 paths = GraphSet.paths((0, 0), (7, 7)) self.assertEqual(len(paths), 789360053252) except: raise finally: GraphSet.converters['to_graph'] = lambda edges: edges GraphSet.converters['to_edges'] = lambda graph: graph
def test_large(self): try: import networkx as nx except ImportError: return try: GraphSet.converters['to_graph'] = nx.Graph GraphSet.converters['to_edges'] = nx.Graph.edges g = nx.grid_2d_graph(8, 8) v00, v01, v10 = (0,0), (0,1), (1,0) GraphSet.set_universe(g) self.assertEqual(len(GraphSet.universe().edges()), 112) # self.assertEqual(GraphSet.universe().edges()[:2], [(v00, v01), (v00, v10)]) gs = GraphSet({}); gs -= GraphSet([nx.Graph([(v00, v01)]), nx.Graph([(v00, v01), (v00, v10)])]) self.assertEqual(gs.len(), 5192296858534827628530496329220094) i = 0 for g in gs: if i > 100: break i += 1 paths = GraphSet.paths((0, 0), (7, 7)) self.assertEqual(len(paths), 789360053252) except: raise finally: GraphSet.converters['to_graph'] = lambda edges: edges GraphSet.converters['to_edges'] = lambda graph: graph
def main(): """Create a structure that represents all paths going from a group of startpoints to a group of endpoints. The start point given by the user is the NE point of a group of 4 points The end point given by the user is the NE point of a group of 4 points The other 3 points are the ones that are one step W, one step S, and two steps SW. """ if len(sys.argv) != 5: print "usage: %s [GRID-M] [GRID-N] [STARTPOINT] [ENDPOINT]" % sys.argv[0] exit(1) dim = (int(sys.argv[1]),int(sys.argv[2])) rows = dim[0] cols = dim[1] dimension = (dim[0]-1,dim[1]-1) startpoint = int(sys.argv[3]) endpoint = int(sys.argv[4]) starts = neighbors(startpoint, cols) ends = neighbors(endpoint, cols) from graphillion import GraphSet import graphillion.tutorial as tl universe = tl.grid(*dimension) GraphSet.set_universe(universe) paths = GraphSet() for start in starts: for end in ends: paths = GraphSet.union(paths,GraphSet.paths(start,end)) print "number of paths: " + str(paths.len()) """ AC: SAVE ZDD TO FILE """ f = open("graphs/fixed_ends-%d-%d-%d-%d.zdd" % (dim[0],dim[1],startpoint,endpoint),"w") paths.dump(f) f.close() """ AC: SAVE GRAPH """ nodes = [None] + [ (x,y) for x in xrange(dim[0]) for y in xrange(dim[1]) ] from collections import defaultdict graph = defaultdict(list) for index,edge in enumerate(paths.universe()): x,y = edge x,y = nodes[x],nodes[y] graph[x].append( (index+1,y) ) graph[y].append( (index+1,x) ) graph_filename = "graphs/fixed_ends-%d-%d-%d-%d.graph.pickle" % (dim[0],dim[1],startpoint,endpoint) # save to file import pickle with open(graph_filename,'wb') as output: pickle.dump(graph,output)
def capacity(self): gs = GraphSet() self.assertFalse(gs) gs = GraphSet([g0, g12, g13]) self.assertTrue(gs) self.assertEqual(len(gs), 3) self.assertEqual(gs.len(), 3)
cand_list = GraphSet() #故障しないものはcand_listへ for i in range(0, 1): cand_list.update(gc.len(E - i)) #print(cand_list.len()) # #f本故障はm_npの数だけ抽出(ひとまずランダムに) # cand_select = gc.len(E-f) # #print(cand_select.len()) # for cnt in range(0,m_np): # rand_graph = next(cand_select.rand_iter()) # cand_list.add(rand_graph) # cand_select.remove(rand_graph) print("total {0} patterns to consider".format(cand_list.len())) for i in cand_list: print(i) #-------------Optimization------------------ I_max = 5 tl = [] R_min = np.inf MAX_loop = 20 weights_opt = copy.deepcopy(weights) for i in range(0, I_max): C_cnt = 0 C_max = 10 target_graph = gc.larger(E - 1).choice() weights = {}
beta_so = 0 for s, d, r_val in r_so: if r_val > beta_so: beta_so = r_val R_link_so = (s, d) #最大輻輳値とそのリンクを保持 # print('beta') beta = (beta_mf - beta_so) / beta_so # print(beta_mf,beta_so,beta)# print(r) ecution_time = time.perf_counter() - start_time # print(ecution_time) #--------output-------------- data = [ eps, f, m_np, cand_mf.len(), alpha_mf, alpha_so, alpha, beta_mf, beta_so, beta, ecution_time ] # print(data) data_str = ','.join(map(str, data)) file_path = './result11_{0}_i{1}_c{2}_tr{3}_fix_2.txt'.format( str(p).split('.')[1], I_max, C_max, len(tr)) with open(file_path, 'a', encoding="utf-8") as f: f.write(data_str) f.write('\n') f.write(str(w_opt_mf)) f.write('\n') f.write(str(w_opt_so)) f.write('\n')
if r_val > beta_mf: beta_mf = r_val R_link_mf = (s,d) #最大輻輳値とそのリンクを保持 beta_so = 0 for s,d,r_val in r_so: if r_val > beta_so: beta_so = r_val R_link_so = (s,d) #最大輻輳値とそのリンクを保持 # print('beta') beta = (beta_mf-beta_so)/beta_so # print(beta_mf,beta_so,beta)# print(r) ecution_time = time.perf_counter() - start_time # print(ecution_time) #--------output-------------- data = [eps,f,m_np,cand_mf.len(),alpha_mf,alpha_so,alpha,beta_mf,beta_so,beta,ecution_time] # print(data) data_str = ','.join(map(str,data)) file_path = './result6a_{0}_i{1}_c{2}_tr{3}_fix_2.txt'.format(str(p).split('.')[1],I_max,C_max,len(tr)) # print(file) # print(data_str) with open(file_path,'a',encoding="utf-8") as f: # f.write('epsilon,Gamma,m_np,F_mf,alpha_PSO-M,alpha_SO,alpha,beta_PSO-M,beta_SO,beta,Time[s]') # f.write('\n') f.write(data_str) f.write('\n')