-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcoverage_class.py
More file actions
81 lines (61 loc) · 2.05 KB
/
coverage_class.py
File metadata and controls
81 lines (61 loc) · 2.05 KB
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
"""
An example class to use the CoverageControl library to run a coverage algorithm
"""
import sys
import coverage_control as cc # Main library
from coverage_control import CoverageSystem
from coverage_control.algorithms import ClairvoyantCVT as CoverageAlgorithm
# Algorithms available:
# ClairvoyantCVT
# CentralizedCVT
# DecentralizedCVT
# NearOptimalCVT
class RunCoverageAlgorithm:
"""
A class to run the coverage algorithm
"""
def __init__(self, params_filename=None):
if params_filename is not None:
self.params_ = cc.Parameters(params_filename)
else:
self.params_ = cc.Parameters()
self.params_.pNumGaussianFeatures = 5
self.params_.pMaxSigma = 100
self.params_.pMinSigma = 100
self.env = CoverageSystem(self.params_)
self.env.PlotInitMap('Init')
self.controller = CoverageAlgorithm(
self.params_, self.params_.pNumRobots, self.env
)
def step(self):
"""
Run one step of the coverage algorithm
"""
self.controller.ComputeActions()
actions = self.controller.GetActions()
error_flag = self.env.StepActions(actions)
return error_flag
def execute(self):
"""
Run the coverage algorithm
"""
num_steps = 1
init_cost = self.env.GetObjectiveValue()
print(f"Initial Coverage cost: {init_cost:.2e}")
while num_steps <= self.params_.pEpisodeSteps:
if self.step():
print(f"Error in step {num_steps}")
break
if self.controller.IsConverged():
print(f"Converged in step {num_steps}")
break
num_steps = num_steps + 1
final_cost = self.env.GetObjectiveValue()
print(f"Improvement %: {100 * (init_cost - final_cost)/init_cost:.2f}")
self.env.PlotSystemMap('Final')
if __name__ == "__main__":
if len(sys.argv) > 1:
cc = RunCoverageAlgorithm(sys.argv[1])
else:
cc = RunCoverageAlgorithm()
cc.execute()