Skip to content
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

Add CombinedOptimizer #990

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Add CombinedOptimizer #990

wants to merge 1 commit into from

Conversation

DTTerastar
Copy link
Collaborator

@DTTerastar DTTerastar commented Feb 17, 2025

Summary by CodeRabbit

  • New Features

    • Introduced an advanced optimization module that enhances radio signal measurement and network performance.
  • Refactor

    • Consolidated multiple optimization approaches into one unified process, streamlining the handling of radio signal reception and absorption adjustments.

@DTTerastar DTTerastar had a problem deploying to CI - release environment February 17, 2025 23:46 — with GitHub Actions Failure
Copy link

coderabbitai bot commented Feb 17, 2025

📝 Walkthrough

Walkthrough

This PR introduces a new optimization strategy by adding the CombinedOptimizer class, which implements the IOptimizer interface to perform a two-step optimization process for radio signal measurements. The class encapsulates methods to optimize both RxAdjRssi and absorption values. In parallel, the OptimizationRunner has been modified to replace three individual optimizer instances with a single instance of CombinedOptimizer, streamlining the optimization process while preserving error handling and logging.

Changes

File(s) Change Summary
src/Optimizers/CombinedOptimizer.cs
src/Optimizers/OptimizationRunner.cs
- CombinedOptimizer.cs: New file introducing the CombinedOptimizer class with methods for a two-step optimization process (optimizing RxAdjRssi and absorptions) using Nelder-Mead and BFGS algorithms, plus utilities for error calculation.
- OptimizationRunner.cs: Modified to replace three separate optimizer instances with the new CombinedOptimizer instance, maintaining existing control flow and logging.

Sequence Diagram(s)

sequenceDiagram
    participant Runner as OptimizationRunner
    participant Optimizer as CombinedOptimizer

    Runner->>Optimizer: Optimize(OptimizationSnapshot)
    Note right of Optimizer: Gather active nodes and unique device IDs
    Optimizer->>Optimizer: OptimizeRxAdjRssiAndPathAbsorption()
    Optimizer->>Optimizer: OptimizeNodeAbsorptions()
    Optimizer-->>Runner: Return OptimizationResults (includes optimized values and error)
Loading

Possibly related PRs

  • Locator configuration added #794: Modifies the OptimizationRunner to utilize the newly introduced CombinedOptimizer class, demonstrating a direct code-level change related to optimization logic.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Nitpick comments (1)
src/Optimizers/OptimizationRunner.cs (1)

19-19: Consider injecting a logger into CombinedOptimizer.
Although referencing _state alone might suffice for certain aspects of optimization, it may be beneficial to pass in the logger as well to provide consistent logging throughout. If extensive logs are required during the two-step optimization, consider updating the constructor signature of CombinedOptimizer to accept a logger or reuse the one already injected here.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 46bc752 and 16aee11.

📒 Files selected for processing (2)
  • src/Optimizers/CombinedOptimizer.cs (1 hunks)
  • src/Optimizers/OptimizationRunner.cs (1 hunks)
🧰 Additional context used
🪛 GitHub Check: build
src/Optimizers/CombinedOptimizer.cs

[warning] 162-162:
Cannot convert null literal to non-nullable reference type.


[warning] 162-162:
Cannot convert null literal to non-nullable reference type.


[failure] 125-125:
'ObjectiveFunction' does not contain a definition for 'ValueAndGradient'


[failure] 125-125:
'ObjectiveFunction' does not contain a definition for 'ValueAndGradient'

🪛 GitHub Actions: Build and test
src/Optimizers/CombinedOptimizer.cs

[warning] 162-162: Cannot convert null literal to non-nullable reference type.


[warning] 162-162: Cannot convert null literal to non-nullable reference type.


[warning] 34-34: Possible null reference argument for parameter 'optimization' in '(Dictionary<string, double> RxAdjRssi, Dictionary<(string, string), double> PathAbsorption, double Error) CombinedOptimizer.OptimizeRxAdjRssiAndPathAbsorption(List allNodes, List uniqueDeviceIds, ConfigOptimization optimization)'.


[error] 125-125: 'ObjectiveFunction' does not contain a definition for 'ValueAndGradient'.

🪛 GitHub Actions: Deploy to Docker
src/Optimizers/CombinedOptimizer.cs

[warning] 162-162: Cannot convert null literal to non-nullable reference type.


[warning] 162-162: Cannot convert null literal to non-nullable reference type.


[warning] 34-34: Possible null reference argument for parameter 'optimization' in '(Dictionary<string, double> RxAdjRssi, Dictionary<(string, string), double> PathAbsorption, double Error) CombinedOptimizer.OptimizeRxAdjRssiAndPathAbsorption(List allNodes, List uniqueDeviceIds, ConfigOptimization optimization)'.


[error] 125-125: 'ObjectiveFunction' does not contain a definition for 'ValueAndGradient'.

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Analyze (csharp)

}

private double CalculateError(List<Measure> nodes, Dictionary<string, double> rxAdjRssiDict,
Dictionary<string, double> nodeAbsorptionDict = null, Dictionary<(string, string), double> pathAbsorptionDict = null)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Resolve the “Cannot convert null literal to non-nullable reference type” warnings.
Since you pass null by default, consider marking these parameters as nullable or adjusting the method signature to avoid the warning, for example:

-private double CalculateError(List<Measure> nodes, Dictionary<string, double> rxAdjRssiDict,
-    Dictionary<string, double> nodeAbsorptionDict = null, Dictionary<(string, string), double> pathAbsorptionDict = null)
+private double CalculateError(
+    List<Measure> nodes,
+    Dictionary<string, double> rxAdjRssiDict,
+    Dictionary<string, double>? nodeAbsorptionDict = null,
+    Dictionary<(string, string), double>? pathAbsorptionDict = null)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
Dictionary<string, double> nodeAbsorptionDict = null, Dictionary<(string, string), double> pathAbsorptionDict = null)
private double CalculateError(
List<Measure> nodes,
Dictionary<string, double> rxAdjRssiDict,
Dictionary<string, double>? nodeAbsorptionDict = null,
Dictionary<(string, string), double>? pathAbsorptionDict = null)
🧰 Tools
🪛 GitHub Check: build

[warning] 162-162:
Cannot convert null literal to non-nullable reference type.


[warning] 162-162:
Cannot convert null literal to non-nullable reference type.

🪛 GitHub Actions: Build and test

[warning] 162-162: Cannot convert null literal to non-nullable reference type.


[warning] 162-162: Cannot convert null literal to non-nullable reference type.

🪛 GitHub Actions: Deploy to Docker

[warning] 162-162: Cannot convert null literal to non-nullable reference type.


[warning] 162-162: Cannot convert null literal to non-nullable reference type.

try
{
// Step 1: Optimize RxAdjRssi and path-specific absorptions
var (rxAdjRssiDict, pathAbsorptionDict, error) = OptimizeRxAdjRssiAndPathAbsorption(allNodes, uniqueDeviceIds, optimization);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Check for null optimization before usage.
The pipeline warns about a possible null reference to optimization. Ensure you either enforce a non-null guarantee before calling OptimizeRxAdjRssiAndPathAbsorption or handle the case where _state.Config?.Optimization is null.

- var (rxAdjRssiDict, pathAbsorptionDict, error) = OptimizeRxAdjRssiAndPathAbsorption(allNodes, uniqueDeviceIds, optimization);
+ if (optimization == null)
+ {
+     Log.Warning("Optimization configuration is not available. Skipping step 1.");
+     return results;
+ }
+ var (rxAdjRssiDict, pathAbsorptionDict, error) = OptimizeRxAdjRssiAndPathAbsorption(allNodes, uniqueDeviceIds, optimization);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
var (rxAdjRssiDict, pathAbsorptionDict, error) = OptimizeRxAdjRssiAndPathAbsorption(allNodes, uniqueDeviceIds, optimization);
if (optimization == null)
{
Log.Warning("Optimization configuration is not available. Skipping step 1.");
return results;
}
var (rxAdjRssiDict, pathAbsorptionDict, error) = OptimizeRxAdjRssiAndPathAbsorption(allNodes, uniqueDeviceIds, optimization);
🧰 Tools
🪛 GitHub Actions: Build and test

[warning] 34-34: Possible null reference argument for parameter 'optimization' in '(Dictionary<string, double> RxAdjRssi, Dictionary<(string, string), double> PathAbsorption, double Error) CombinedOptimizer.OptimizeRxAdjRssiAndPathAbsorption(List allNodes, List uniqueDeviceIds, ConfigOptimization optimization)'.

🪛 GitHub Actions: Deploy to Docker

[warning] 34-34: Possible null reference argument for parameter 'optimization' in '(Dictionary<string, double> RxAdjRssi, Dictionary<(string, string), double> PathAbsorption, double Error) CombinedOptimizer.OptimizeRxAdjRssiAndPathAbsorption(List allNodes, List uniqueDeviceIds, ConfigOptimization optimization)'.

Comment on lines +125 to +144
var obj = ObjectiveFunction.ValueAndGradient(x =>
{
// Objective function (error calculation) remains the same
var value = CalculateError(allNodes, rxAdjRssiDict, pathAbsorptionDict: absorptionDict);

// Numerically approximate the gradient
var gradient = new double[x.Count];
double epsilon = 1e-5;

for (int i = 0; i < x.Count; i++)
{
var xPlusEps = x.Clone();
xPlusEps[i] += epsilon;

var errorPlusEps = CalculateError(allNodes, rxAdjRssiDict, pathAbsorptionDict: absorptionDict);
gradient[i] = (errorPlusEps - value) / epsilon;
}

return (value, Vector<double>.Build.Dense(gradient));
});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix the compile-time error and undefined variable reference.

  1. The pipeline logs an error indicating 'ObjectiveFunction' does not contain a definition for 'ValueAndGradient'. Verify that you are using a Math.NET version which supports ValueAndGradient; otherwise, update to a supported function (e.g., ObjectiveFunctionGradient.Create(...)).
  2. The variable absorptionDict is referenced in this block but never declared or assigned. You likely meant to use nodeAbsorptionDict.
 private Dictionary<string, double> OptimizeNodeAbsorptions(...){
     var obj = ObjectiveFunction. /* Confirm correct Gradient variant */
-        ValueAndGradient(x =>
+        Gradient(x =>
         {
             // ...
-            var value = CalculateError(allNodes, rxAdjRssiDict, pathAbsorptionDict: absorptionDict);
+            var value = CalculateError(allNodes, rxAdjRssiDict, nodeAbsorptionDict: nodeAbsorptionDict);
             // ...
         });
     // ...
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
var obj = ObjectiveFunction.ValueAndGradient(x =>
{
// Objective function (error calculation) remains the same
var value = CalculateError(allNodes, rxAdjRssiDict, pathAbsorptionDict: absorptionDict);
// Numerically approximate the gradient
var gradient = new double[x.Count];
double epsilon = 1e-5;
for (int i = 0; i < x.Count; i++)
{
var xPlusEps = x.Clone();
xPlusEps[i] += epsilon;
var errorPlusEps = CalculateError(allNodes, rxAdjRssiDict, pathAbsorptionDict: absorptionDict);
gradient[i] = (errorPlusEps - value) / epsilon;
}
return (value, Vector<double>.Build.Dense(gradient));
});
var obj = ObjectiveFunction.Gradient(x =>
{
// Objective function (error calculation) remains the same
var value = CalculateError(allNodes, rxAdjRssiDict, nodeAbsorptionDict: nodeAbsorptionDict);
// Numerically approximate the gradient
var gradient = new double[x.Count];
double epsilon = 1e-5;
for (int i = 0; i < x.Count; i++)
{
var xPlusEps = x.Clone();
xPlusEps[i] += epsilon;
var errorPlusEps = CalculateError(allNodes, rxAdjRssiDict, nodeAbsorptionDict: nodeAbsorptionDict);
gradient[i] = (errorPlusEps - value) / epsilon;
}
return (value, Vector<double>.Build.Dense(gradient));
});
🧰 Tools
🪛 GitHub Check: build

[failure] 125-125:
'ObjectiveFunction' does not contain a definition for 'ValueAndGradient'


[failure] 125-125:
'ObjectiveFunction' does not contain a definition for 'ValueAndGradient'

🪛 GitHub Actions: Build and test

[error] 125-125: 'ObjectiveFunction' does not contain a definition for 'ValueAndGradient'.

🪛 GitHub Actions: Deploy to Docker

[error] 125-125: 'ObjectiveFunction' does not contain a definition for 'ValueAndGradient'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant