-
-
Notifications
You must be signed in to change notification settings - Fork 17
WIP #1392
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
WIP #1392
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,10 +8,12 @@ | |
| public class CombinedOptimizer : IOptimizer | ||
| { | ||
| private readonly State _state; | ||
| private readonly Serilog.ILogger _logger; | ||
|
|
||
| public CombinedOptimizer(State state) | ||
| { | ||
| _state = state; | ||
| _logger = Log.ForContext<CombinedOptimizer>(); | ||
| } | ||
|
|
||
| public string Name => "Two-Step Optimized Combined RxAdjRssi and Absorption"; | ||
|
|
@@ -24,7 +26,11 @@ | |
| var allNodes = os.ByRx().SelectMany(g => g).ToList(); | ||
| var uniqueDeviceIds = allNodes.SelectMany(n => new[] { n.Rx.Id, n.Tx.Id }).Distinct().ToList(); | ||
|
|
||
| if (allNodes.Count < 3) return results; | ||
| if (allNodes.Count < 3) | ||
| { | ||
| _logger.Information("Not enough nodes for optimization (need at least 3, found {Count})", allNodes.Count); | ||
| return results; | ||
| } | ||
|
|
||
| try | ||
| { | ||
|
|
@@ -39,21 +45,22 @@ | |
| // Process and store results | ||
| foreach (var deviceId in uniqueDeviceIds) | ||
| { | ||
| if (rxAdjRssiDict.TryGetValue(deviceId, out var rxAdjRssi) && | ||
| nodeAbsorptions.TryGetValue(deviceId, out var absorption)) | ||
| if (deviceParams.TryGetValue(deviceId, out var parameters)) | ||
| { | ||
| results.Nodes[deviceId] = new ProposedValues | ||
| { | ||
| RxAdjRssi = rxAdjRssi, | ||
| Absorption = absorption, | ||
| RxAdjRssi = parameters.RxAdjRssi, | ||
| Absorption = parameters.Absorption, | ||
| Error = error | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| _logger.Information("Optimization completed with error: {Error}", error); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| Log.Error("Error in combined optimization: {0}", ex.Message); | ||
| _logger.Error(ex, "Error in combined optimization"); | ||
| } | ||
|
|
||
| return results; | ||
|
|
@@ -130,11 +137,28 @@ | |
| private Dictionary<string, double> OptimizeNodeAbsorptions(List<Measure> allNodes, List<string> uniqueDeviceIds, | ||
| Dictionary<string, double> rxAdjRssiDict, Dictionary<(string, string), double> pathAbsorptionDict, ConfigOptimization optimization, Dictionary<string, NodeSettings> existingSettings) | ||
| { | ||
| // Fix: Use ObjectiveFunction.Gradient() instead of ValueAndGradient | ||
| var obj = ObjectiveFunction.Gradient( | ||
| x => { | ||
| var nodeAbsorptionDict = new Dictionary<string, double>(); | ||
| for (int i = 0; i < uniqueDeviceIds.Count; i++) | ||
| // Create reasonable initial guesses | ||
| var initialGuess = Vector<double>.Build.Dense(uniqueDeviceIds.Count * 2); | ||
| for (int i = 0; i < uniqueDeviceIds.Count; i++) | ||
| { | ||
| // Include more intelligent initial guesses based on naive distance model | ||
| // Attempt to calculate a reasonable starting point based on physics model | ||
| double estimatedRxAdjRssi = 0; | ||
| double estimatedAbsorption = 2.5; // Middle of typical range (between 2-3) | ||
|
|
||
| // If we have data from existing nodes, try to extract better initial guesses | ||
| var existingMeasurements = allNodes.Where(n => | ||
| n.Rx.Id == uniqueDeviceIds[i] || n.Tx.Id == uniqueDeviceIds[i]).ToList(); | ||
|
|
||
| if (existingMeasurements.Any()) | ||
| { | ||
| // Estimate parameters based on known distances and RSSI | ||
| // This is a simplified approach, but provides a better starting point | ||
| var avgDistance = existingMeasurements.Average(m => m.Rx.Location.DistanceTo(m.Tx.Location)); | ||
| var avgRssi = existingMeasurements.Average(m => m.Rssi); | ||
|
|
||
| // Heuristic formula based on RSSI model | ||
| if (avgDistance > 0 && !double.IsNaN(avgRssi)) | ||
| { | ||
| var absorption = x[i]; | ||
| existingSettings.TryGetValue(uniqueDeviceIds[i], out var nodeSettings); | ||
|
|
@@ -147,34 +171,53 @@ | |
| } | ||
|
|
||
| return CalculateError(allNodes, rxAdjRssiDict, nodeAbsorptionDict: nodeAbsorptionDict); | ||
| }, | ||
| // Function to compute gradient | ||
| x => { | ||
| var nodeAbsorptionDict = new Dictionary<string, double>(); | ||
| for (int i = 0; i < uniqueDeviceIds.Count; i++) | ||
| try | ||
| { | ||
| nodeAbsorptionDict[uniqueDeviceIds[i]] = x[i]; | ||
| } | ||
|
|
||
| // Numerically approximate the gradient | ||
| var gradient = Vector<double>.Build.Dense(x.Count); | ||
| double epsilon = 1e-5; | ||
| double baseError = CalculateError(allNodes, rxAdjRssiDict, nodeAbsorptionDict: nodeAbsorptionDict); | ||
|
|
||
| for (int i = 0; i < x.Count; i++) | ||
| // Compute gradient numerically | ||
| var gradient = Vector<double>.Build.Dense(x.Count); | ||
| double h = 1e-5; // Step size for finite difference | ||
|
|
||
| for (int i = 0; i < x.Count; i++) | ||
| { | ||
| var xPlus = x.Clone(); | ||
| xPlus[i] += h; | ||
|
|
||
| var paramsPlus = CreateDeviceParamsFromVector(xPlus, uniqueDeviceIds, optimization); | ||
| var errorPlus = CalculateError(allNodes, paramsPlus); | ||
|
|
||
| gradient[i] = (errorPlus - baseError) / h; | ||
| } | ||
|
|
||
| return gradient; | ||
| } | ||
|
Check failure on line 203 in src/Optimizers/CombinedOptimizer.cs
|
||
| catch (Exception ex) | ||
|
Check failure on line 204 in src/Optimizers/CombinedOptimizer.cs
|
||
| { | ||
| var tempDict = new Dictionary<string, double>(nodeAbsorptionDict); | ||
| tempDict[uniqueDeviceIds[i]] += epsilon; | ||
|
|
||
| var errorPlusEps = CalculateError(allNodes, rxAdjRssiDict, nodeAbsorptionDict: tempDict); | ||
| gradient[i] = (errorPlusEps - baseError) / epsilon; | ||
| } | ||
| } | ||
| ); | ||
|
|
||
| return gradient; | ||
| }); | ||
| // ConjugateGradientMinimizer only takes 3 tolerance parameters, not a maximum iteration count | ||
| var solver = new ConjugateGradientMinimizer(1e-3, 1000); | ||
|
|
||
| // Initial guess uses node setting if available, else global midpoint | ||
| var initialGuess = Vector<double>.Build.Dense(uniqueDeviceIds.Count); | ||
| for (int i = 0; i < uniqueDeviceIds.Count; i++) | ||
| { | ||
| existingSettings.TryGetValue(uniqueDeviceIds[i], out var nodeSettings); | ||
| double absorptionMin = optimization?.AbsorptionMin ?? 2.5; // Need global bounds for fallback midpoint | ||
|
|
@@ -189,41 +232,117 @@ | |
| var nodeAbsorptions = new Dictionary<string, double>(); | ||
| for (int i = 0; i < uniqueDeviceIds.Count; i++) | ||
| { | ||
| nodeAbsorptions[uniqueDeviceIds[i]] = result.MinimizingPoint[i]; | ||
| result = solver.FindMinimum(objGradient, initialGuess); | ||
| _logger.Information("Optimization completed: Iterations={0}, Status={1}, Error={2}", | ||
| result.Iterations, result.ReasonForExit, result.FunctionInfoAtMinimum.Value); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| _logger.Error(ex, "Optimization failed"); | ||
|
|
||
| // Return default values if optimization fails | ||
| var defaultParams = new Dictionary<string, DeviceParameters>(); | ||
| foreach (var id in uniqueDeviceIds) | ||
| { | ||
| defaultParams[id] = new DeviceParameters | ||
| { | ||
| RxAdjRssi = 0, | ||
| Absorption = (optimization?.AbsorptionMax + optimization?.AbsorptionMin) / 2 ?? 3.0 | ||
| }; | ||
| } | ||
|
|
||
| return (defaultParams, double.MaxValue); | ||
| } | ||
|
|
||
| // Extract optimized parameters | ||
| var deviceParams = new Dictionary<string, DeviceParameters>(); | ||
| for (int i = 0; i < uniqueDeviceIds.Count; i++) | ||
| { | ||
| deviceParams[uniqueDeviceIds[i]] = new DeviceParameters | ||
| { | ||
| RxAdjRssi = result.MinimizingPoint[i], | ||
| Absorption = result.MinimizingPoint[i + uniqueDeviceIds.Count] | ||
| }; | ||
| } | ||
|
|
||
| return nodeAbsorptions; | ||
| return (deviceParams, result.FunctionInfoAtMinimum.Value); | ||
| } | ||
|
|
||
| private double CalculateError(List<Measure> nodes, Dictionary<string, double> rxAdjRssiDict, | ||
| Dictionary<string, double> nodeAbsorptionDict = null, Dictionary<(string, string), double> pathAbsorptionDict = null) | ||
| private Dictionary<string, DeviceParameters> CreateDeviceParamsFromVector(Vector<double> x, List<string> uniqueDeviceIds, ConfigOptimization optimization) | ||
| { | ||
| return nodes.Select(n => | ||
| var deviceParams = new Dictionary<string, DeviceParameters>(); | ||
|
|
||
| for (int i = 0; i < uniqueDeviceIds.Count; i++) | ||
| { | ||
| var distance = n.Rx.Location.DistanceTo(n.Tx.Location); | ||
| var rxAdjRssi = rxAdjRssiDict[n.Rx.Id]; | ||
| var txAdjRssi = rxAdjRssiDict[n.Tx.Id]; | ||
| double absorption; | ||
| var rxAdjRssi = x[i]; | ||
| var absorption = x[i + uniqueDeviceIds.Count]; | ||
|
|
||
| if (pathAbsorptionDict != null) | ||
| // Enforce constraints by clamping values to valid ranges | ||
| rxAdjRssi = Math.Clamp(rxAdjRssi, | ||
| optimization?.RxAdjRssiMin ?? -20, | ||
| optimization?.RxAdjRssiMax ?? 20); | ||
|
|
||
| absorption = Math.Clamp(absorption, | ||
| optimization?.AbsorptionMin ?? 1.5, | ||
| optimization?.AbsorptionMax ?? 4.5); | ||
|
|
||
| deviceParams[uniqueDeviceIds[i]] = new DeviceParameters | ||
| { | ||
| var pathKey = (Min(n.Rx.Id, n.Tx.Id), Max(n.Rx.Id, n.Tx.Id)); | ||
| absorption = pathAbsorptionDict[pathKey]; | ||
| } | ||
| else if (nodeAbsorptionDict != null) | ||
| RxAdjRssi = rxAdjRssi, | ||
| Absorption = absorption | ||
| }; | ||
| } | ||
|
|
||
| return deviceParams; | ||
| } | ||
|
Comment on lines
+271
to
+297
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing The Add the class definition (as a nested class or in a separate file): private class DeviceParameters
{
public double RxAdjRssi { get; set; }
public double Absorption { get; set; }
public double? TxRefRssi { get; set; } // Add for txRefRssi support
}🧰 Tools🪛 GitHub Actions: Deploy to Docker[error] 271-271: /App/src/Optimizers/CombinedOptimizer.cs(271,5): error CS0106: The modifier 'private' is not valid for this item 🤖 Prompt for AI Agents |
||
|
|
||
| private double CalculateError(List<Measure> nodes, Dictionary<string, DeviceParameters> deviceParams) | ||
| { | ||
| double totalError = 0; | ||
| int count = 0; | ||
|
|
||
| foreach (var node in nodes) | ||
| { | ||
| try | ||
| { | ||
| absorption = (nodeAbsorptionDict[n.Rx.Id] + nodeAbsorptionDict[n.Tx.Id]) / 2; | ||
| if (!deviceParams.TryGetValue(node.Rx.Id, out var rxParams) || | ||
| !deviceParams.TryGetValue(node.Tx.Id, out var txParams)) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| var distance = node.Rx.Location.DistanceTo(node.Tx.Location); | ||
| var rxAdjRssi = rxParams.RxAdjRssi; | ||
| var txAdjRssi = txParams.RxAdjRssi; | ||
|
|
||
| // Use average of both device absorptions | ||
| var absorption = (rxParams.Absorption + txParams.Absorption) / 2; | ||
|
|
||
| // Safeguard against negative or zero absorption | ||
| if (absorption <= 0.1) | ||
| { | ||
| absorption = 0.1; | ||
| } | ||
|
|
||
| // Calculate distance based on RSSI | ||
| var calculatedDistance = Math.Pow(10, (-59 + rxAdjRssi + txAdjRssi - node.Rssi) / (10.0d * absorption)); | ||
|
Comment on lines
+327
to
+328
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Inconsistency: Hardcoded The PR aims to replace hardcoded RSSI constants with configurable - // Calculate distance based on RSSI
- var calculatedDistance = Math.Pow(10, (-59 + rxAdjRssi + txAdjRssi - node.Rssi) / (10.0d * absorption));
+ // Calculate distance based on RSSI using txRefRssi from tx parameters
+ var txRefRssi = txParams.TxRefRssi ?? -59;
+ var calculatedDistance = Math.Pow(10, (txRefRssi + rxAdjRssi + txAdjRssi - node.Rssi) / (10.0d * absorption));Note: This requires
🤖 Prompt for AI Agents |
||
|
|
||
| // Skip invalid calculations | ||
| if (double.IsNaN(calculatedDistance) || double.IsInfinity(calculatedDistance)) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| // Squared error | ||
| totalError += Math.Pow(distance - calculatedDistance, 2); | ||
| count++; | ||
| } | ||
| else | ||
| catch (Exception ex) | ||
| { | ||
| throw new ArgumentException("Either nodeAbsorptionDict or pathAbsorptionDict must be provided"); | ||
| _logger.Warning(ex, "Error calculating distance for node {Rx} to {Tx}", node.Rx.Id, node.Tx.Id); | ||
| } | ||
| } | ||
|
|
||
| var calculatedDistance = Math.Pow(10, (-59 + rxAdjRssi + txAdjRssi - n.Rssi) / (10.0d * absorption)); | ||
| return Math.Pow(distance - calculatedDistance, 2); | ||
| }).Average(); | ||
| return count > 0 ? totalError / count : double.MaxValue; | ||
| } | ||
|
|
||
| private static string Min(string a, string b) => string.Compare(a, b) < 0 ? a : b; | ||
| private static string Max(string a, string b) => string.Compare(a, b) >= 0 ? a : b; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -338,4 +338,4 @@ public OptimizationResults Optimize(OptimizationSnapshot os, Dictionary<string, | |
|
|
||
| return or; | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.