This repository has been archived by the owner on Nov 30, 2023. It is now read-only.
forked from timvandermeij/mobile-radio-tomography
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plan_reconstruct.py
131 lines (105 loc) · 4.25 KB
/
plan_reconstruct.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# Core imports
import datetime
import os
import sys
import time
import json
# matplotlib imports
import matplotlib
try:
# Make it possible to run matplotlib in displayless (console-only) mode
matplotlib.use('Agg' if 'DISPLAY' not in os.environ or os.environ['DISPLAY'] == '' else matplotlib.get_backend())
except ValueError as e:
raise ImportError("Could not load matplotlib backend: {}".format(e.message))
finally:
import matplotlib.pyplot as plt
# Package imports
from __init__ import __package__
from core.Import_Manager import Import_Manager
from core.Thread_Manager import Thread_Manager
from planning.Runner import Planning_Runner
from settings import Arguments
def do_plot(name):
"""
Finish plotting by saving or showing the plot.
"""
backend = matplotlib.get_backend()
if backend.lower() == 'agg' or 'SAVE_PATH' in os.environ:
path = os.environ['SAVE_PATH'] if 'SAVE_PATH' in os.environ else '.'
filename = "{}/{}".format(path, name)
plt.savefig(filename)
print("Saved plot as {}".format(filename))
else:
print("Close the plot window to continue.")
try:
plt.show()
except StandardError:
# Somethimes things go wrong in the plot display (such as when
# clicking close button too fast), so ignore those errors.
pass
def do_data(name, data):
"""
Handle data output.
Either write a JSON file with the given `name` for the `data` object, or
print the data to the standard output.
"""
if matplotlib.get_backend() == 'Agg' or 'SAVE_PATH' in os.environ:
path = os.environ['SAVE_PATH'] if 'SAVE_PATH' in os.environ else '.'
filename = "{}/{}.json".format(path, name)
with open(filename, 'wb') as f:
json.dump(data, f)
else:
print(data)
def iteration_callback(algorithm, data):
t = data["iteration"]
cur_time = data["cur_time"]
speed = t/float(cur_time)
print("Iteration {} ({} sec, {} it/s)".format(t, cur_time, speed))
if speed != 0.0:
rate = (algorithm.t_max - t) / speed
end_time = datetime.datetime.now() + datetime.timedelta(seconds=rate)
print("{} seconds remaining, ETA: {}".format(rate, end_time))
Feasible = data["feasible"]
Objectives = data["objectives"]
scores = list(sorted((i for i in range(algorithm.mu) if Feasible[i]), key=lambda i: Objectives[i]))
if scores:
idx = scores[len(scores)/2]
print("Current knee point objectives: {}".format(Objectives[idx]))
print("Infeasible count: {}".format(algorithm.mu - sum(Feasible)))
def main(argv):
# Initialize, read parameters from input and set up problems
stamp = int(time.time())
thread_manager = Thread_Manager()
import_manager = Import_Manager()
arguments = Arguments("settings.json", argv)
runner = Planning_Runner(arguments, thread_manager, import_manager,
iteration_callback)
arguments.check_help()
t_max = runner.get_iteration_limit()
size = runner.get_population_size()
print("Settings: Algorithm {}, mu={}, t_max={}".format(runner.algorithm.get_name(), size, t_max))
print("Steps: {}".format(runner.algorithm.steps))
indices = runner.start()
# Show feasible solutions in a sorted manner.
if len(indices) == 0:
print("No feasible solutions found after {} iterations!".format(t_max))
return
print("Search variables an objective values for feasible solutions:")
# If we have fewer nondominated solutions than the total number of
# individuals, then only show the nondominated ones. Otherwise, just show
# all feasible solutions.
c = 0
for i in indices:
c += 1
positions, unsnappable = runner.get_positions_plot(i, c, len(indices))
if positions.size == 0:
continue
print("{}. {} ({})".format(i, runner.get_objectives(i), unsnappable))
do_data("positions-{}-{}".format(stamp, c), positions.tolist())
do_plot("display-{}-{}.eps".format(stamp, c))
# Plot the pareto front between the two objectives.
print("Pareto front after t={}".format(t_max))
runner.make_pareto_plot()
do_plot("front-{}.eps".format(stamp))
if __name__ == "__main__":
main(sys.argv[1:])