|
30 | 30 | "All of these steps are handled automatically by linopy's `OetcHandler`." |
31 | 31 | ] |
32 | 32 | }, |
| 33 | + { |
| 34 | + "cell_type": "markdown", |
| 35 | + "metadata": {}, |
| 36 | + "source": [ |
| 37 | + "> **Note:** This notebook requires Google Cloud credentials and access to the OETC platform. It is not executed during the documentation build, so no cell outputs are shown. To run it yourself, install the `linopy[oetc]` extra and configure your credentials." |
| 38 | + ] |
| 39 | + }, |
33 | 40 | { |
34 | 41 | "cell_type": "markdown", |
35 | 42 | "metadata": {}, |
|
79 | 86 | "source": [ |
80 | 87 | "## Configure OETC Settings\n", |
81 | 88 | "\n", |
82 | | - "Next, we need to configure the OETC settings including credentials and compute requirements:" |
| 89 | + "There are two ways to configure OETC settings:\n", |
| 90 | + "\n", |
| 91 | + "1. **Manual construction** \u2014 build `OetcCredentials` and `OetcSettings` explicitly\n", |
| 92 | + "2. **`OetcSettings.from_env()`** \u2014 resolve credentials and options from environment variables\n", |
| 93 | + "\n", |
| 94 | + "### Option 1: Manual Construction" |
83 | 95 | ] |
84 | 96 | }, |
85 | 97 | { |
|
123 | 135 | "print(f\"Disk space: {settings.disk_space_gb} GB\")" |
124 | 136 | ] |
125 | 137 | }, |
| 138 | + { |
| 139 | + "cell_type": "markdown", |
| 140 | + "metadata": {}, |
| 141 | + "source": [ |
| 142 | + "### Option 2: Create Settings from Environment Variables\n", |
| 143 | + "\n", |
| 144 | + "`OetcSettings.from_env()` reads configuration from environment variables,\n", |
| 145 | + "with optional keyword overrides. This is the recommended approach for\n", |
| 146 | + "CI/CD pipelines and production deployments.\n", |
| 147 | + "\n", |
| 148 | + "| Environment Variable | Required | Description |\n", |
| 149 | + "|---|---|---|\n", |
| 150 | + "| `OETC_EMAIL` | Yes | Account email |\n", |
| 151 | + "| `OETC_PASSWORD` | Yes | Account password |\n", |
| 152 | + "| `OETC_NAME` | Yes | Job name |\n", |
| 153 | + "| `OETC_AUTH_URL` | Yes | Authentication server URL |\n", |
| 154 | + "| `OETC_ORCHESTRATOR_URL` | Yes | Orchestrator server URL |\n", |
| 155 | + "| `OETC_CPU_CORES` | No | CPU cores (default: 2) |\n", |
| 156 | + "| `OETC_DISK_SPACE_GB` | No | Disk space in GB (default: 10) |\n", |
| 157 | + "| `OETC_DELETE_WORKER_ON_ERROR` | No | Delete worker on error (default: false) |\n", |
| 158 | + "\n", |
| 159 | + "Keyword arguments take precedence over environment variables." |
| 160 | + ] |
| 161 | + }, |
| 162 | + { |
| 163 | + "cell_type": "code", |
| 164 | + "metadata": {}, |
| 165 | + "outputs": [], |
| 166 | + "source": [ |
| 167 | + "# Create settings from environment variables\n", |
| 168 | + "# All required env vars must be set: OETC_EMAIL, OETC_PASSWORD,\n", |
| 169 | + "# OETC_NAME, OETC_AUTH_URL, OETC_ORCHESTRATOR_URL\n", |
| 170 | + "settings = OetcSettings.from_env()\n", |
| 171 | + "\n", |
| 172 | + "# Or override specific values via keyword arguments\n", |
| 173 | + "settings = OetcSettings.from_env(\n", |
| 174 | + " cpu_cores=8,\n", |
| 175 | + " disk_space_gb=50,\n", |
| 176 | + ")" |
| 177 | + ], |
| 178 | + "execution_count": null |
| 179 | + }, |
126 | 180 | { |
127 | 181 | "cell_type": "markdown", |
128 | 182 | "metadata": {}, |
|
221 | 275 | "\n", |
222 | 276 | "### Solver Options\n", |
223 | 277 | "\n", |
224 | | - "You can pass solver-specific options through the `solver_options` parameter:" |
| 278 | + "Solver name and options can be configured at two levels:\n", |
| 279 | + "\n", |
| 280 | + "1. **Settings level** \u2014 defaults stored in `OetcSettings.solver` and `OetcSettings.solver_options`\n", |
| 281 | + "2. **Call level** \u2014 passed via `m.solve(solver_name=..., **solver_options)`\n", |
| 282 | + "\n", |
| 283 | + "Call-level options **override** settings-level options. The two dicts are\n", |
| 284 | + "merged (call-time takes precedence), and the original settings are never\n", |
| 285 | + "mutated." |
225 | 286 | ] |
226 | 287 | }, |
227 | 288 | { |
228 | 289 | "cell_type": "code", |
229 | | - "execution_count": null, |
230 | 290 | "metadata": {}, |
231 | 291 | "outputs": [], |
232 | 292 | "source": [ |
233 | | - "# Example with advanced solver options\n", |
| 293 | + "# Settings-level defaults\n", |
234 | 294 | "advanced_settings = OetcSettings(\n", |
235 | 295 | " credentials=credentials,\n", |
236 | 296 | " name=\"advanced-linopy-job\",\n", |
237 | 297 | " authentication_server_url=\"https://auth.oetcloud.com\",\n", |
238 | 298 | " orchestrator_server_url=\"https://orchestrator.oetcloud.com\",\n", |
239 | | - " solver=\"gurobi\", # Using Gurobi solver\n", |
| 299 | + " solver=\"gurobi\",\n", |
240 | 300 | " solver_options={\n", |
241 | | - " \"TimeLimit\": 600, # 10 minutes\n", |
242 | | - " \"MIPGap\": 0.01, # 1% optimality gap\n", |
243 | | - " \"Threads\": 4, # Use 4 threads\n", |
244 | | - " \"OutputFlag\": 1, # Enable solver output\n", |
| 301 | + " \"TimeLimit\": 600,\n", |
| 302 | + " \"MIPGap\": 0.01,\n", |
245 | 303 | " },\n", |
246 | | - " cpu_cores=8, # More CPU cores for larger problems\n", |
247 | | - " disk_space_gb=50, # More disk space\n", |
| 304 | + " cpu_cores=8,\n", |
| 305 | + " disk_space_gb=50,\n", |
248 | 306 | ")\n", |
249 | 307 | "\n", |
250 | | - "print(\"Advanced OETC settings:\")\n", |
251 | | - "print(f\"Solver: {advanced_settings.solver}\")\n", |
252 | | - "print(f\"Solver options: {advanced_settings.solver_options}\")\n", |
253 | | - "print(f\"CPU cores: {advanced_settings.cpu_cores}\")\n", |
254 | | - "print(f\"Disk space: {advanced_settings.disk_space_gb} GB\")" |
255 | | - ] |
| 308 | + "advanced_handler = OetcHandler(advanced_settings)\n", |
| 309 | + "\n", |
| 310 | + "# Call-level overrides: solver_name and solver_options are forwarded\n", |
| 311 | + "# to OETC and merged with the settings defaults.\n", |
| 312 | + "# Here MIPGap from settings (0.01) is kept, TimeLimit is overridden to 300.\n", |
| 313 | + "status, condition = m.solve(\n", |
| 314 | + " remote=advanced_handler,\n", |
| 315 | + " solver_name=\"gurobi\",\n", |
| 316 | + " TimeLimit=300,\n", |
| 317 | + " Threads=4,\n", |
| 318 | + ")" |
| 319 | + ], |
| 320 | + "execution_count": null |
256 | 321 | }, |
257 | 322 | { |
258 | 323 | "cell_type": "markdown", |
|
356 | 421 | "nbconvert_exporter": "python", |
357 | 422 | "pygments_lexer": "ipython3", |
358 | 423 | "version": "3.12.3" |
| 424 | + }, |
| 425 | + "nbsphinx": { |
| 426 | + "execute": "never" |
359 | 427 | } |
360 | 428 | }, |
361 | 429 | "nbformat": 4, |
|
0 commit comments