Problem¶
This module contains the Problem
, Instance
, Solution
, and other related classes and thus the interface you can use
to provide your own problems.
algobattle.problem.Problem
¶
The definition of a problem.
Source code in algobattle/problem.py
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 326 327 328 329 330 331 332 333 334 335 336 337 338 339 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 |
|
__init__(*, name, instance_cls, solution_cls, min_size=0, with_solution=True, score_function=default_score, test_instance=None)
¶
__init__(*, name: str, instance_cls: type[InstanceT], solution_cls: type[SolutionT], min_size: int = 0, with_solution: Literal[True] = True, score_function: ScoreFunctionWithSol[InstanceT, SolutionT] = default_score, test_instance: InstanceT | None = None) -> None
__init__(*, name: str, instance_cls: type[InstanceT], solution_cls: type[SolutionT], min_size: int = 0, with_solution: Literal[False], score_function: ScoreFunctionNoSol[InstanceT, SolutionT] = default_score, test_instance: InstanceT | None = None) -> None
The definition of a problem.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the problem. |
required |
instance_cls
|
type[InstanceT]
|
Class defining what instances of this problem look like. |
required |
solution_cls
|
type[SolutionT]
|
Class definitng what solutions of this problem look like. |
required |
min_size
|
int
|
Minimum size of valid instances of this problem. |
0
|
with_solution
|
bool
|
Whether the generator should also create a solution. |
True
|
score_function
|
ScoreFunction[InstanceT, SolutionT]
|
Function used to score how well a solution solves a problem instance. The default scoring function returns the quotient of the solver's to the generator's solution score. The score function always takes the instance as the first argument. If |
default_score
|
test_instance
|
InstanceT | None
|
A dummy instance that can be used to test whether a solver produces correct output. |
None
|
Source code in algobattle/problem.py
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 |
|
score(instance, *, solution=None, generator_solution=None, solver_solution=None)
¶
score(instance: InstanceT, *, solution: Solution[InstanceT]) -> float
score(instance: InstanceT, *, generator_solution: Solution[InstanceT], solver_solution: Solution[InstanceT]) -> float
Helper function to call self.score_function with easier to use overloads.
Source code in algobattle/problem.py
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 |
|
load_file(name, file)
classmethod
¶
Loads the problem from the specified file.
Source code in algobattle/problem.py
343 344 345 346 347 348 349 350 351 352 353 354 355 |
|
load(name, file=None)
classmethod
¶
Loads the problem with the given name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the Problem to use. |
required |
file
|
Path | None
|
Path to a file containing this problem. |
None
|
Raises:
Type | Description |
---|---|
ValueError
|
If the problem is not specified properly |
RuntimeError
|
If the problem's dynamic import fails |
Source code in algobattle/problem.py
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 |
|
available()
classmethod
¶
Returns the names of all available Problems.
Source code in algobattle/problem.py
389 390 391 392 |
|
algobattle.problem.Instance
¶
Bases: Encodable
, ABC
Instance base class.
Source code in algobattle/problem.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
|
size: int
abstractmethod
property
¶
The instance's size.
validate_instance()
¶
Confirms that the parsed instance is valid.
Should be idempotent, but may also perform additional postprocessing such as bringing the instance into a normal form.
Raises:
Type | Description |
---|---|
ValidationError
|
if the created instance is invalid. |
Source code in algobattle/problem.py
59 60 61 62 63 64 65 66 67 68 |
|
algobattle.problem.Solution
¶
Bases: EncodableBase
, Generic[InstanceT]
, ABC
A proposed solution for an instance of this problem.
Source code in algobattle/problem.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
|
validate_solution(instance, role)
¶
Confirms that the parsed solution is valid.
Should be idempotent, but may also perform additional postprocessing such as bringing the solution into a normal form.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
instance
|
InstanceT
|
The problem instance this solution is purported to solve. |
required |
role
|
Role
|
The role of the team that generated this solution. |
required |
Raises:
Type | Description |
---|---|
ValidationError
|
if the created instance is invalid. |
Source code in algobattle/problem.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
|
score(instance, role)
¶
Calculate the score of this solution for the given problem instance.
The default implementation always returns 1, indicating that all solutions of this problem are equally good.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
instance
|
InstanceT
|
The instance this solution solves |
required |
role
|
Role
|
The role of the team that generated this solution |
required |
Returns: The calculates score of this solution. Must be a nonnegative number. Bigger scores are considered better, if your score rates better scores lower you can use the @minimize decorator.
Source code in algobattle/problem.py
98 99 100 101 102 103 104 105 106 107 108 109 110 |
|
algobattle.problem.InstanceModel
¶
Bases: InstanceSolutionModel
, EncodableModel
, Instance
, ABC
An instance that can easily be parsed to/from a json file.
Source code in algobattle/problem.py
555 556 557 558 |
|
algobattle.problem.SolutionModel
¶
Bases: InstanceSolutionModel
, Solution[InstanceT]
, ABC
A solution that can easily be parsed to/from a json file.
Source code in algobattle/problem.py
561 562 563 564 565 566 567 568 |
|
decode(source, max_size, role, instance)
classmethod
¶
Uses pydantic to create a python object from a .json
file.
Source code in algobattle/problem.py
564 565 566 567 568 |
|