def ind_to_output(ind, inp: WusnInput) -> WusnOutput: out = WusnOutput(inp) sensors = ind[:ind.sensor_count] relays = ind[ind.sensor_count:] out.sensors = sensors out.relays = relays chunk_size = ind.sensor_count // ind.relay_count for i, rn in enumerate(relays): sns = sensors[i * chunk_size:(i + 1) * chunk_size] out.relay_to_sensors[rn] = [] for sn in sns: out.relay_to_sensors[rn].append(sn) return out
def prob_to_out(prob: pulp.LpProblem, inp: WusnInput): N, M, Y = len(inp.sensors), len(inp.relays), inp.relay_num v = prob.variablesDict() sensors, all_relays = inp.sensors, inp.relays relay_to_sensors = {} relays = [] # Get assignment matrix for i in range(N): sn = sensors[i] for j in range(M): a = v['A_%d_%d' % (i, j)] if a.value() > 0: rn = all_relays[j] relays.append(rn) if rn not in relay_to_sensors.keys(): relay_to_sensors[rn] = [sn] else: relay_to_sensors[rn].append(sn) break out = WusnOutput(inp, sensors=inp.sensors, relays=relays, relay_to_sensors=relay_to_sensors) return out
def lbsna2(prev: WusnOutput, verbose=True) -> WusnOutput: def verbose_print(*args, **kwargs): if verbose: print(*args, **kwargs) inp = prev.input _ = inp.loss out = WusnOutput(prev.input, sensors=prev.sensors[:], relays=prev.relays[:], relay_to_sensors=copy.deepcopy(prev.relay_to_sensors)) verbose_print('Starting LBSNA-2...') target_load = len(inp.sensors) // inp.relay_num verbose_print('Target load: %d' % target_load) current_relays = prev.relays[:] for i in range(inp.relay_num): verbose_print('Iter %d/%d' % (i + 1, inp.relay_num)) chosen = _find_optimal(current_relays, out) verbose_print('Chosen relay: %s' % chosen) current_relays.remove(chosen) load_relay(chosen, current_relays, out, target_load, verbose=verbose) return out
def kmeans_greedy(inp: WusnInput, heuristic=False, km_runs=1): X = list(map(lambda s: [s.x, s.y], inp.sensors)) X = np.asarray(X) km = KMeans(n_clusters=inp.relay_num, n_init=km_runs, max_iter=3500, verbose=0) km.fit(X) clusters = [[] for _ in range(inp.relay_num)] pairs = [None] * inp.relay_num for i, c in enumerate(km.labels_): clusters[c].append(inp.sensors[i]) centroid = Point(*km.cluster_centers_[c]) pairs[c] = (clusters[c], centroid) # Sort clusters by centroid distance to an anchor point anchor = Point(0., 0.) pairs.sort(key=lambda x: x[1].distance(anchor)) clusters = list(map(lambda x: x[0], pairs)) # Greedily choose a relay for each cluster current_relays = inp.relays.copy() relay_to_sensors = {} selected = [] for cl in clusters: best_rn = None best_loss = float('inf') for rn in current_relays: max_loss = -float('inf') for sn in cl: ls = inp.loss[(sn, rn)] if ls > max_loss: max_loss = ls if max_loss < best_loss: best_loss = max_loss best_rn = rn current_relays.remove(best_rn) selected.append(best_rn) relay_to_sensors[best_rn] = cl if heuristic: out1 = WusnOutput(inp, sensors=inp.sensors.copy(), relays=list(relay_to_sensors.keys()), relay_to_sensors=relay_to_sensors) out2 = lbsna3.lbsna3(out1, verbose=False) # Reorder selected and sensors selected, clusters = [], [] for rn, sns in out2.relay_to_sensors.items(): selected.append(rn) clusters.append(sns) indv = Individual(sensor_count=len(inp.sensors), relay_count=inp.relay_num) # Flatten clusters for cl in clusters: indv.extend(cl) indv.extend(selected) return indv
def lbsna3(prev: WusnOutput, verbose=True) -> WusnOutput: def verbose_print(*args, **kwargs): if verbose: print(*args, **kwargs) inp = prev.input _ = inp.loss out = WusnOutput(prev.input, sensors=prev.sensors[:], relays=prev.relays[:], relay_to_sensors=copy.deepcopy(prev.relay_to_sensors)) Y = inp.relay_num verbose_print('Starting LBSNA-3...') target_load = len(inp.sensors) // inp.relay_num verbose_print('Target load: %d' % target_load) W = Point(0., 0.) for p in prev.sensors + prev.relays: W += Point(p.x, p.y) W /= (len(prev.sensors) + len(prev.relays)) current_relays = prev.relays[:] for i in range(Y): verbose_print('Iter %d/%d' % (i + 1, Y)) max_distance = -float("inf") chosen = None for rn in current_relays: if distance(rn, W) > max_distance: max_distance = distance(rn, W) chosen = rn sns = out.relay_to_sensors[rn] for sn in sns: if distance(sn, W) > max_distance: max_distance = distance(sn, W) chosen = rn verbose_print('Chosen relay: %s' % chosen) current_relays.remove(chosen) if len(out.relay_to_sensors[chosen]) > target_load: unload_relay(chosen, current_relays, out, target_load, verbose=verbose) else: load_relay(chosen, current_relays, out, target_load, verbose=verbose) return out
def ind_to_output(ind, inp: WusnInput, delegate=lbsna3.lbsna3): # relays = list(ind) relays = [] for i, v in enumerate(ind): if v > 0: relays.append(inp.relays[i]) out = WusnOutput(inp, sensors=inp.sensors, relays=relays) greedy_assign(out) out = delegate(out, verbose=False) return out
def greedy_assign(out: WusnOutput): for rn in out.relays: out.relay_to_sensors[rn] = [] for sn in out.sensors: best_rn = None best_loss = float('inf') for rn in out.relays: if out.input.loss[(sn, rn)] < best_loss: best_rn = rn best_loss = out.input.loss[(sn, rn)] out.relay_to_sensors[best_rn].append(sn)
def lurns1(inp: WusnInput) -> WusnOutput: sensors = inp.sensors in_relays = inp.relays[:] Y = inp.relay_num out_relays = [] out_relays_to_sensors = {} loss = inp.loss # L(sn, rn) = loss[(sn, rn)] print("Starting LURNS-1...") while len(out_relays) < Y: min_T = float("inf") best_rn = None for fq in in_relays: losses = [] for id1, sn in enumerate(sensors): Ts = float('inf') for rn in out_relays + [fq]: ls = loss[(sn, rn)] if ls < Ts: Ts = ls losses.append(Ts) Tc = max(losses) if Tc < min_T: min_T = Tc best_rn = fq print('[%d] Picked relay: %s' % (len(out_relays), best_rn)) out_relays.append(best_rn) in_relays.remove(best_rn) # Gan cac sn cho rn for rn in out_relays: out_relays_to_sensors[rn] = [] for sn in sensors: t_min = float("inf") best_rn = None for rn in out_relays: ls = loss[(sn, rn)] if ls < t_min: t_min = ls best_rn = rn out_relays_to_sensors[best_rn].append(sn) # Ket qua out = WusnOutput(inp, sensors, out_relays, out_relays_to_sensors) return out
plt.ioff() print('Enter a path to an input/output file to view its plot.') print('Ctrl+C or Ctrl+D to exit.') try: while True: path = prompt('> ', history=history) if not os.path.exists(path): print('No such path exists.') continue try: if path.endswith('.test'): obj = WusnInput.from_file(path) else: obj = WusnOutput.from_text_file(path) except Exception: print('Failed to open file.') continue fig = plt.figure() ax = Axes3D(fig) obj.plot(ax, highlight_max=False) ax.legend() plt.show() fig.clf() except (KeyboardInterrupt, EOFError): print()
def lurns2(inp: WusnInput) -> WusnOutput: sensors = inp.sensors Y = inp.relay_num in_relays = inp.relays[:] out_relays = list() or_set = set() out_relays_to_sensors = {} loss = inp.loss # L(sn, rn) = loss[(sn, rn)] print("Starting LURNS-2...") for sn in sensors: best_rn = None t_min = float("inf") for rn in in_relays: ls = loss[(sn, rn)] if ls < t_min: t_min = ls best_rn = rn print('[%d] Picking %s' % (len(out_relays), best_rn)) # out_relays.append(best_rn) # in_relays.remove(best_rn) if best_rn not in or_set: or_set.add(best_rn) out_relays.append(best_rn) del or_set out_relays = list(out_relays) while len(out_relays) > Y: T_min = float("inf") best_rn = None for fq in out_relays: out2 = out_relays[:] out2.remove(fq) losses = [] for sn in sensors: Ts = float('inf') for rn in out2: ls = loss[(sn, rn)] if ls < Ts: Ts = ls losses.append(Ts) Tc = max(losses) if Tc < T_min: T_min = Tc best_rn = fq print('[%d] Removing %s' % (len(out_relays), best_rn)) out_relays.remove(best_rn) # Gan cac sn cho rn for rn in out_relays: out_relays_to_sensors[rn] = [] for sn in sensors: best_rn = None t_min = float("inf") for rn in out_relays: ls = loss[(sn, rn)] if ls < t_min: t_min = ls best_rn = rn out_relays_to_sensors[best_rn].append(sn) # Ket qua out = WusnOutput(inp, sensors, out_relays, out_relays_to_sensors) return out
import sys sys.path.append('.') from wusn.commons import WusnOutput from wusn.yuan.lbsna import lbsna2 if __name__ == '__main__': out1 = WusnOutput.from_text_file('tests/001_lu.out') out2 = lbsna2.lbsna2(out1) out2.to_text_file('data/001.test', 'tests/001_lb.out')
import sys from wusn.commons import WusnOutput if __name__ == '__main__': inp = sys.argv[1] out_path = sys.argv[2] out = WusnOutput.from_text_file(inp) out.plot_to_file(out_path)