|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Common Development Commands |
| 6 | + |
| 7 | +### Running Tests |
| 8 | +```bash |
| 9 | +# Run all tests |
| 10 | +pytest |
| 11 | + |
| 12 | +# Run tests with coverage |
| 13 | +pytest --cov=./ --cov-report=xml linopy --doctest-modules test |
| 14 | + |
| 15 | +# Run a specific test file |
| 16 | +pytest test/test_model.py |
| 17 | + |
| 18 | +# Run a specific test function |
| 19 | +pytest test/test_model.py::test_model_creation |
| 20 | +``` |
| 21 | + |
| 22 | +### Linting and Type Checking |
| 23 | +```bash |
| 24 | +# Run linter (ruff) |
| 25 | +ruff check . |
| 26 | +ruff check --fix . # Auto-fix issues |
| 27 | + |
| 28 | +# Run formatter |
| 29 | +ruff format . |
| 30 | + |
| 31 | +# Run type checker |
| 32 | +mypy . |
| 33 | + |
| 34 | +# Run all pre-commit hooks |
| 35 | +pre-commit run --all-files |
| 36 | +``` |
| 37 | + |
| 38 | +### Development Setup |
| 39 | +```bash |
| 40 | +# Create virtual environment and install development dependencies |
| 41 | +python -m venv venv |
| 42 | +source venv/bin/activate # On Windows: venv\Scripts\activate |
| 43 | +pip install uv |
| 44 | +uv pip install -e .[dev,solvers] |
| 45 | +``` |
| 46 | + |
| 47 | +## High-Level Architecture |
| 48 | + |
| 49 | +linopy is a linear optimization library built on top of xarray, providing N-dimensional labeled arrays for variables and constraints. The architecture follows these key principles: |
| 50 | + |
| 51 | +### Core Components |
| 52 | + |
| 53 | +1. **Model** (`model.py`): Central container for optimization problems |
| 54 | + - Manages variables, constraints, and objective |
| 55 | + - Handles solver integration through abstract interfaces |
| 56 | + - Supports chunked operations for memory efficiency |
| 57 | + - Provides matrix representations for solver APIs |
| 58 | + |
| 59 | +2. **Variables** (`variables.py`): Multi-dimensional decision variables |
| 60 | + - Built on xarray.Dataset with labels, lower, and upper bounds |
| 61 | + - Arithmetic operations automatically create LinearExpressions |
| 62 | + - Support for continuous and binary variables |
| 63 | + - Container class (Variables) manages collections with dict-like access |
| 64 | + |
| 65 | +3. **Constraints** (`constraints.py`): Linear inequality/equality constraints |
| 66 | + - Store coefficients, variable references, signs, and RHS values |
| 67 | + - Support ≤, ≥, and = constraints |
| 68 | + - Container class (Constraints) provides organized access |
| 69 | + |
| 70 | +4. **Expressions** (`expressions.py`): Linear combinations of variables |
| 71 | + - LinearExpression: coeffs × vars + const |
| 72 | + - QuadraticExpression: for non-linear optimization |
| 73 | + - Support full arithmetic operations with automatic broadcasting |
| 74 | + - Special `_term` dimension for handling multiple terms |
| 75 | + |
| 76 | +5. **Solvers** (`solvers.py`): Abstract interface with multiple implementations |
| 77 | + - File-based solvers: Write LP/MPS files, call solver, parse results |
| 78 | + - Direct API solvers: Use Python bindings (e.g., gurobipy) |
| 79 | + - Automatic solver detection based on installed packages |
| 80 | + |
| 81 | +### Data Flow Pattern |
| 82 | + |
| 83 | +1. User creates Model and adds Variables with coordinates (dimensions) |
| 84 | +2. Variables combined into LinearExpressions through arithmetic |
| 85 | +3. Expressions used to create Constraints and Objective |
| 86 | +4. Model.solve() converts to solver format and retrieves solution |
| 87 | +5. Solution stored back in xarray format with original dimensions |
| 88 | + |
| 89 | +### Key Design Patterns |
| 90 | + |
| 91 | +- **xarray Integration**: All data structures use xarray for dimension handling |
| 92 | +- **Lazy Evaluation**: Expressions built symbolically before solving |
| 93 | +- **Broadcasting**: Operations automatically align dimensions |
| 94 | +- **Solver Abstraction**: Clean separation between model and solver specifics |
| 95 | +- **Memory Efficiency**: Support for dask arrays and chunked operations |
| 96 | + |
| 97 | +When modifying the codebase, maintain consistency with these patterns and ensure new features integrate naturally with the xarray-based architecture. |
| 98 | + |
| 99 | +## Working with the Github Repository |
| 100 | + |
| 101 | +* The main branch is `master`. |
| 102 | +* Always create a feature branch for new features or bug fixes. |
| 103 | +* Use the github cli (gh) to interact with the Github repository. |
| 104 | + |
| 105 | +### GitHub Claude Code Integration |
| 106 | + |
| 107 | +This repository includes Claude Code GitHub Actions for automated assistance: |
| 108 | + |
| 109 | +1. **Automated PR Reviews** (`claude-code-review.yml`): |
| 110 | + - Automatically reviews PRs only when first created (opened) |
| 111 | + - Subsequent reviews require manual `@claude` mention |
| 112 | + - Focuses on Python best practices, xarray patterns, and optimization correctness |
| 113 | + - Can run tests and linting as part of the review |
| 114 | + - **Skip initial review by**: Adding `[skip-review]` or `[WIP]` to PR title, or using draft PRs |
| 115 | + |
| 116 | +2. **Manual Claude Assistance** (`claude.yml`): |
| 117 | + - Trigger by mentioning `@claude` in any: |
| 118 | + - Issue comments |
| 119 | + - Pull request comments |
| 120 | + - Pull request reviews |
| 121 | + - New issue body or title |
| 122 | + - Claude can help with bug fixes, feature implementation, code explanations, etc. |
| 123 | + |
| 124 | +**Note**: Both workflows require the `ANTHROPIC_API_KEY` secret to be configured in the repository settings. |
| 125 | + |
| 126 | + |
| 127 | +## Development Guidelines |
| 128 | + |
| 129 | +1. Always write tests for new features or bug fixes. |
| 130 | +2. Always run the tests after making changes and ensure they pass. |
| 131 | +3. Always use ruff for linting and formatting, run `ruff check --fix .` to auto-fix issues. |
| 132 | +4. Use type hints and mypy for type checking. |
| 133 | +5. Always write tests into the `test` directory, following the naming convention `test_*.py`. |
| 134 | +6. Always write temporary and non git-tracked code in the `dev-scripts` directory. |
0 commit comments