File size: 4,594 Bytes
ab849c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
111
112
113
114
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
154
155
156
157
158
159
160
"""Data models for the Optimization Operating System."""

from __future__ import annotations

from dataclasses import asdict, dataclass, field
from typing import Any


@dataclass
class OptimizationRequest:
    problem_type: str
    data: dict[str, Any] = field(default_factory=dict)
    constraints: dict[str, Any] = field(default_factory=dict)
    objectives: dict[str, Any] = field(default_factory=dict)
    solver: dict[str, Any] = field(default_factory=dict)
    time_limit: float = 60.0

    def to_dict(self) -> dict[str, Any]:
        return asdict(self)


@dataclass
class InstanceFeatures:
    n_variables: int = 0
    n_constraints: int = 0
    density: float = 0.0
    pct_integer: float = 0.0
    constraint_tightness: float = 0.0

    def to_dict(self) -> dict[str, float | int]:
        return asdict(self)


@dataclass
class ProblemInstance:
    problem_type: str
    instance_id: str
    label: str
    size: str
    seed: int
    data: dict[str, Any]
    features: InstanceFeatures
    constraints: dict[str, Any] = field(default_factory=dict)
    objectives: dict[str, Any] = field(default_factory=dict)
    known_optimum: float | None = None

    def to_dict(self) -> dict[str, Any]:
        return {
            "problem_type": self.problem_type,
            "instance_id": self.instance_id,
            "label": self.label,
            "size": self.size,
            "seed": self.seed,
            "data": self.data,
            "features": self.features.to_dict(),
            "constraints": self.constraints,
            "objectives": self.objectives,
            "known_optimum": self.known_optimum,
        }


@dataclass
class SolveMetrics:
    objective_value: float = 0.0
    best_bound: float = 0.0
    optimality_gap: float = 0.0
    elapsed_time_sec: float = 0.0
    iterations: int = 0
    constraint_violations: int = 0
    feasible: bool = False
    status: str = "unknown"
    time_to_first_feasible: float = 0.0
    memory_mb: float = 0.0

    def to_dict(self) -> dict[str, Any]:
        return asdict(self)


@dataclass
class SolveResult:
    method_id: str
    method_label: str
    method_category: str
    solver_id: str
    solver_config: dict[str, Any]
    instance_id: str
    problem_type: str
    metrics: SolveMetrics
    solution: dict[str, Any] = field(default_factory=dict)
    log: str = ""

    def to_dict(self) -> dict[str, Any]:
        return {
            "method_id": self.method_id,
            "method_label": self.method_label,
            "method_category": self.method_category,
            "solver_id": self.solver_id,
            "solver_config": self.solver_config,
            "instance_id": self.instance_id,
            "problem_type": self.problem_type,
            "metrics": self.metrics.to_dict(),
            "solution": self.solution,
            "log": self.log,
        }


@dataclass
class ExperimentRun:
    run_id: str
    instance: ProblemInstance
    model_version: str
    solver_id: str
    solver_config: dict[str, Any]
    parameters: dict[str, Any]
    results: list[SolveResult]
    winner: str
    winner_gap_pct: float
    runtime_sec: float

    def to_dict(self) -> dict[str, Any]:
        return {
            "run_id": self.run_id,
            "instance": self.instance.to_dict(),
            "model_version": self.model_version,
            "solver_id": self.solver_id,
            "solver_config": self.solver_config,
            "parameters": self.parameters,
            "results": [r.to_dict() for r in self.results],
            "winner": self.winner,
            "winner_gap_pct": self.winner_gap_pct,
            "runtime_sec": self.runtime_sec,
        }


@dataclass
class ScenarioResult:
    scenario_type: str
    scenario_label: str
    perturbation: dict[str, Any]
    baseline_objective: float
    perturbed_objective: float
    delta_pct: float
    feasible: bool
    binding_constraints: list[str] = field(default_factory=list)

    def to_dict(self) -> dict[str, Any]:
        return asdict(self)


@dataclass
class ExplanationReport:
    binding_constraints: list[dict[str, Any]] = field(default_factory=list)
    shadow_prices: list[dict[str, Any]] = field(default_factory=list)
    infeasibility_reason: str = ""
    what_if_suggestions: list[str] = field(default_factory=list)
    counterfactuals: list[dict[str, Any]] = field(default_factory=list)

    def to_dict(self) -> dict[str, Any]:
        return asdict(self)