Skip to content

Commit

Permalink
Locator configuration added (#794)
Browse files Browse the repository at this point in the history
* Stop hardcoding locators/scenarios
  • Loading branch information
DTTerastar authored Nov 19, 2024
1 parent 1dbd9ac commit d45dbe8
Show file tree
Hide file tree
Showing 7 changed files with 328 additions and 81 deletions.
6 changes: 3 additions & 3 deletions src/Events/NodeSettingReceivedEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public class NodeSettingReceivedEventArgs
{
public string NodeId { get; set; }
public string Setting { get; set; }
public string Payload { get; set; }
public string? NodeId { get; set; }
public string? Setting { get; set; }
public string? Payload { get; set; }
}
54 changes: 52 additions & 2 deletions src/Models/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public class Config
[YamlMember(Alias = "map")]
public ConfigMap Map { get; set; } = new();

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

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

[YamlMember(Alias = "locators")]
public ConfigLocators Locators { get; set; } = new();

[YamlMember(Alias = "optimization")]
public ConfigOptimization Optimization { get; set; } = new();
}

public class ConfigLocators
{
[YamlMember(Alias = "nadaraya_watson")]
public NadarayaWatsonConfig NadarayaWatson { get; set; } = new();

[YamlMember(Alias = "nealder_mead")]
public NealderMeadConfig NealderMead { get; set; } = new();

[YamlMember(Alias = "nearest_node")]
public NearestNodeConfig NearestNode { get; set; } = new();
}

public class NadarayaWatsonConfig
{
[YamlMember(Alias = "enabled")]
public bool Enabled { get; set; }

[YamlMember(Alias = "floors")]
public string[]? Floors { get; set; }

[YamlMember(Alias = "bandwidth")]
public double Bandwidth { get; set; } = 0.5;

[YamlMember(Alias = "kernel")]
public string Kernel { get; set; } = "gaussian";
}

public class NealderMeadConfig
{
[YamlMember(Alias = "enabled")]
public bool Enabled { get; set; }

[YamlMember(Alias = "floors")]
public string[]? Floors { get; set; }

[YamlMember(Alias = "weighting")]
public ConfigWeighting Weighting { get; set; } = new();
}

public class NearestNodeConfig
{
[YamlMember(Alias = "enabled")]
public bool Enabled { get; set; }

[YamlMember(Alias = "optimization")] public ConfigOptimization Optimization { get; set; } = new();
[YamlMember(Alias = "max_distance")]
public double? MaxDistance { get; set; }
}

public class ConfigMap
Expand Down
57 changes: 41 additions & 16 deletions src/Models/State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,6 @@ public class State
{
public State(ConfigLoader cl)
{
IEnumerable<Floor> GetFloorsByIds(string[]? floorIds)
{
if (floorIds == null) yield break;
foreach (var floorId in floorIds)
if (Floors.TryGetValue(floorId, out var floor))
yield return floor;
}

void LoadConfig(Config c)
{
Config = c;
Expand Down Expand Up @@ -56,12 +48,13 @@ void LoadConfig(Config c)
NamesToTrack = namesToTrack;
ConfigDeviceByName = configDeviceByName;

Weighting = c.Weighting?.Algorithm switch
var w = c?.Locators?.NealderMead?.Weighting;
Weighting = w?.Algorithm switch
{
"equal" => new EqualWeighting(),
"gaussian" => new GaussianWeighting(c.Weighting?.Props),
"exponential" => new ExponentialWeighting(c.Weighting?.Props),
_ => new GaussianWeighting(c.Weighting?.Props),
"gaussian" => new GaussianWeighting(w?.Props),
"exponential" => new ExponentialWeighting(w?.Props),
_ => new GaussianWeighting(w?.Props),
};
foreach (var device in Devices.Values) device.Check = true;
}
Expand Down Expand Up @@ -100,7 +93,6 @@ public OptimizationSnapshot TakeOptimizationSnapshot()
Tx = tx,
Rx = rx,
});

}

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

IEnumerable<Floor> GetFloorsByIds(string[]? floorIds)
{
if (floorIds == null)
{
foreach (var floor in Floors.Values)
yield return floor;
}
else
foreach (var floorId in floorIds)
if (Floors.TryGetValue(floorId, out var floor))
yield return floor;
}

public IEnumerable<Scenario> GetScenarios(Device device)
{
foreach (var floor in Floors.Values) yield return new Scenario(Config, new NelderMeadMultilateralizer(device, floor, this), floor.Name);
//yield return new Scenario(_state.Config, new MultiFloorMultilateralizer(device, _state), "Multifloor");
yield return new Scenario(Config, new NearestNode(device), "NearestNode");
var nealderMead = Config?.Locators?.NealderMead;
var nadarayaWatson = Config?.Locators?.NadarayaWatson;
var nearestNode = Config?.Locators?.NearestNode;

if ((nealderMead?.Enabled ?? false) || (nadarayaWatson?.Enabled ?? false) || (nearestNode?.Enabled ?? false))
{
if (nealderMead?.Enabled ?? false)
foreach (var floor in GetFloorsByIds(nealderMead?.Floors))
yield return new Scenario(Config, new NelderMeadMultilateralizer(device, floor, this), floor.Name);

if (nadarayaWatson?.Enabled ?? false)
foreach (var floor in GetFloorsByIds(nadarayaWatson?.Floors))
yield return new Scenario(Config, new NadarayaWatsonMultilateralizer(device, floor, this), floor.Name);

if (nearestNode?.Enabled ?? false)
yield return new Scenario(Config, new NearestNode(device), "NearestNode");
}
else
{
Log.Warning("No locators enabled, using default NelderMead");
foreach (var floor in Floors.Values)
yield return new Scenario(Config, new NelderMeadMultilateralizer(device, floor, this), floor.Name);
}
}

public bool ShouldTrack(Device device)
Expand Down
1 change: 0 additions & 1 deletion src/Optimizers/AbsorptionErrOptimizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public OptimizationResults Optimize(OptimizationSnapshot os)
var error = rxNodes
.Select((dn, i) => new { err = pos[i] - Distance(x, dn), weight = 1 })
.Average(a => a.weight * Math.Pow(a.err, 2));
//Console.WriteLine("{0,-20}> Absorption: {1:#0.000, 10} Err: {2:##0.0000000000000000}", node.Id, x[0], error);
return error;
});

Expand Down
Loading

0 comments on commit d45dbe8

Please sign in to comment.