def exchange_info(ID, data, controllerIP, gain, udp_port): freq_control = 3000e6 file2send = '/root/total/discovery_phase/my_info.txt' file_received = '/root/total/discovery_phase/received_info.txt' ready_file = '/root/total/discovery_phase/ready.txt' ok_file = '/root/total/discovery_phase/ok.txt' sec = 4 f = open(file2send, 'w') if not isinstance(data, basestring): data = l2str(data) str2fil = str(ID) + ' ' + data + '\n' f.write(str2fil) f.close() myIP = socket.gethostbyname(socket.gethostname()) with open('/root/total/ipnet.json') as jf: diction = json.load(jf) ips = diction['net'] nodeList = [int(x) for x in ips.keys()] num_of_ch = len(nodeList) - 1 print 'num_of_channels ', num_of_ch f = open('/root/total/discovery_phase/ready.txt', 'w') f.write('Ready ' + str(ID) + ' To Exchange\n') f.close() udp_transmit.transmit(controllerIP, udp_port + int(ID), ready_file) udp_receive.receive(myIP, udp_port, ok_file) receive_transmit.transceive(freq_control + int(ID) * 100e6, freq_control + 100e6 * min(nodeList), file2send, file_received, sec, num_of_ch, gain) winners = getVectors(file_received, 2, nodeList) os.remove(file2send) os.remove(file_received) os.remove(ok_file) os.remove(ready_file) return winners
if __name__ == '__main__': #usage: PU executes this script, it chooses at random a channel and starts transmitting at this channel for up to countdown slots channel_list = eval(sys.argv[1]) gain = eval(sys.argv[2]) sec = eval(sys.argv[3]) #f = open('/root/total/PU_scan/PU_log.txt', 'a') myIP = socket.gethostbyname(socket.gethostname()) countdown = random.randint(1,3) chosen_channel = random.sample(channel_list,1)[0] #choose a channel iteration= 0 while 1: udp_receive.receive(myIP, 14000, 'empty.txt') os.remove('empty.txt') samp = random.uniform(0,1) print "Round: ", iteration print "Countdown: ", countdown f = open('/root/total/PU_scan/PU_log.txt', 'a') f.write('Round: ' + str(iteration) + ' ') if samp < 0.6: if (countdown <=0): chosen_channel = random.sample(channel_list,1)[0] #choose a channel countdown = random.randint(1,3) print "I am transmitting at channel: ", chosen_channel print "Chosen channel: ", chosen_channel f.write("Chosen_channel_id: " + str(channel_list.index(chosen_channel))) PU_transmit.transmit(chosen_channel, 'input.txt', gain, sec) print "I terminated my transmission!"
def udp_listen(IP_address, udp_port, filename): udp_receive.receive(IP_address, udp_port, filename) global counter counter = counter + 1 print 'Received'
def exchange(ID, vectorsSend, nCh, gain, sec): """ Vectors' exchange within the neighborhood Gs. vectorsSend: contains the filename with the data to be exchanged """ udp_port = 11000 freq_control = 3000e6 ready_file = '/root/total/SIG_exchange/ready.txt' ok_file = '/root/total/SIG_exchange/ok.txt' file_received = '/root/total/SIG_exchange/dataReceived.txt' file_received2 = '/root/total/SIG_exchange/dataReceived2.txt' file2Send = '/root/total/SIG_exchange/tsdata.txt' with open(ready_file, 'w') as f: f.write("Ready to start phase!") # exchange vectors within transmission range Ts # winners is a list that contains the lines corresponding to each neighbor data # tsA, tsN, tsC, tsU are lists of the 1-hop neighbors respective vectors # read configuration file with open('/root/total/ipnet.json') as jf: diction = json.load(jf) ips = diction['net'] # dictionary ips contains the IP addresses of the nodes ips = {int(x): y.encode('utf8') for x, y in ips.items()} nodeList = ips.keys() # list of nodes c_id = max(ips.keys()) # controller always takes the largest id udp_transmit.transmit(ips[c_id], udp_port + int(ID), ready_file) udp_receive.receive(ips[ID], udp_port, ok_file) receive_transmit.transceive(freq_control + int(ID) * 100e6, freq_control + 100e6 * min(nodeList), vectorsSend, file_received, sec, c_id - 3, gain) copyfile(file_received, '/root/total/kostacito.txt') winners, tsA, tsN, tsC, tsU = rv.getVectors(file_received, nCh, nodeList) tsA = [l for l in tsA if l[0] != ID] tsN = [l for l in tsN if l[0] != ID] tsC = [l for l in tsC if l[0] != ID] tsU = [l for l in tsU if l[0] != ID] print "First step completed! -- Signaling Exchange" # write in the file the data of your 1-hop neighbors f2 = open(file2Send, 'w') for w in winners: f2.write(w) f2.close() # exchange within your transmission range the above file receive_transmit.transceive(freq_control + int(ID) * 100e6, freq_control + 100e6 * min(nodeList), file2Send, file_received2, sec, c_id - 3, gain) _, gA, gN, gC, gU = rv.getVectors(file_received2, nCh, nodeList) print "Second step completed! -- Signaling Exchange" # GA, GN, GC, GU are lists of the neighbors respective vectors GA = rem_dupl( tsA + [l for l in gA if l[0] != ID]) # remove duplicates and yourself GN = rem_dupl(tsN + [l for l in gN if l[0] != ID]) GC = rem_dupl(tsC + [l for l in gC if l[0] != ID]) GU = rem_dupl(tsU + [l for l in gU if l[0] != ID]) print "\nTRANSMISSION AREA" print tsA print tsN print tsC print tsU print "\nNEIGHBORHOOD" print GA print GN print GC print GU os.remove(file2Send) os.remove(file_received) os.remove(ok_file) os.remove(ready_file) os.remove(file_received2) os.remove(vectorsSend) return (tsU, GA, GN, GU)
def gibbs_samp(ID, ips, sweeps, TsU, GsU, GsA, GsN, myA, myC, myU, rs, convergence_filename): for w in range(1, sweeps + 1): T = temperature(w, 2) winners = list() # sequential scheme = {3,4,5,6,...} # port used for 8000 nodeList = [int(x) for x in ips.keys()] if not (w == 1 and ID == 3): # new info is of the format: # ID * neighID1 neighID2 ... * new_u_id * old_u_id # every time new info is added to the file # before sending file is cleaned from possible repetitions udp_receive.receive( ips[ID], 8000, '/root/total/final_gibbs_sampling/new_info.txt') winners = read_new_info( '/root/total/final_gibbs_sampling/new_info.txt', nodeList) data = get_new_vectors(winners) for n_vectors in data: ancestor_id = n_vectors[0] if ancestor_id in [i[0] for i in TsU]: # ancestor_id in TsU if n_vectors[2] != n_vectors[3]: # change has happened index = TsU.index( [ancestor_id] + n_vectors[2] ) # find index of u vector of the right node TsU[index] = [ancestor_id ] + n_vectors[3] # replace new value if ancestor_id in [i[0] for i in GsU]: if n_vectors[2] != n_vectors[3]: # change has happened index = GsU.index( [ancestor_id] + n_vectors[2] ) # find index of u vector of the right node GsU[index] = [ancestor_id ] + n_vectors[3] # replace new value for j, node in enumerate([i[0] for i in GsN]): if node in n_vectors[1]: GsN[j] = [ k - g for k, g in zip(GsN[j], [ancestor_id] + n_vectors[2]) ] # subtract old u GsN[j] = [ k + g for k, g in zip(GsN[j], [ancestor_id] + n_vectors[3]) ] # add new u else: time.sleep(1.5) u_star, prob = draw_sample(T, TsU, GsU, GsA, GsN, myA, myC, rs, myU) loc_energy = local_energy.energy_calc(TsU, GsU, GsA, GsN, u_star, myA, myC, rs, myU) c_f = open(convergence_filename, 'a') c_f.write( str(w) + ' ' + lst2str(u_star) + ' ' + str(round(loc_energy, 3)) + ' ' + str(round(prob, 3)) + '\n') c_f.close() affected = [i[0] for i in TsU] length = 6 + len(affected) + len(myU) * 2 MYstring = str(ID) + ' * ' + lst2str(affected) + '* ' + lst2str( myU) + '* ' + lst2str(u_star) + '* ' + str(length) myU = u_star if w != 1: os.remove('/root/total/final_gibbs_sampling/new_info.txt') f = open('/root/total/final_gibbs_sampling/new_info.txt', 'w+') ## must overwrite for w in winners: if int(w.split()[0]) != ID: f.write(w) f.write(MYstring) f.write('\n\n') f.close() copyfile('/root/total/final_gibbs_sampling/new_info.txt', '/root/total/final_gibbs_sampling/myNew_info.txt') target = ID + 1 ## VISITING SCHEME tls = ips.keys() mx = max(ips.keys()) tls.remove(mx) if target not in tls: target = sorted(ips.keys())[0] udp_transmit.transmit(ips[target], 8000, '/root/total/final_gibbs_sampling/new_info.txt') os.remove('/root/total/final_gibbs_sampling/new_info.txt') return u_star
def udp_listen(IP_address, udp_port, filename): udp_receive.receive(IP_address, udp_port, filename) print "got data from node", udp_port - 8000 global counter counter = counter + 1 print 'Received'
def gibbs_samp(ID, ips, sweeps, TsU, GsU, GsA, GsN, myA, myC, myU, rs, convergence_filename): nodeList = [int(x) for x in ips.keys()] Ts_nodes = [n[0] for n in TsU] Gs_nodes = [n[0] for n in GsU] prob = 0 for w in range(1, sweeps + 1): if (w != 1): winners = list() udp_receive.receive( ips[ID], 8000, '/root/total/final_gibbs_sampling/new_info.txt') winners = read_new_info( '/root/total/final_gibbs_sampling/new_info.txt', nodeList) data = get_new_vectors(winners) for n_vectors in data: node_id = n_vectors[0] if node_id in Ts_nodes: if n_vectors[2] != n_vectors[3]: # change has happened index = TsU.index( [node_id] + n_vectors[2]) # find index of u vector TsU[index] = [node_id ] + n_vectors[3] # replace new value if node_id in Gs_nodes: if n_vectors[2] != n_vectors[3]: # change has happened index = GsU.index( [node_id] + n_vectors[2]) # find index of u vector GsU[index] = [node_id ] + n_vectors[3] # replace new value for j, node in enumerate([i[0] for i in GsN]): if node in n_vectors[1]: GsN[j] = [ k - g for k, g in zip(GsN[j], [node_id] + n_vectors[2]) ] GsN[j] = [ k + g for k, g in zip(GsN[j], [node_id] + n_vectors[3]) ] T = temperature(w, 2) tau = 0.2 # may need fine-tuning temp = bernoulli.rvs(tau, size=1) if (temp[0] == True): print "I will update my state!" u_star, prob = draw_sample(T, TsU, GsU, GsA, GsN, myA, myC, rs, myU) else: print "I will NOT update my state!" u_star = myU loc_energy = local_energy.energy_calc(TsU, GsU, GsA, GsN, u_star, myA, myC, rs, myU) c_f = open(convergence_filename, 'a') c_f.write( str(w) + ' ' + lst2str(u_star) + ' ' + str(round(loc_energy, 3)) + ' ' + str(round(prob, 3)) + '\n') c_f.close() print 'i picked u: ', u_star affected = Ts_nodes length = 6 + len(affected) + len(myU) * 2 MYstring = str(ID) + ' * ' + lst2str(affected) + '* ' + lst2str( myU) + '* ' + lst2str(u_star) + '* ' + str(length) myU = u_star if w != 1: os.remove('/root/total/final_gibbs_sampling/new_info.txt') f = open('/root/total/final_gibbs_sampling/new_info.txt', 'w+') ## must overwrite f.write(MYstring) f.write('\n\n') f.close() copyfile('/root/total/final_gibbs_sampling/new_info.txt', '/root/total/final_gibbs_sampling/myNew_info.txt') mx = max(ips.keys()) #ID of controller time.sleep(1.9) udp_transmit.transmit(ips[mx], 8000 + int(ID), '/root/total/final_gibbs_sampling/new_info.txt') os.remove('/root/total/final_gibbs_sampling/new_info.txt') return u_star
def one_iteration(filename, filename1, filename2, filename3, ID, nCh, neigh): with open('/root/total/ipnet.json') as jf: diction = json.load(jf) ips = diction['net'] ips = {int(x): y.encode('utf8') for x, y in ips.items()} nodeList = [int(x) for x in ips.keys()] threadR_array = [] threadS_array = [] # in a topology a node opens up as many receivers # and transmitters as its neighbours number. Each is listening/transmitting # in a carefully selected port. # open receivers in threads for ip in neigh: snd = rev_lookup(ips, ip) port = int(snd) * 1000 + int(ID) # open a receiver temp = threading.Thread(target=udp_listen, args=[ ips[ID], port, filename2, ]) threadR_array.append(temp) # open a sender port = int(ID) * 1000 + int(snd) temp = threading.Thread(target=udp_send, args=[ ip, port, filename3, ]) threadS_array.append(temp) for t in threadR_array: t.start() # controller always has the maximum ID c_id = max(ips.keys()) udp_transmit.transmit(ips[c_id], 13000 + ID, filename) udp_receive.receive(ips[ID], 13000, filename1) for t in threadS_array: t.start() global counter while (counter < (len(threadR_array) + len(threadS_array))): pass counter = 0 winners, lA, lN, lC, lU = rv.getVectors( '/root/total/final_gibbs_sampling/dataReceived.txt', nCh, nodeList) os.remove(filename1) os.remove(filename2) os.remove(filename3) for t in threadR_array: t.join() for t in threadS_array: t.join() return (winners, lA, lN, lC, lU)