desdeo 1.2__py3-none-any.whl → 2.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (182) hide show
  1. desdeo/__init__.py +8 -8
  2. desdeo/adm/ADMAfsar.py +551 -0
  3. desdeo/adm/ADMChen.py +414 -0
  4. desdeo/adm/BaseADM.py +119 -0
  5. desdeo/adm/__init__.py +11 -0
  6. desdeo/api/README.md +73 -0
  7. desdeo/api/__init__.py +15 -0
  8. desdeo/api/app.py +50 -0
  9. desdeo/api/config.py +90 -0
  10. desdeo/api/config.toml +64 -0
  11. desdeo/api/db.py +27 -0
  12. desdeo/api/db_init.py +85 -0
  13. desdeo/api/db_models.py +164 -0
  14. desdeo/api/malaga_db_init.py +27 -0
  15. desdeo/api/models/__init__.py +266 -0
  16. desdeo/api/models/archive.py +23 -0
  17. desdeo/api/models/emo.py +128 -0
  18. desdeo/api/models/enautilus.py +69 -0
  19. desdeo/api/models/gdm/gdm_aggregate.py +139 -0
  20. desdeo/api/models/gdm/gdm_base.py +69 -0
  21. desdeo/api/models/gdm/gdm_score_bands.py +114 -0
  22. desdeo/api/models/gdm/gnimbus.py +138 -0
  23. desdeo/api/models/generic.py +104 -0
  24. desdeo/api/models/generic_states.py +401 -0
  25. desdeo/api/models/nimbus.py +158 -0
  26. desdeo/api/models/preference.py +128 -0
  27. desdeo/api/models/problem.py +717 -0
  28. desdeo/api/models/reference_point_method.py +18 -0
  29. desdeo/api/models/session.py +49 -0
  30. desdeo/api/models/state.py +463 -0
  31. desdeo/api/models/user.py +52 -0
  32. desdeo/api/models/utopia.py +25 -0
  33. desdeo/api/routers/_EMO.backup +309 -0
  34. desdeo/api/routers/_NAUTILUS.py +245 -0
  35. desdeo/api/routers/_NAUTILUS_navigator.py +233 -0
  36. desdeo/api/routers/_NIMBUS.py +765 -0
  37. desdeo/api/routers/__init__.py +5 -0
  38. desdeo/api/routers/emo.py +497 -0
  39. desdeo/api/routers/enautilus.py +237 -0
  40. desdeo/api/routers/gdm/gdm_aggregate.py +234 -0
  41. desdeo/api/routers/gdm/gdm_base.py +420 -0
  42. desdeo/api/routers/gdm/gdm_score_bands/gdm_score_bands_manager.py +398 -0
  43. desdeo/api/routers/gdm/gdm_score_bands/gdm_score_bands_routers.py +377 -0
  44. desdeo/api/routers/gdm/gnimbus/gnimbus_manager.py +698 -0
  45. desdeo/api/routers/gdm/gnimbus/gnimbus_routers.py +591 -0
  46. desdeo/api/routers/generic.py +233 -0
  47. desdeo/api/routers/nimbus.py +705 -0
  48. desdeo/api/routers/problem.py +307 -0
  49. desdeo/api/routers/reference_point_method.py +93 -0
  50. desdeo/api/routers/session.py +100 -0
  51. desdeo/api/routers/test.py +16 -0
  52. desdeo/api/routers/user_authentication.py +520 -0
  53. desdeo/api/routers/utils.py +187 -0
  54. desdeo/api/routers/utopia.py +230 -0
  55. desdeo/api/schema.py +100 -0
  56. desdeo/api/tests/__init__.py +0 -0
  57. desdeo/api/tests/conftest.py +151 -0
  58. desdeo/api/tests/test_enautilus.py +330 -0
  59. desdeo/api/tests/test_models.py +1179 -0
  60. desdeo/api/tests/test_routes.py +1075 -0
  61. desdeo/api/utils/_database.py +263 -0
  62. desdeo/api/utils/_logger.py +29 -0
  63. desdeo/api/utils/database.py +36 -0
  64. desdeo/api/utils/emo_database.py +40 -0
  65. desdeo/core.py +34 -0
  66. desdeo/emo/__init__.py +159 -0
  67. desdeo/emo/hooks/archivers.py +188 -0
  68. desdeo/emo/methods/EAs.py +541 -0
  69. desdeo/emo/methods/__init__.py +0 -0
  70. desdeo/emo/methods/bases.py +12 -0
  71. desdeo/emo/methods/templates.py +111 -0
  72. desdeo/emo/operators/__init__.py +1 -0
  73. desdeo/emo/operators/crossover.py +1282 -0
  74. desdeo/emo/operators/evaluator.py +114 -0
  75. desdeo/emo/operators/generator.py +459 -0
  76. desdeo/emo/operators/mutation.py +1224 -0
  77. desdeo/emo/operators/scalar_selection.py +202 -0
  78. desdeo/emo/operators/selection.py +1778 -0
  79. desdeo/emo/operators/termination.py +286 -0
  80. desdeo/emo/options/__init__.py +108 -0
  81. desdeo/emo/options/algorithms.py +435 -0
  82. desdeo/emo/options/crossover.py +164 -0
  83. desdeo/emo/options/generator.py +131 -0
  84. desdeo/emo/options/mutation.py +260 -0
  85. desdeo/emo/options/repair.py +61 -0
  86. desdeo/emo/options/scalar_selection.py +66 -0
  87. desdeo/emo/options/selection.py +127 -0
  88. desdeo/emo/options/templates.py +383 -0
  89. desdeo/emo/options/termination.py +143 -0
  90. desdeo/explanations/__init__.py +6 -0
  91. desdeo/explanations/explainer.py +100 -0
  92. desdeo/explanations/utils.py +90 -0
  93. desdeo/gdm/__init__.py +22 -0
  94. desdeo/gdm/gdmtools.py +45 -0
  95. desdeo/gdm/score_bands.py +114 -0
  96. desdeo/gdm/voting_rules.py +50 -0
  97. desdeo/mcdm/__init__.py +41 -0
  98. desdeo/mcdm/enautilus.py +338 -0
  99. desdeo/mcdm/gnimbus.py +484 -0
  100. desdeo/mcdm/nautili.py +345 -0
  101. desdeo/mcdm/nautilus.py +477 -0
  102. desdeo/mcdm/nautilus_navigator.py +656 -0
  103. desdeo/mcdm/nimbus.py +417 -0
  104. desdeo/mcdm/pareto_navigator.py +269 -0
  105. desdeo/mcdm/reference_point_method.py +186 -0
  106. desdeo/problem/__init__.py +83 -0
  107. desdeo/problem/evaluator.py +561 -0
  108. desdeo/problem/external/__init__.py +18 -0
  109. desdeo/problem/external/core.py +356 -0
  110. desdeo/problem/external/pymoo_provider.py +266 -0
  111. desdeo/problem/external/runtime.py +44 -0
  112. desdeo/problem/gurobipy_evaluator.py +562 -0
  113. desdeo/problem/infix_parser.py +341 -0
  114. desdeo/problem/json_parser.py +944 -0
  115. desdeo/problem/pyomo_evaluator.py +487 -0
  116. desdeo/problem/schema.py +1829 -0
  117. desdeo/problem/simulator_evaluator.py +348 -0
  118. desdeo/problem/sympy_evaluator.py +244 -0
  119. desdeo/problem/testproblems/__init__.py +88 -0
  120. desdeo/problem/testproblems/benchmarks_server.py +120 -0
  121. desdeo/problem/testproblems/binh_and_korn_problem.py +88 -0
  122. desdeo/problem/testproblems/cake_problem.py +185 -0
  123. desdeo/problem/testproblems/dmitry_forest_problem_discrete.py +71 -0
  124. desdeo/problem/testproblems/dtlz2_problem.py +102 -0
  125. desdeo/problem/testproblems/forest_problem.py +283 -0
  126. desdeo/problem/testproblems/knapsack_problem.py +163 -0
  127. desdeo/problem/testproblems/mcwb_problem.py +831 -0
  128. desdeo/problem/testproblems/mixed_variable_dimenrions_problem.py +83 -0
  129. desdeo/problem/testproblems/momip_problem.py +172 -0
  130. desdeo/problem/testproblems/multi_valued_constraints.py +119 -0
  131. desdeo/problem/testproblems/nimbus_problem.py +143 -0
  132. desdeo/problem/testproblems/pareto_navigator_problem.py +89 -0
  133. desdeo/problem/testproblems/re_problem.py +492 -0
  134. desdeo/problem/testproblems/river_pollution_problems.py +440 -0
  135. desdeo/problem/testproblems/rocket_injector_design_problem.py +140 -0
  136. desdeo/problem/testproblems/simple_problem.py +351 -0
  137. desdeo/problem/testproblems/simulator_problem.py +92 -0
  138. desdeo/problem/testproblems/single_objective.py +289 -0
  139. desdeo/problem/testproblems/spanish_sustainability_problem.py +945 -0
  140. desdeo/problem/testproblems/zdt_problem.py +274 -0
  141. desdeo/problem/utils.py +245 -0
  142. desdeo/tools/GenerateReferencePoints.py +181 -0
  143. desdeo/tools/__init__.py +120 -0
  144. desdeo/tools/desc_gen.py +22 -0
  145. desdeo/tools/generics.py +165 -0
  146. desdeo/tools/group_scalarization.py +3090 -0
  147. desdeo/tools/gurobipy_solver_interfaces.py +258 -0
  148. desdeo/tools/indicators_binary.py +117 -0
  149. desdeo/tools/indicators_unary.py +362 -0
  150. desdeo/tools/interaction_schema.py +38 -0
  151. desdeo/tools/intersection.py +54 -0
  152. desdeo/tools/iterative_pareto_representer.py +99 -0
  153. desdeo/tools/message.py +265 -0
  154. desdeo/tools/ng_solver_interfaces.py +199 -0
  155. desdeo/tools/non_dominated_sorting.py +134 -0
  156. desdeo/tools/patterns.py +283 -0
  157. desdeo/tools/proximal_solver.py +99 -0
  158. desdeo/tools/pyomo_solver_interfaces.py +477 -0
  159. desdeo/tools/reference_vectors.py +229 -0
  160. desdeo/tools/scalarization.py +2065 -0
  161. desdeo/tools/scipy_solver_interfaces.py +454 -0
  162. desdeo/tools/score_bands.py +627 -0
  163. desdeo/tools/utils.py +388 -0
  164. desdeo/tools/visualizations.py +67 -0
  165. desdeo/utopia_stuff/__init__.py +0 -0
  166. desdeo/utopia_stuff/data/1.json +15 -0
  167. desdeo/utopia_stuff/data/2.json +13 -0
  168. desdeo/utopia_stuff/data/3.json +15 -0
  169. desdeo/utopia_stuff/data/4.json +17 -0
  170. desdeo/utopia_stuff/data/5.json +15 -0
  171. desdeo/utopia_stuff/from_json.py +40 -0
  172. desdeo/utopia_stuff/reinit_user.py +38 -0
  173. desdeo/utopia_stuff/utopia_db_init.py +212 -0
  174. desdeo/utopia_stuff/utopia_problem.py +403 -0
  175. desdeo/utopia_stuff/utopia_problem_old.py +415 -0
  176. desdeo/utopia_stuff/utopia_reference_solutions.py +79 -0
  177. desdeo-2.1.0.dist-info/METADATA +186 -0
  178. desdeo-2.1.0.dist-info/RECORD +180 -0
  179. {desdeo-1.2.dist-info → desdeo-2.1.0.dist-info}/WHEEL +1 -1
  180. desdeo-2.1.0.dist-info/licenses/LICENSE +21 -0
  181. desdeo-1.2.dist-info/METADATA +0 -16
  182. desdeo-1.2.dist-info/RECORD +0 -4
@@ -0,0 +1,233 @@
1
+ """Endpoints for NAUTILUS Navigator."""
2
+
3
+ from typing import Annotated
4
+
5
+ from fastapi import APIRouter, Depends, HTTPException
6
+ from pydantic import BaseModel, Field, ValidationError
7
+ from sqlalchemy.orm import Session
8
+
9
+ from desdeo.api.db import get_db
10
+ from desdeo.api.db_models import Problem as ProblemInDB
11
+ from desdeo.api.db_models import Results
12
+ from desdeo.api.routers.user_authentication import get_current_user
13
+ from desdeo.api.schema import User
14
+ from desdeo.mcdm.nautilus_navigator import (
15
+ NAUTILUS_Response,
16
+ get_current_path,
17
+ navigator_all_steps,
18
+ navigator_init,
19
+ step_back_index,
20
+ )
21
+ from desdeo.problem.schema import Problem
22
+
23
+ router = APIRouter(prefix="/nautnavi")
24
+
25
+
26
+ class InitRequest(BaseModel):
27
+ """The request to initialize the NAUTILUS Navigator."""
28
+
29
+ problem_id: int = Field(description="The ID of the problem to navigate.")
30
+ """The ID of the problem to navigate."""
31
+ total_steps: int = Field(
32
+ description="The total number of steps in the NAUTILUS Navigator. The default value is 100.", default=100
33
+ )
34
+ "The total number of steps in the NAUTILUS Navigator. The default value is 100."
35
+
36
+
37
+ class NavigateRequest(BaseModel):
38
+ """The request to navigate the NAUTILUS Navigator."""
39
+
40
+ problem_id: int = Field(description="The ID of the problem to navigate.")
41
+ preference: dict[str, float] = Field(description="The preference of the DM.")
42
+ bounds: dict[str, float] = Field(description="The bounds preference of the DM.")
43
+ go_back_step: int = Field(description="The step index to go back.")
44
+ steps_remaining: int = Field(description="The number of steps remaining. Should be total_steps - go_back_step.")
45
+
46
+
47
+ class InitialResponse(BaseModel):
48
+ """The response from the initial endpoint of NAUTILUS Navigator."""
49
+
50
+ objective_symbols: list[str] = Field(description="The symbols of the objectives.")
51
+ objective_long_names: list[str] = Field(description="Long/descriptive names of the objectives.")
52
+ units: list[str] | None = Field(description="The units of the objectives.")
53
+ is_maximized: list[bool] = Field(description="Whether the objectives are to be maximized or minimized.")
54
+ ideal: list[float] = Field(description="The ideal values of the objectives.")
55
+ nadir: list[float] = Field(description="The nadir values of the objectives.")
56
+ total_steps: int = Field(description="The total number of steps in the NAUTILUS Navigator.")
57
+
58
+
59
+ class Response(BaseModel):
60
+ """The response from most NAUTILUS Navigator endpoints.
61
+
62
+ Contains information about the full navigation process.
63
+ """
64
+
65
+ objective_symbols: list[str] = Field(description="The symbols of the objectives.")
66
+ objective_long_names: list[str] = Field(description="Long/descriptive names of the objectives.")
67
+ units: list[str] | None = Field(description="The units of the objectives.")
68
+ is_maximized: list[bool] = Field(description="Whether the objectives are to be maximized or minimized.")
69
+ ideal: list[float] = Field(description="The ideal values of the objectives.")
70
+ nadir: list[float] = Field(description="The nadir values of the objectives.")
71
+ lower_bounds: dict[str, list[float]] = Field(description="The lower bounds of the reachable region.")
72
+ upper_bounds: dict[str, list[float]] = Field(description="The upper bounds of the reachable region.")
73
+ preferences: dict[str, list[float]] = Field(description="The preferences used in each step.")
74
+ bounds: dict[str, list[float]] = Field(description="The bounds preference of the DM.")
75
+ total_steps: int = Field(description="The total number of steps in the NAUTILUS Navigator.")
76
+ reachable_solution: dict = Field(description="The solution reached at the end of navigation.")
77
+
78
+
79
+ @router.post("/initialize")
80
+ def init_navigator(
81
+ init_request: InitRequest,
82
+ user: Annotated[User, Depends(get_current_user)],
83
+ db: Annotated[Session, Depends(get_db)],
84
+ ) -> InitialResponse:
85
+ """Initialize the NAUTILUS Navigator.
86
+
87
+ Args:
88
+ init_request (InitRequest): The request to initialize the NAUTILUS Navigator.
89
+ user (Annotated[User, Depends(get_current_user)]): The current user.
90
+ db (Annotated[Session, Depends(get_db)]): The database session.
91
+
92
+ Returns:
93
+ InitialResponse: The initial response from the NAUTILUS Navigator.
94
+ """
95
+ problem_id = init_request.problem_id
96
+ problem = db.query(ProblemInDB).filter(ProblemInDB.id == problem_id).first()
97
+
98
+ if problem is None:
99
+ raise HTTPException(status_code=404, detail="Problem not found.")
100
+ if problem.owner != user.index and problem.owner is not None:
101
+ raise HTTPException(status_code=403, detail="Unauthorized to access chosen problem.")
102
+ if problem.value is None:
103
+ raise HTTPException(status_code=500, detail="Problem not found.")
104
+ try:
105
+ problem = Problem.model_validate(problem.value) # Ignore the mypy error here for now.
106
+ except ValidationError:
107
+ raise HTTPException(status_code=500, detail="Error in parsing the problem.") from ValidationError
108
+
109
+ response = navigator_init(problem)
110
+
111
+ # Get and delete all Results from previous runs of NAUTILUS Navigator
112
+ results = db.query(Results).filter(Results.problem == problem_id).filter(Results.user == user.index).all()
113
+ for result in results:
114
+ db.delete(result)
115
+ db.commit()
116
+
117
+ new_result = Results(
118
+ user=user.index,
119
+ problem=problem_id,
120
+ value=response.model_dump(mode="json"),
121
+ )
122
+ db.add(new_result)
123
+ db.commit()
124
+
125
+ return InitialResponse(
126
+ objective_symbols=[obj.symbol for obj in problem.objectives],
127
+ objective_long_names=[obj.name for obj in problem.objectives],
128
+ units=[obj.unit or "" for obj in problem.objectives], # For unitless objectives, return an empty string
129
+ is_maximized=[obj.maximize for obj in problem.objectives],
130
+ ideal=[obj.ideal for obj in problem.objectives],
131
+ nadir=[obj.nadir for obj in problem.objectives],
132
+ total_steps=init_request.total_steps,
133
+ )
134
+
135
+
136
+ @router.post("/navigate")
137
+ def navigate(
138
+ request: NavigateRequest,
139
+ user: Annotated[User, Depends(get_current_user)],
140
+ db: Annotated[Session, Depends(get_db)],
141
+ ) -> Response:
142
+ """Navigate the NAUTILUS Navigator.
143
+
144
+ Runs the entire navigation process.
145
+
146
+ Args:
147
+ request (NavigateRequest): The request to navigate the NAUTILUS Navigator.
148
+
149
+ Raises:
150
+ HTTPException: _description_
151
+ HTTPException: _description_
152
+ HTTPException: _description_
153
+ HTTPException: _description_
154
+
155
+ Returns:
156
+ Response: _description_
157
+ """
158
+ problem_id, preference, go_back_step, steps_remaining, bounds = (
159
+ request.problem_id,
160
+ request.preference,
161
+ request.go_back_step,
162
+ request.steps_remaining,
163
+ request.bounds,
164
+ )
165
+ problem = db.query(ProblemInDB).filter(ProblemInDB.id == problem_id).first()
166
+ if problem is None:
167
+ raise HTTPException(status_code=404, detail="Problem not found.")
168
+ if problem.owner != user.index and problem.owner is not None:
169
+ raise HTTPException(status_code=403, detail="Unauthorized to access chosen problem.")
170
+ try:
171
+ problem = Problem.model_validate(problem.value) # Ignore the mypy error here for now.
172
+ except ValidationError:
173
+ raise HTTPException(status_code=500, detail="Error in parsing the problem.") from ValidationError
174
+
175
+ results = db.query(Results).filter(Results.problem == problem_id).filter(Results.user == user.index).all()
176
+ if not results:
177
+ raise HTTPException(status_code=404, detail="NAUTILUS Navigator not initialized.")
178
+
179
+ responses = [NAUTILUS_Response.model_validate(result.value) for result in results]
180
+
181
+ responses.append(responses[step_back_index(responses, go_back_step)])
182
+
183
+ try:
184
+ new_responses = navigator_all_steps(
185
+ problem,
186
+ steps_remaining=steps_remaining,
187
+ reference_point=preference,
188
+ previous_responses=responses,
189
+ bounds=bounds,
190
+ )
191
+ except IndexError as e:
192
+ raise HTTPException(status_code=400, detail="Possible reason for error: bounds are too restrictive.") from e
193
+
194
+ for response in new_responses:
195
+ new_result = Results(
196
+ user=user.index,
197
+ problem=problem_id,
198
+ value=response.model_dump(mode="json"),
199
+ )
200
+ db.add(new_result)
201
+ db.commit()
202
+
203
+ responses = [*responses, *new_responses]
204
+ current_path = get_current_path(responses)
205
+ active_responses = [responses[i] for i in current_path]
206
+ lower_bounds = {}
207
+ upper_bounds = {}
208
+ preferences = {}
209
+ bounds = {}
210
+ for obj in problem.objectives:
211
+ lower_bounds[obj.symbol] = [
212
+ response.reachable_bounds["lower_bounds"][obj.symbol] for response in active_responses
213
+ ]
214
+ upper_bounds[obj.symbol] = [
215
+ response.reachable_bounds["upper_bounds"][obj.symbol] for response in active_responses
216
+ ]
217
+ preferences[obj.symbol] = [response.reference_point[obj.symbol] for response in active_responses[1:]]
218
+ bounds[obj.symbol] = [response.bounds[obj.symbol] for response in active_responses[1:]]
219
+
220
+ return Response(
221
+ objective_symbols=[obj.symbol for obj in problem.objectives],
222
+ objective_long_names=[obj.name for obj in problem.objectives],
223
+ units=[obj.unit or "" for obj in problem.objectives],
224
+ is_maximized=[obj.maximize for obj in problem.objectives],
225
+ ideal=[obj.ideal for obj in problem.objectives],
226
+ nadir=[obj.nadir for obj in problem.objectives],
227
+ lower_bounds=lower_bounds,
228
+ upper_bounds=upper_bounds,
229
+ bounds=bounds,
230
+ preferences=preferences,
231
+ total_steps=len(active_responses) - 1,
232
+ reachable_solution=active_responses[-1].reachable_solution,
233
+ )