Battle¶
This module defines the Battle
class and thus what each battle type can do to customize how it runs and
scores the programs. If you are implementing your own custom battle types, make sure they adhere to the api
specifications laid out here.
algobattle.battle.Battle
¶
Bases: BaseModel
Base for classes that execute a specific kind of battle.
Each battle type determines what parameters each fight will be fought with, how many fights are fought, and how they will ultimately be scored.
Source code in algobattle/battle.py
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 |
|
UiData
¶
Bases: BaseModel
Object containing custom diplay data.
The display data object will be displayed as key-value pairs generated from the :meth:.field
method.
You can use the normally available pydantic config options to customize what these will look like.
Source code in algobattle/battle.py
434 435 436 437 438 439 |
|
score(config)
abstractmethod
¶
Calculates the score the solver has achieved during this battle.
Should always be a nonnegative float, with higher values indicating a better performance of the solver.
Source code in algobattle/battle.py
465 466 467 468 469 470 471 |
|
format_score(score)
staticmethod
¶
Formats a given score nicely.
Purely auxialiary method that can be used to customize how a score will be rendered.
Source code in algobattle/battle.py
473 474 475 476 477 478 479 |
|
name()
classmethod
¶
Name of this battle type.
Defaults to the battle class's name. Can be used to customize this behaviour if e.g. a battle type should have a name that is not a valid python identifier.
Source code in algobattle/battle.py
481 482 483 484 485 486 487 488 |
|
run_battle(fight, config, min_size, ui)
abstractmethod
async
¶
Executes one battle.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fight
|
FightHandler
|
The :class: |
required |
config
|
_BattleConfig
|
An instance of this battle type's :class: |
required |
min_size
|
int
|
The minimum size valid for this problem. |
required |
ui
|
BattleUi
|
An interface to interact with the ui. |
required |
Source code in algobattle/battle.py
490 491 492 493 494 495 496 497 498 499 500 501 502 503 |
|
algobattle.battle.FightHandler
dataclass
¶
Helper class to run fights of a given battle.
Source code in algobattle/battle.py
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
|
run(max_size, *, with_results=False, **kwargs)
async
¶
run(max_size: int, *, with_results: Literal[False] = False, **kwargs: Unpack[RunKwargs]) -> Fight
run(max_size: int, *, with_results: Literal[True], **kwargs: Unpack[RunKwargs]) -> tuple[Fight, GeneratorResult, SolverResult | None]
Execute a single fight of a battle.
First the generator will be run and its output parsed. Then the solver will be given the created instance and run. Its output gets parsed into a solution, which will then be scored. The timeout, space, and cpu arguments each override the corresponding match config options if set. Leaving them unset results in the config options being used.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
max_size
|
int
|
The maximum instance size the generator is allowed to create. |
required |
timeout_generator
|
Timeout in seconds for the generator to finish running. |
required | |
space_generator
|
Memory space in MB the generator has access to. |
required | |
cpus_generator
|
Number of physical cpu cores the generator can use. |
required | |
timeout_solver
|
Timeout in seconds for the solver to finish running. |
required | |
space_solver
|
Memory space in MB the solver has access to. |
required | |
cpus_solver
|
Number of physical cpu cores the solver can use. |
required | |
generator_battle_input
|
Additional data the generator will be provided with. |
required | |
solver_battle_input
|
Additional data the solver will be provided with. |
required | |
generator_battle_output
|
Class used to parse additional data the generator outputs into a python object. |
required | |
solver_battle_output
|
Class used to parse additional data the solver outputs into a python object. |
required | |
with_results
|
bool
|
Whether to return the raw result objects. |
False
|
Returns:
Type | Description |
---|---|
Fight | tuple[Fight, GeneratorResult, SolverResult | None]
|
The resulting info about the executed fight, and the results if the flag has been set. |
Source code in algobattle/battle.py
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
|
algobattle.battle.Fight
¶
Bases: BaseModel
The result of one fight between the participating teams.
For a more detailed description of what each fight looks like, see :meth:FightHandler.run
.
Source code in algobattle/battle.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
|
score: float
instance-attribute
¶
The solving Team's score.
Always a number in [0, 1]. 0 indicates a total failure of the solver, 1 that it succeeded perfectly.
max_size: int
instance-attribute
¶
The maximum size of an instance the generator was allowed to create.
generator: ProgramRunInfo
instance-attribute
¶
Data about the generator's execution.
solver: ProgramRunInfo | None
instance-attribute
¶
Data about the solver's execution.
from_results(max_size, score, generator, solver, *, config)
classmethod
¶
Turns the involved result objects into a jsonable model.
Source code in algobattle/battle.py
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
|