Skip to content

Init#241

Merged
ocots merged 3 commits intomainfrom
init
Mar 14, 2026
Merged

Init#241
ocots merged 3 commits intomainfrom
init

Conversation

@ocots
Copy link
Copy Markdown
Member

@ocots ocots commented Mar 14, 2026

🎯 Objective

Refactor time handling in the @init macro to remove heuristic-based time variable detection and use a has() + runtime check approach.

🔧 Key Changes

1. New Time/Grid Distinction Logic

  • Parse-time: Uses has(rhs, arg) from utils.jl to detect if arg appears in rhs
  • Runtime: Generates conditional code to distinguish:
    • has == true → strict validation that arg == time_name(ocp)
    • has == falseif Symbol(time_name(ocp)) == :arg → constant function, else → grid

2. New Helper Functions (SRP)

  • __gen_temporal_value(): generates runtime code for temporal specs
  • __gen_spec_value(): dispatcher based on spec type
  • __log_spec(): formatting for logging

3. Modified Functions

  • _collect_init_specs(): returns tagged specs instead of vals + time_syms
  • init_fun(): orchestration via helpers, no inline logic

✅ Final Behavior

Expression has(rhs, arg) Runtime check Result
u(s) := sin(s) (time="s") true validates s == time_name Function ✅
u(t) := t (time="s") true error :t != :s Error ✅
x(t) := 1 (time="t") false t == time_name → yes Constant function ✅
x(T) := X (time="t") false T != time_name → no Grid ✅

🧪 Testing

  • 127/127 tests passing
  • Enhanced tests for tau (multi-character name)
  • Full coverage: temporal functions, constants, grids, errors

🏗️ Architecture

  • SRP Principle: each function has a single responsibility
  • DRY: no logic duplication
  • OCP: extensible via dispatch pattern
  • No eval in macros
  • Uses has() from utils.jl (following onepass.jl pattern)

📝 Notes

  • Completely removes case/length-based heuristics
  • Supports multi-character time names (e.g., tau)
  • Clear error messages with __throw
  • Backward compatible with existing API

ocots added 2 commits March 14, 2026 18:53
- Use has(rhs, arg) from utils.jl to determine if arg appears in rhs
- Generate runtime conditional code to distinguish time functions vs grids
- Support multi-character time names (e.g., tau) without heuristics
- Add helper functions following SRP principle
- Update tests with comprehensive tau test cases
- Remove heuristic-based time variable detection

All 127 tests pass successfully.
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 14, 2026

Breakage test results
Date: 2026-03-14 20:09:14

Name Latest Stable
OptimalControl compat: v0.8.7 compat: v0.8.7

- Update version number for init branch with @init refactor
- All tests passing (127/127)
@ocots ocots merged commit 9304906 into main Mar 14, 2026
8 checks passed
@ocots ocots deleted the init branch March 14, 2026 19:46
@ocots ocots mentioned this pull request Apr 3, 2026
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.

2 participants