Skip to content

Commit d45dbe8

Browse files
authored
Locator configuration added (#794)
* Stop hardcoding locators/scenarios
1 parent 1dbd9ac commit d45dbe8

7 files changed

Lines changed: 328 additions & 81 deletions

File tree

src/Events/NodeSettingReceivedEventArgs.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
public class NodeSettingReceivedEventArgs
44
{
5-
public string NodeId { get; set; }
6-
public string Setting { get; set; }
7-
public string Payload { get; set; }
5+
public string? NodeId { get; set; }
6+
public string? Setting { get; set; }
7+
public string? Payload { get; set; }
88
}

src/Models/Config.cs

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ public class Config
2525
[YamlMember(Alias = "map")]
2626
public ConfigMap Map { get; set; } = new();
2727

28-
[YamlMember(Alias = "floors")] public ConfigFloor[] Floors { get; set; } = Array.Empty<ConfigFloor>();
28+
[YamlMember(Alias = "floors")]
29+
public ConfigFloor[] Floors { get; set; } = Array.Empty<ConfigFloor>();
2930

3031
[YamlMember(Alias = "nodes")]
3132
public ConfigNode[] Nodes { get; set; } = Array.Empty<ConfigNode>();
@@ -39,10 +40,59 @@ public class Config
3940
[YamlMember(Alias = "history")]
4041
public ConfigHistory History { get; set; } = new();
4142

43+
[YamlMember(Alias = "locators")]
44+
public ConfigLocators Locators { get; set; } = new();
45+
46+
[YamlMember(Alias = "optimization")]
47+
public ConfigOptimization Optimization { get; set; } = new();
48+
}
49+
50+
public class ConfigLocators
51+
{
52+
[YamlMember(Alias = "nadaraya_watson")]
53+
public NadarayaWatsonConfig NadarayaWatson { get; set; } = new();
54+
55+
[YamlMember(Alias = "nealder_mead")]
56+
public NealderMeadConfig NealderMead { get; set; } = new();
57+
58+
[YamlMember(Alias = "nearest_node")]
59+
public NearestNodeConfig NearestNode { get; set; } = new();
60+
}
61+
62+
public class NadarayaWatsonConfig
63+
{
64+
[YamlMember(Alias = "enabled")]
65+
public bool Enabled { get; set; }
66+
67+
[YamlMember(Alias = "floors")]
68+
public string[]? Floors { get; set; }
69+
70+
[YamlMember(Alias = "bandwidth")]
71+
public double Bandwidth { get; set; } = 0.5;
72+
73+
[YamlMember(Alias = "kernel")]
74+
public string Kernel { get; set; } = "gaussian";
75+
}
76+
77+
public class NealderMeadConfig
78+
{
79+
[YamlMember(Alias = "enabled")]
80+
public bool Enabled { get; set; }
81+
82+
[YamlMember(Alias = "floors")]
83+
public string[]? Floors { get; set; }
84+
4285
[YamlMember(Alias = "weighting")]
4386
public ConfigWeighting Weighting { get; set; } = new();
87+
}
88+
89+
public class NearestNodeConfig
90+
{
91+
[YamlMember(Alias = "enabled")]
92+
public bool Enabled { get; set; }
4493

45-
[YamlMember(Alias = "optimization")] public ConfigOptimization Optimization { get; set; } = new();
94+
[YamlMember(Alias = "max_distance")]
95+
public double? MaxDistance { get; set; }
4696
}
4797

4898
public class ConfigMap

src/Models/State.cs

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,6 @@ public class State
1313
{
1414
public State(ConfigLoader cl)
1515
{
16-
IEnumerable<Floor> GetFloorsByIds(string[]? floorIds)
17-
{
18-
if (floorIds == null) yield break;
19-
foreach (var floorId in floorIds)
20-
if (Floors.TryGetValue(floorId, out var floor))
21-
yield return floor;
22-
}
23-
2416
void LoadConfig(Config c)
2517
{
2618
Config = c;
@@ -56,12 +48,13 @@ void LoadConfig(Config c)
5648
NamesToTrack = namesToTrack;
5749
ConfigDeviceByName = configDeviceByName;
5850

59-
Weighting = c.Weighting?.Algorithm switch
51+
var w = c?.Locators?.NealderMead?.Weighting;
52+
Weighting = w?.Algorithm switch
6053
{
6154
"equal" => new EqualWeighting(),
62-
"gaussian" => new GaussianWeighting(c.Weighting?.Props),
63-
"exponential" => new ExponentialWeighting(c.Weighting?.Props),
64-
_ => new GaussianWeighting(c.Weighting?.Props),
55+
"gaussian" => new GaussianWeighting(w?.Props),
56+
"exponential" => new ExponentialWeighting(w?.Props),
57+
_ => new GaussianWeighting(w?.Props),
6558
};
6659
foreach (var device in Devices.Values) device.Check = true;
6760
}
@@ -100,7 +93,6 @@ public OptimizationSnapshot TakeOptimizationSnapshot()
10093
Tx = tx,
10194
Rx = rx,
10295
});
103-
10496
}
10597

10698
if (OptimizationSnaphots.Count > Config?.Optimization.MaxSnapshots) OptimizationSnaphots.RemoveAt(0);
@@ -109,11 +101,44 @@ public OptimizationSnapshot TakeOptimizationSnapshot()
109101
return os;
110102
}
111103

104+
IEnumerable<Floor> GetFloorsByIds(string[]? floorIds)
105+
{
106+
if (floorIds == null)
107+
{
108+
foreach (var floor in Floors.Values)
109+
yield return floor;
110+
}
111+
else
112+
foreach (var floorId in floorIds)
113+
if (Floors.TryGetValue(floorId, out var floor))
114+
yield return floor;
115+
}
116+
112117
public IEnumerable<Scenario> GetScenarios(Device device)
113118
{
114-
foreach (var floor in Floors.Values) yield return new Scenario(Config, new NelderMeadMultilateralizer(device, floor, this), floor.Name);
115-
//yield return new Scenario(_state.Config, new MultiFloorMultilateralizer(device, _state), "Multifloor");
116-
yield return new Scenario(Config, new NearestNode(device), "NearestNode");
119+
var nealderMead = Config?.Locators?.NealderMead;
120+
var nadarayaWatson = Config?.Locators?.NadarayaWatson;
121+
var nearestNode = Config?.Locators?.NearestNode;
122+
123+
if ((nealderMead?.Enabled ?? false) || (nadarayaWatson?.Enabled ?? false) || (nearestNode?.Enabled ?? false))
124+
{
125+
if (nealderMead?.Enabled ?? false)
126+
foreach (var floor in GetFloorsByIds(nealderMead?.Floors))
127+
yield return new Scenario(Config, new NelderMeadMultilateralizer(device, floor, this), floor.Name);
128+
129+
if (nadarayaWatson?.Enabled ?? false)
130+
foreach (var floor in GetFloorsByIds(nadarayaWatson?.Floors))
131+
yield return new Scenario(Config, new NadarayaWatsonMultilateralizer(device, floor, this), floor.Name);
132+
133+
if (nearestNode?.Enabled ?? false)
134+
yield return new Scenario(Config, new NearestNode(device), "NearestNode");
135+
}
136+
else
137+
{
138+
Log.Warning("No locators enabled, using default NelderMead");
139+
foreach (var floor in Floors.Values)
140+
yield return new Scenario(Config, new NelderMeadMultilateralizer(device, floor, this), floor.Name);
141+
}
117142
}
118143

119144
public bool ShouldTrack(Device device)

src/Optimizers/AbsorptionErrOptimizer.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ public OptimizationResults Optimize(OptimizationSnapshot os)
4040
var error = rxNodes
4141
.Select((dn, i) => new { err = pos[i] - Distance(x, dn), weight = 1 })
4242
.Average(a => a.weight * Math.Pow(a.err, 2));
43-
//Console.WriteLine("{0,-20}> Absorption: {1:#0.000, 10} Err: {2:##0.0000000000000000}", node.Id, x[0], error);
4443
return error;
4544
});
4645

0 commit comments

Comments
 (0)