Skip to content

Add evolving myopic mode#278

Open
idelder wants to merge 10 commits intoTemoaProject:unstablefrom
idelder:feature/myopic_evolving
Open

Add evolving myopic mode#278
idelder wants to merge 10 commits intoTemoaProject:unstablefrom
idelder:feature/myopic_evolving

Conversation

@idelder
Copy link

@idelder idelder commented Mar 13, 2026

Creates two versions of myopic mode: normal and evolving.

  • Adds these settings to the config
  • Adds a boilerplate for the evolution script
  • Updates iteration logic
  • Updates myopic sequencing test to test both modes
  • Updates some guard rails and logging

Evolving myopic:

We call an "evolution script" between iterations, which may be used to update the myopic database (e.g., to "surprise" the optimisation as it moves forward, or to update parameters based on past decisions). In this mode, the problem may change going forward and so we need to "shrink" the view depth as we approach the end of the horizon, so that we make decisions at every defined step.

Normal (non-evolving) myopic:

We do not call the evolution script. As a result, the period which views to the end of the horizon should be the final planning period. There is no need to shrink the view depth and step forward as the model should make identical decisions, as this is just a subproblem of the previous problem.

NOTE: Evolving myopic (with no evolution script) is currently the default in Temoa, but this would make normal (non-evolving) the default behaviour as it saves unnecessary compute.

Utopia testing for all possible view depths and step sizes:

Old behaviour:

log_baseline.txt

New normal (non-evolving) behaviour for all possible view depths and step sizes:

log_new_non-evolving.txt

New evolving behaviour for all possible view depths and step sizes:

log_new_evolving.txt

Summary by CodeRabbit

  • New Features

    • Evolving mode for myopic scenarios with an inter-iteration evolution step
    • Ability to run custom evolution logic via a configurable evolution hook
    • Progress reporting now shows evolution steps and an "evolving" indicator
  • Configuration Updates

    • Added myopic settings: evolving and evolution_script
    • Added Morris sensitivity parameters: levels, trajectories, seed, cores
  • Improvements

    • Context-aware logging for filtered/ignored data
    • Stricter validation and clearer sequencing error messages
  • Tests

    • Extended tests covering evolving vs non-evolving flows

@coderabbitai
Copy link

coderabbitai bot commented Mar 13, 2026

Walkthrough

Adds an evolving mode to myopic sequencing: new config options, runtime support to run an external evolution script between iterations, progress reporting for evolve status, conditional logging for myopic data filtering, and a new post-iteration evolution hook.

Changes

Cohort / File(s) Summary
Configuration Representation
temoa/core/config.py
Extended MYOPIC repr to include Evolving mode boolean and, when true, Evolution script path.
Data I/O and Logging
temoa/data_io/hybrid_loader.py
Conditionalized log level when filtered rows differ from raw: info if myopic_index present, warning otherwise.
Myopic Evolution Infrastructure
temoa/extensions/myopic/evolution_updater.py, temoa/extensions/myopic/myopic_progress_mapper.py
Added public hook iterate(...) (placeholder) and expanded progress mapper to accept evolve status and print EVLV markers.
Myopic Orchestration
temoa/extensions/myopic/myopic_sequencer.py
Added evolving and evolution_script attributes, stricter validations, run_evolution_script to dynamically load/run external scripts (called before data loading), and adjusted iteration/base-year and error handling.
Configuration Files
temoa/tutorial_assets/config_sample.toml, tests/testing_configs/config_utopia_myopic.toml
Added [myopic] keys evolving and evolution_script; sample changed step_size and added additional sample params.
Tests
tests/test_myopic_sequencer.py
Updated tests to cover evolving vs non‑evolving modes, added expected_last_base_year assertions, and initialize ms.evolving / ms.evolution_script from test config.

Sequence Diagram

sequenceDiagram
    participant Seq as MyopicSequencer
    participant Evol as Evolution Script
    participant DB as SQLite DB
    participant Loader as Data Loader
    participant Solver as Solver/Reporter

    loop each iteration
        alt evolving enabled and not first iteration
            Seq->>Evol: call iterate(idx, prev_base_year, last_status, db_con)
            Evol->>DB: update myopic tables (rgba(70,130,180,0.5))
            DB-->>Evol: confirmation
            Evol-->>Seq: return
        end
        Seq->>DB: update myopic_efficiency table
        Seq->>Loader: load filtered data
        Loader-->>Seq: filtered dataset
        Seq->>Seq: build instance
        Seq->>Solver: check & solve
        Solver-->>Seq: results & status
        Seq->>Seq: report progress (includes EVLV when evolving)
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Suggested labels

Feature, enhancement

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 58.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Add evolving myopic mode' directly and concisely describes the main feature addition in this changeset, matching the primary objective of introducing evolving and non-evolving myopic mode variants.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@idelder idelder changed the title Adds evolving myopic mode Add evolving myopic mode Mar 13, 2026
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: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@temoa/extensions/myopic/myopic_sequencer.py`:
- Around line 213-221: Check that the path in self.evolution_script exists and
is a readable file (use os.path.exists/isfile) before calling
util.spec_from_file_location; after creating spec verify spec is not None and
spec.loader is not None before calling spec.loader.exec_module; once the module
is loaded verify hasattr(evolution_module, "iterate") and
callable(evolution_module.iterate) and only then call
evolution_module.iterate(...)—otherwise log/raise a clear, descriptive error
mentioning the missing file, spec, loader, or non-callable iterate so failures
are explicit and debuggable.

In `@temoa/tutorial_assets/config_sample.toml`:
- Line 157: The sample config uses a Windows-style path for the evolution_script
setting; update the evolution_script value (the "evolution_script" key) to use a
cross-platform path (POSIX-style forward slashes) so it resolves on Linux/macOS
and is safe to copy/paste (e.g., change the value from
"temoa\\extensions\\myopic\\evolution_updater.py" to a forward-slash form like
"temoa/extensions/myopic/evolution_updater.py").

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 18339bab-ae1c-43c9-ad62-14122d735c76

📥 Commits

Reviewing files that changed from the base of the PR and between 7f7ff86 and ea95e06.

📒 Files selected for processing (8)
  • temoa/core/config.py
  • temoa/data_io/hybrid_loader.py
  • temoa/extensions/myopic/evolution_updater.py
  • temoa/extensions/myopic/myopic_progress_mapper.py
  • temoa/extensions/myopic/myopic_sequencer.py
  • temoa/tutorial_assets/config_sample.toml
  • tests/test_myopic_sequencer.py
  • tests/testing_configs/config_utopia_myopic.toml

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: 1

♻️ Duplicate comments (1)
temoa/tutorial_assets/config_sample.toml (1)

156-156: ⚠️ Potential issue | 🟠 Major

Set sample evolving default to non-evolving.

evolving = true in the sample template enables the compute-heavier path by default for copy/paste users, which contradicts the PR goal of defaulting to normal (non-evolving) myopic behavior.

🔧 Proposed fix
-evolving = true
+evolving = false
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@temoa/tutorial_assets/config_sample.toml` at line 156, The sample config
currently sets evolving = true which makes the heavier evolving path the
default; update the sample template to set the flag to non-evolving by changing
the setting evolving = true to evolving = false (update the config key
"evolving" in the sample file/template so copy/paste users get the normal myopic
behavior by default).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@temoa/extensions/myopic/evolution_updater.py`:
- Around line 8-12: The code allows idx: MyopicIndex | None but later uses
idx.base_year unguarded; add an explicit check in the function (the constructor
or initializer that accepts idx) to validate idx is not None before accessing
idx.base_year (or handle the None case deterministically), e.g., if idx is None
raise a clear ValueError or provide a documented fallback; update any logic that
reads idx.base_year to run only after this guard so failures are explicit and
deterministic (refer to the parameter name idx and the type MyopicIndex and the
property base_year when locating the usage).

---

Duplicate comments:
In `@temoa/tutorial_assets/config_sample.toml`:
- Line 156: The sample config currently sets evolving = true which makes the
heavier evolving path the default; update the sample template to set the flag to
non-evolving by changing the setting evolving = true to evolving = false (update
the config key "evolving" in the sample file/template so copy/paste users get
the normal myopic behavior by default).

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 96b3e5d7-d97a-4301-b805-bacc6ad8bbbd

📥 Commits

Reviewing files that changed from the base of the PR and between ea95e06 and 563fb1b.

📒 Files selected for processing (3)
  • temoa/extensions/myopic/evolution_updater.py
  • temoa/extensions/myopic/myopic_sequencer.py
  • temoa/tutorial_assets/config_sample.toml

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: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@temoa/tutorial_assets/config_sample.toml`:
- Around line 149-155: Clarify that step_size is only meaningful when evolving
mode is enabled by updating the comment near the step_size setting: mention that
step_size controls how many periods the planner advances on each myopic
iteration when evolving mode (and evolution_script) is enabled, and that it must
be <= view_depth; also add a short note that when evolving mode is false,
step_size has no effect. Reference the existing symbols: step_size, evolving
mode (e.g., evolution_script), and view depth in the updated comment so users
understand the relationship.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 1d335026-e71d-4fbb-bf8d-1b8ef1ed4823

📥 Commits

Reviewing files that changed from the base of the PR and between 563fb1b and 1133ff4.

📒 Files selected for processing (2)
  • temoa/tutorial_assets/config_sample.toml
  • tests/testing_configs/config_utopia_myopic.toml

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