desdeo 2.0.0__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 (126) hide show
  1. desdeo/adm/ADMAfsar.py +551 -0
  2. desdeo/adm/ADMChen.py +414 -0
  3. desdeo/adm/BaseADM.py +119 -0
  4. desdeo/adm/__init__.py +11 -0
  5. desdeo/api/__init__.py +6 -6
  6. desdeo/api/app.py +38 -28
  7. desdeo/api/config.py +65 -44
  8. desdeo/api/config.toml +23 -12
  9. desdeo/api/db.py +10 -8
  10. desdeo/api/db_init.py +12 -6
  11. desdeo/api/models/__init__.py +220 -20
  12. desdeo/api/models/archive.py +16 -27
  13. desdeo/api/models/emo.py +128 -0
  14. desdeo/api/models/enautilus.py +69 -0
  15. desdeo/api/models/gdm/gdm_aggregate.py +139 -0
  16. desdeo/api/models/gdm/gdm_base.py +69 -0
  17. desdeo/api/models/gdm/gdm_score_bands.py +114 -0
  18. desdeo/api/models/gdm/gnimbus.py +138 -0
  19. desdeo/api/models/generic.py +104 -0
  20. desdeo/api/models/generic_states.py +401 -0
  21. desdeo/api/models/nimbus.py +158 -0
  22. desdeo/api/models/preference.py +44 -6
  23. desdeo/api/models/problem.py +274 -64
  24. desdeo/api/models/session.py +4 -1
  25. desdeo/api/models/state.py +419 -52
  26. desdeo/api/models/user.py +7 -6
  27. desdeo/api/models/utopia.py +25 -0
  28. desdeo/api/routers/_EMO.backup +309 -0
  29. desdeo/api/routers/_NIMBUS.py +6 -3
  30. desdeo/api/routers/emo.py +497 -0
  31. desdeo/api/routers/enautilus.py +237 -0
  32. desdeo/api/routers/gdm/gdm_aggregate.py +234 -0
  33. desdeo/api/routers/gdm/gdm_base.py +420 -0
  34. desdeo/api/routers/gdm/gdm_score_bands/gdm_score_bands_manager.py +398 -0
  35. desdeo/api/routers/gdm/gdm_score_bands/gdm_score_bands_routers.py +377 -0
  36. desdeo/api/routers/gdm/gnimbus/gnimbus_manager.py +698 -0
  37. desdeo/api/routers/gdm/gnimbus/gnimbus_routers.py +591 -0
  38. desdeo/api/routers/generic.py +233 -0
  39. desdeo/api/routers/nimbus.py +705 -0
  40. desdeo/api/routers/problem.py +201 -4
  41. desdeo/api/routers/reference_point_method.py +20 -44
  42. desdeo/api/routers/session.py +50 -26
  43. desdeo/api/routers/user_authentication.py +180 -26
  44. desdeo/api/routers/utils.py +187 -0
  45. desdeo/api/routers/utopia.py +230 -0
  46. desdeo/api/schema.py +10 -4
  47. desdeo/api/tests/conftest.py +94 -2
  48. desdeo/api/tests/test_enautilus.py +330 -0
  49. desdeo/api/tests/test_models.py +550 -72
  50. desdeo/api/tests/test_routes.py +902 -43
  51. desdeo/api/utils/_database.py +263 -0
  52. desdeo/api/utils/database.py +28 -266
  53. desdeo/api/utils/emo_database.py +40 -0
  54. desdeo/core.py +7 -0
  55. desdeo/emo/__init__.py +154 -24
  56. desdeo/emo/hooks/archivers.py +18 -2
  57. desdeo/emo/methods/EAs.py +128 -5
  58. desdeo/emo/methods/bases.py +9 -56
  59. desdeo/emo/methods/templates.py +111 -0
  60. desdeo/emo/operators/crossover.py +544 -42
  61. desdeo/emo/operators/evaluator.py +10 -14
  62. desdeo/emo/operators/generator.py +127 -24
  63. desdeo/emo/operators/mutation.py +212 -41
  64. desdeo/emo/operators/scalar_selection.py +202 -0
  65. desdeo/emo/operators/selection.py +956 -214
  66. desdeo/emo/operators/termination.py +124 -16
  67. desdeo/emo/options/__init__.py +108 -0
  68. desdeo/emo/options/algorithms.py +435 -0
  69. desdeo/emo/options/crossover.py +164 -0
  70. desdeo/emo/options/generator.py +131 -0
  71. desdeo/emo/options/mutation.py +260 -0
  72. desdeo/emo/options/repair.py +61 -0
  73. desdeo/emo/options/scalar_selection.py +66 -0
  74. desdeo/emo/options/selection.py +127 -0
  75. desdeo/emo/options/templates.py +383 -0
  76. desdeo/emo/options/termination.py +143 -0
  77. desdeo/gdm/__init__.py +22 -0
  78. desdeo/gdm/gdmtools.py +45 -0
  79. desdeo/gdm/score_bands.py +114 -0
  80. desdeo/gdm/voting_rules.py +50 -0
  81. desdeo/mcdm/__init__.py +23 -1
  82. desdeo/mcdm/enautilus.py +338 -0
  83. desdeo/mcdm/gnimbus.py +484 -0
  84. desdeo/mcdm/nautilus_navigator.py +7 -6
  85. desdeo/mcdm/reference_point_method.py +70 -0
  86. desdeo/problem/__init__.py +5 -1
  87. desdeo/problem/external/__init__.py +18 -0
  88. desdeo/problem/external/core.py +356 -0
  89. desdeo/problem/external/pymoo_provider.py +266 -0
  90. desdeo/problem/external/runtime.py +44 -0
  91. desdeo/problem/infix_parser.py +2 -2
  92. desdeo/problem/pyomo_evaluator.py +25 -6
  93. desdeo/problem/schema.py +69 -48
  94. desdeo/problem/simulator_evaluator.py +65 -15
  95. desdeo/problem/testproblems/__init__.py +26 -11
  96. desdeo/problem/testproblems/benchmarks_server.py +120 -0
  97. desdeo/problem/testproblems/cake_problem.py +185 -0
  98. desdeo/problem/testproblems/dmitry_forest_problem_discrete.py +71 -0
  99. desdeo/problem/testproblems/forest_problem.py +77 -69
  100. desdeo/problem/testproblems/multi_valued_constraints.py +119 -0
  101. desdeo/problem/testproblems/{river_pollution_problem.py → river_pollution_problems.py} +28 -22
  102. desdeo/problem/testproblems/single_objective.py +289 -0
  103. desdeo/problem/testproblems/zdt_problem.py +4 -1
  104. desdeo/tools/__init__.py +39 -21
  105. desdeo/tools/desc_gen.py +22 -0
  106. desdeo/tools/generics.py +22 -2
  107. desdeo/tools/group_scalarization.py +3090 -0
  108. desdeo/tools/indicators_binary.py +107 -1
  109. desdeo/tools/indicators_unary.py +3 -16
  110. desdeo/tools/message.py +33 -2
  111. desdeo/tools/non_dominated_sorting.py +4 -3
  112. desdeo/tools/patterns.py +9 -7
  113. desdeo/tools/pyomo_solver_interfaces.py +48 -35
  114. desdeo/tools/reference_vectors.py +118 -351
  115. desdeo/tools/scalarization.py +340 -1413
  116. desdeo/tools/score_bands.py +491 -328
  117. desdeo/tools/utils.py +117 -49
  118. desdeo/tools/visualizations.py +67 -0
  119. desdeo/utopia_stuff/utopia_problem.py +1 -1
  120. desdeo/utopia_stuff/utopia_problem_old.py +1 -1
  121. {desdeo-2.0.0.dist-info → desdeo-2.1.0.dist-info}/METADATA +46 -28
  122. desdeo-2.1.0.dist-info/RECORD +180 -0
  123. {desdeo-2.0.0.dist-info → desdeo-2.1.0.dist-info}/WHEEL +1 -1
  124. desdeo-2.0.0.dist-info/RECORD +0 -120
  125. /desdeo/api/utils/{logger.py → _logger.py} +0 -0
  126. {desdeo-2.0.0.dist-info → desdeo-2.1.0.dist-info/licenses}/LICENSE +0 -0
@@ -0,0 +1,185 @@
1
+ """Defines the 'best cake problem'."""
2
+
3
+ from desdeo.problem.schema import (
4
+ Constant,
5
+ Objective,
6
+ ObjectiveTypeEnum,
7
+ Problem,
8
+ Variable,
9
+ VariableTypeEnum,
10
+ )
11
+
12
+ PI = 3.14159265358979323846
13
+
14
+
15
+ ## Helper func
16
+ def U(z: float):
17
+ return 4.0 * z * (1.0 - z)
18
+
19
+
20
+ ## Helper funcs to return string representations
21
+
22
+
23
+ def bowl_str(z: str, a: str, invD: str) -> str:
24
+ tmp: str = f"({z} - {a})*{invD}"
25
+ return f"({tmp}*{tmp})"
26
+ # return f"{clamp01_str(f"{tmp}*{tmp}")}"
27
+
28
+
29
+ def U_str(z: str) -> str:
30
+ tmp: str = f"(4*{z}*(1.0 - {z}))"
31
+ return f"({tmp}*{tmp})"
32
+
33
+
34
+ def ripple_str(t: str) -> str:
35
+ tmp: str = f"Sin({PI} * {t})"
36
+ return f"({tmp}*{tmp})"
37
+
38
+
39
+ # Objective function string representations
40
+ def f0_str() -> str:
41
+ yliq: str = "(0.5*x5 + 0.3*x4 + 0.2*x3)"
42
+ v: str = (
43
+ f"(0.4 * {bowl_str('x1', 'T1', 'INV_D1')}) + "
44
+ f"(0.4 * {bowl_str(yliq, 'Y_LIQ_STAR', 'INV_D_YLIQ')}) + "
45
+ f" (0.2 * {ripple_str('((x1 + x6) - (T1 + T6))')})"
46
+ )
47
+ return f"14*({v})"
48
+
49
+
50
+ def f1_str() -> str:
51
+ sbar: str = "((x2 + 0.5*x3)/1.5)"
52
+ w25: str = f"({U_str('x2')}*{U_str('x5')})"
53
+ d25: str = f"(({w25} - W25_STAR)*INV_DW25)"
54
+ v: str = f"(0.4*{bowl_str('x2', 'T2', 'INV_D2')}) + (0.3*{ripple_str(f'{sbar} - SBAR_STAR')}) +(0.3*{d25}*{d25})"
55
+ return f"14*({v})"
56
+
57
+
58
+ def f2_str() -> str:
59
+ v: str = (
60
+ f"(0.35*{bowl_str('x6', 'T6', 'INV_D6')}) + "
61
+ f"(0.25*{bowl_str('x4', 'T4', 'INV_D4')}) + "
62
+ f"(0.4*{ripple_str('((x6 - 0.5*x4) - (T6 - 0.5*T4))')})"
63
+ )
64
+ return f"14*({v})"
65
+
66
+
67
+ def f3_str() -> str:
68
+ w35: str = f"({U_str('x3')}*{U_str('x5')})"
69
+ d35: str = f"(({w35} - W35_STAR) * INV_DW35)"
70
+ v: str = f"(0.3*{bowl_str('x3', 'T3', 'INV_D3')}) + (0.3*{bowl_str('x5', 'T5', 'INV_D5')}) + (0.4*({d35}*{d35}))"
71
+ return f"14*({v})"
72
+
73
+
74
+ def f4_str() -> str:
75
+ v: str = (
76
+ f"(0.25*{bowl_str('x2', 'T2', 'INV_D2')}) + "
77
+ f"(0.25*{bowl_str('x3', 'T3', 'INV_D3')}) + "
78
+ f"(0.20*{ripple_str('(x4 - T4)')}) + "
79
+ f"(0.30*{ripple_str('((x2 - x5) - (T2 - T5))')})"
80
+ )
81
+ return f"14*({v})"
82
+
83
+
84
+ ## The cake problem
85
+ def best_cake_problem() -> Problem:
86
+ """Defines the best cake problem."""
87
+ variable_inits = [
88
+ ("Flour", 0.70),
89
+ ("Sugar", 0.10),
90
+ ("Butter", 0.40),
91
+ ("Eggs", 0.50),
92
+ ("Milk", 0.20),
93
+ ("Baking powder", 0.80),
94
+ ]
95
+ variables = [
96
+ Variable(
97
+ name=var[0],
98
+ symbol=f"x{i + 1}",
99
+ variable_type=VariableTypeEnum.real,
100
+ lowerbound=0.0,
101
+ upperbound=1.0,
102
+ initial_value=var[1],
103
+ )
104
+ for i, var in enumerate(variable_inits)
105
+ ]
106
+
107
+ constants_init = [
108
+ ("T1", 0.60),
109
+ ("T2", 0.35),
110
+ ("T3", 0.25),
111
+ ("T4", 0.30),
112
+ ("T5", 0.35),
113
+ ("T6", 0.40),
114
+ ("INV_D1", 1.0 / 0.60),
115
+ ("INV_D2", 1.0 / 0.65),
116
+ ("INV_D3", 1.0 / 0.75),
117
+ ("INV_D4", 1.0 / 0.70),
118
+ ("INV_D5", 1.0 / 0.65),
119
+ ("INV_D6", 1.0 / 0.60),
120
+ ("Y_LIQ_STAR", 0.5 * 0.35 + 0.3 * 0.30 + 0.2 * 0.25),
121
+ ("INV_D_YLIQ", 1.0 / 0.685),
122
+ ("SBAR_STAR", (0.35 + 0.5 * 0.25) / 1.5),
123
+ ("W25_STAR", U(0.35) * U(0.35)),
124
+ ("INV_DW25", 1.0 / 0.8281),
125
+ ("W35_STAR", U(0.25) * U(0.35)),
126
+ ("INV_DW35", 1.0 / 0.6825),
127
+ ]
128
+
129
+ constants = [Constant(name=const[0], symbol=const[0], value=const[1]) for const in constants_init]
130
+
131
+ objectives = [
132
+ Objective(
133
+ name="Dry/crumb error",
134
+ symbol="dry_crumb",
135
+ func=f0_str(),
136
+ ideal=0.0,
137
+ nadir=14.0,
138
+ objective_type=ObjectiveTypeEnum.analytical,
139
+ is_twice_differentiable=True, # right?
140
+ ),
141
+ Objective(
142
+ name="Sweetness/texture off-target",
143
+ symbol="sweet_texture",
144
+ func=f1_str(),
145
+ ideal=0.0,
146
+ nadir=14.0,
147
+ objective_type=ObjectiveTypeEnum.analytical,
148
+ is_twice_differentiable=True,
149
+ ),
150
+ Objective(
151
+ name="Rise/collapse risk",
152
+ symbol="rise_collapse",
153
+ func=f2_str(),
154
+ ideal=0.0,
155
+ nadir=14.0,
156
+ objective_type=ObjectiveTypeEnum.analytical,
157
+ is_twice_differentiable=True,
158
+ ),
159
+ Objective(
160
+ name="Moistness/grease imbalance",
161
+ symbol="moistness_grease",
162
+ func=f3_str(),
163
+ ideal=0.0,
164
+ nadir=14.0,
165
+ objective_type=ObjectiveTypeEnum.analytical,
166
+ is_twice_differentiable=True,
167
+ ),
168
+ Objective(
169
+ name="Browning/burn risk",
170
+ symbol="browning_burn",
171
+ func=f4_str(),
172
+ ideal=0.0,
173
+ nadir=14.0,
174
+ objective_type=ObjectiveTypeEnum.analytical,
175
+ is_twice_differentiable=True,
176
+ ),
177
+ ]
178
+
179
+ return Problem(
180
+ name="Cake problem",
181
+ description="Try to find the most delicious cake!",
182
+ constants=constants,
183
+ variables=variables,
184
+ objectives=objectives,
185
+ )
@@ -0,0 +1,71 @@
1
+ """A forest problem with discrete representation."""
2
+ from pathlib import Path
3
+
4
+ import polars as pl
5
+
6
+ from desdeo.problem.schema import (
7
+ DiscreteRepresentation,
8
+ Objective,
9
+ ObjectiveTypeEnum,
10
+ Problem,
11
+ Variable,
12
+ VariableTypeEnum,
13
+ )
14
+
15
+
16
+ def dmitry_forest_problem_disc() -> Problem:
17
+ """Implements the dmitry forest problem using Pareto front representation.
18
+
19
+ Returns:
20
+ Problem: A problem instance representing the forest problem.
21
+ """
22
+ path = Path(__file__)
23
+ while not str(path).endswith("/DESDEO"):
24
+ path = path.parent
25
+
26
+ path = path / "tests/data/dmitry_discrete_repr/dmitry_forest_problem_non_dom_solns.csv"
27
+
28
+ obj_names = ["Rev", "HA", "Carb", "DW"]
29
+
30
+ var_name = "index"
31
+
32
+ data = pl.read_csv(
33
+ path, has_header=True, columns=["Rev", "HA", "Carb", "DW"], separator=",", #decimal_comma=True
34
+ )
35
+
36
+ variables = [
37
+ Variable(
38
+ name=var_name,
39
+ symbol=var_name,
40
+ variable_type=VariableTypeEnum.integer,
41
+ lowerbound=0,
42
+ upperbound=len(data) - 1,
43
+ initial_value=0,
44
+ )
45
+ ]
46
+
47
+ objectives = [
48
+ Objective(
49
+ name=obj_name,
50
+ symbol=obj_name,
51
+ objective_type=ObjectiveTypeEnum.data_based,
52
+ ideal=data[obj_name].max(),
53
+ nadir=data[obj_name].min(),
54
+ maximize=True,
55
+ )
56
+ for obj_name in obj_names
57
+ ]
58
+
59
+ discrete_def = DiscreteRepresentation(
60
+ variable_values={"index": list(range(len(data)))},
61
+ objective_values=data[[obj.symbol for obj in objectives]].to_dict(),
62
+ )
63
+
64
+ return Problem(
65
+ name="Dmitry Forest Problem (Discrete)",
66
+ description="Defines a forest problem with four objectives: revenue, habitat availability, carbon storage, and deadwood.",
67
+ variables=variables,
68
+ objectives=objectives,
69
+ discrete_representation=discrete_def,
70
+ is_twice_differentiable=False,
71
+ )
@@ -16,7 +16,13 @@ from desdeo.problem.schema import (
16
16
  VariableTypeEnum,
17
17
  )
18
18
 
19
- def forest_problem(simulation_results: str, treatment_key: str, holding: int = 1, comparing: bool = False) -> Problem:
19
+
20
+ def forest_problem(
21
+ simulation_results: str = "./tests/data/alternatives_290124.csv",
22
+ treatment_key: str = "./tests/data/alternatives_key_290124.csv",
23
+ holding: int = 1,
24
+ comparing: bool = False,
25
+ ) -> Problem:
20
26
  r"""Defines a test forest problem that has TensorConstants and TensorVariables.
21
27
 
22
28
  The problem has TensorConstants V, W and P as vectors taking values from a data file and
@@ -45,8 +51,8 @@ def forest_problem(simulation_results: str, treatment_key: str, holding: int = 1
45
51
  simulation_results (str): Location of the simulation results file.
46
52
  treatment_key (str): Location of the file with the treatment information.
47
53
  holding (int, optional): The number of the holding to be optimized. Defaults to 1.
48
- comparing (bool, optional): Determines if solutions are to be compared to those from the rahti app.
49
- Defaults to False.
54
+ comparing (bool, optional): This is only used for testing the method.
55
+ If comparing == True, the results are nonsense. Defaults to False.
50
56
 
51
57
  Returns:
52
58
  Problem: An instance of the test forest problem.
@@ -54,58 +60,60 @@ def forest_problem(simulation_results: str, treatment_key: str, holding: int = 1
54
60
  df = pl.read_csv(simulation_results, schema_overrides={"unit": pl.Float64})
55
61
  df_key = pl.read_csv(treatment_key, schema_overrides={"unit": pl.Float64})
56
62
 
57
- selected_df_v = df.filter(pl.col("holding") == holding).select(["unit", "schedule", "npv_5_percent"])
58
- unique_units = selected_df_v.unique(["unit"], maintain_order=True).get_column("unit")
59
- selected_df_v.group_by(["unit", "schedule"])
60
- rows_by_key = selected_df_v.rows_by_key(key=["unit", "schedule"])
61
- v_array = np.zeros((selected_df_v["unit"].n_unique(), selected_df_v["schedule"].n_unique()))
62
- for i in range(np.shape(v_array)[0]):
63
- for j in range(np.shape(v_array)[1]):
64
- if (unique_units[i], j) in rows_by_key:
65
- v_array[i][j] = rows_by_key[(unique_units[i], j)][0]
66
-
67
- # determine whether the results are to be compared to those from the rahti app (for testing purposes)
68
- # if compared, the stock values are calculated by substacting the value after 2025 period from
69
- # the value after the 2035 period (in other words, last value - first value)
70
- if comparing:
71
- selected_df_w = df.filter(pl.col("holding") == holding).select(["unit", "schedule", "stock_2025", "stock_2035"])
72
- selected_df_w.group_by(["unit", "schedule"])
73
- rows_by_key = selected_df_w.rows_by_key(key=["unit", "schedule"])
74
- selected_df_key_w = df_key.select(["unit", "schedule", "treatment"])
75
- selected_df_key_w.group_by(["unit", "schedule"])
76
- rows_by_key_df_key = selected_df_key_w.rows_by_key(key=["unit", "schedule"])
77
- w_array = np.zeros((selected_df_w["unit"].n_unique(), selected_df_w["schedule"].n_unique()))
78
- for i in range(np.shape(w_array)[0]):
79
- for j in range(np.shape(w_array)[1]):
80
- if len(rows_by_key_df_key[(unique_units[i], j)]) == 0:
81
- continue
82
- if (unique_units[i], j) in rows_by_key:
83
- w_array[i][j] = rows_by_key[(unique_units[i], j)][0][1] - rows_by_key[(unique_units[i], j)][0][0]
84
- else:
85
- selected_df_w = df.filter(pl.col("holding") == holding).select(["unit", "schedule", "stock_2035"])
86
- selected_df_w.group_by(["unit", "schedule"])
87
- rows_by_key = selected_df_w.rows_by_key(key=["unit", "schedule"])
88
- selected_df_key_w = df_key.select(["unit", "schedule", "treatment"])
89
- selected_df_key_w.group_by(["unit", "schedule"])
90
- rows_by_key_df_key = selected_df_key_w.rows_by_key(key=["unit", "schedule"])
91
- w_array = np.zeros((selected_df_w["unit"].n_unique(), selected_df_w["schedule"].n_unique()))
92
- for i in range(np.shape(w_array)[0]):
93
- for j in range(np.shape(w_array)[1]):
94
- if len(rows_by_key_df_key[(unique_units[i], j)]) == 0:
95
- continue
96
- if (unique_units[i], j) in rows_by_key:
97
- w_array[i][j] = rows_by_key[(unique_units[i], j)][0][0]
98
-
99
- selected_df_p = df.filter(pl.col("holding") == holding).select(
100
- ["unit", "schedule", "harvest_value_period_2025", "harvest_value_period_2030", "harvest_value_period_2035"]
63
+ df_joined = df.join(df_key, on=["holding", "unit", "schedule"], how="left")
64
+
65
+ selected_df = df_joined.filter(pl.col("holding") == holding).select(
66
+ [
67
+ "unit",
68
+ "schedule",
69
+ "npv_5_percent",
70
+ "stock_2025",
71
+ "stock_2035",
72
+ "harvest_value_period_2025",
73
+ "harvest_value_period_2030",
74
+ "harvest_value_period_2035",
75
+ "treatment",
76
+ ]
101
77
  )
102
- selected_df_p.group_by(["unit", "schedule"])
103
- rows_by_key = selected_df_p.rows_by_key(key=["unit", "schedule"])
104
- p_array = np.zeros((selected_df_p["unit"].n_unique(), selected_df_p["schedule"].n_unique()))
105
- for i in range(np.shape(p_array)[0]):
106
- for j in range(np.shape(p_array)[1]):
107
- if (unique_units[i], j) in rows_by_key:
108
- p_array[i][j] = sum(rows_by_key[(unique_units[i], j)][0])
78
+ unique_units = selected_df.unique(["unit"], maintain_order=True).get_column("unit")
79
+ n_units = len(unique_units)
80
+ unique_schedules = selected_df.unique(["schedule"], maintain_order=True).get_column("schedule")
81
+ n_schedules = len(unique_schedules)
82
+
83
+ v_array = np.zeros((n_units, n_schedules))
84
+ w_array = np.zeros((n_units, n_schedules))
85
+ p_array = np.zeros((n_units, n_schedules))
86
+
87
+ # This is not the fastest way to do this, but the code is probably more understandable
88
+ for i in range(n_units):
89
+ for j in range(n_schedules):
90
+ unit = unique_units[i]
91
+ schedule = unique_schedules[j]
92
+ print(f"unit {unit} schedule {schedule}")
93
+ if selected_df.filter((pl.col("unit") == unit) & (pl.col("schedule") == schedule)).height == 0:
94
+ continue
95
+ v_array[i][j] = (
96
+ selected_df.filter((pl.col("unit") == unit) & (pl.col("schedule") == schedule))
97
+ .select("npv_5_percent")
98
+ .item()
99
+ )
100
+ w_array[i][j] = (
101
+ selected_df.filter((pl.col("unit") == unit) & (pl.col("schedule") == schedule))
102
+ .select("stock_2035")
103
+ .item()
104
+ )
105
+ if comparing:
106
+ w_array[i][j] -= (
107
+ selected_df.filter((pl.col("unit") == unit) & (pl.col("schedule") == schedule))
108
+ .select("stock_2025")
109
+ .item()
110
+ )
111
+ # The harvest values are not going to be discounted like this
112
+ p_array[i][j] = sum(
113
+ selected_df.filter((pl.col("unit") == unit) & (pl.col("schedule") == schedule))
114
+ .select(["harvest_value_period_2025", "harvest_value_period_2030", "harvest_value_period_2035"])
115
+ .row(0)
116
+ )
109
117
 
110
118
  constants = []
111
119
  variables = []
@@ -114,25 +122,25 @@ def forest_problem(simulation_results: str, treatment_key: str, holding: int = 1
114
122
  f_2_func = []
115
123
  f_3_func = []
116
124
  # define the constants V, W and P, decision variable X, constraints, and objective function expressions in one loop
117
- for i in range(np.shape(v_array)[0]):
125
+ for i in range(n_units):
118
126
  # Constants V, W and P
119
127
  v = TensorConstant(
120
- name=f"V_{i+1}",
121
- symbol=f"V_{i+1}",
128
+ name=f"V_{i + 1}",
129
+ symbol=f"V_{i + 1}",
122
130
  shape=[np.shape(v_array)[1]], # NOTE: vectors have to be of form [2] instead of [2,1] or [1,2]
123
131
  values=v_array[i].tolist(),
124
132
  )
125
133
  constants.append(v)
126
134
  w = TensorConstant(
127
- name=f"W_{i+1}",
128
- symbol=f"W_{i+1}",
135
+ name=f"W_{i + 1}",
136
+ symbol=f"W_{i + 1}",
129
137
  shape=[np.shape(w_array)[1]], # NOTE: vectors have to be of form [2] instead of [2,1] or [1,2]
130
138
  values=w_array[i].tolist(),
131
139
  )
132
140
  constants.append(w)
133
141
  p = TensorConstant(
134
- name=f"P_{i+1}",
135
- symbol=f"P_{i+1}",
142
+ name=f"P_{i + 1}",
143
+ symbol=f"P_{i + 1}",
136
144
  shape=[np.shape(p_array)[1]], # NOTE: vectors have to be of form [2] instead of [2,1] or [1,2]
137
145
  values=p_array[i].tolist(),
138
146
  )
@@ -140,8 +148,8 @@ def forest_problem(simulation_results: str, treatment_key: str, holding: int = 1
140
148
  # Decision variable X
141
149
  constants.append(p)
142
150
  x = TensorVariable(
143
- name=f"X_{i+1}",
144
- symbol=f"X_{i+1}",
151
+ name=f"X_{i + 1}",
152
+ symbol=f"X_{i + 1}",
145
153
  variable_type=VariableTypeEnum.binary,
146
154
  shape=[np.shape(v_array)[1]], # NOTE: vectors have to be of form [2] instead of [2,1] or [1,2]
147
155
  lowerbounds=np.shape(v_array)[1] * [0],
@@ -152,10 +160,10 @@ def forest_problem(simulation_results: str, treatment_key: str, holding: int = 1
152
160
 
153
161
  # Constraints
154
162
  con = Constraint(
155
- name=f"x_con_{i+1}",
156
- symbol=f"x_con_{i+1}",
163
+ name=f"x_con_{i + 1}",
164
+ symbol=f"x_con_{i + 1}",
157
165
  cons_type=ConstraintTypeEnum.EQ,
158
- func=f"Sum(X_{i+1}) - 1",
166
+ func=f"Sum(X_{i + 1}) - 1",
159
167
  is_linear=True,
160
168
  is_convex=False, # not checked
161
169
  is_twice_differentiable=True,
@@ -163,13 +171,13 @@ def forest_problem(simulation_results: str, treatment_key: str, holding: int = 1
163
171
  constraints.append(con)
164
172
 
165
173
  # Objective function expressions
166
- exprs = f"V_{i+1}@X_{i+1}"
174
+ exprs = f"V_{i + 1}@X_{i + 1}"
167
175
  f_1_func.append(exprs)
168
176
 
169
- exprs = f"W_{i+1}@X_{i+1}"
177
+ exprs = f"W_{i + 1}@X_{i + 1}"
170
178
  f_2_func.append(exprs)
171
179
 
172
- exprs = f"P_{i+1}@X_{i+1}"
180
+ exprs = f"P_{i + 1}@X_{i + 1}"
173
181
  f_3_func.append(exprs)
174
182
 
175
183
  # form the objective function sums
@@ -0,0 +1,119 @@
1
+ """Defines a test problem with a constraint that is multi-valued."""
2
+
3
+ from desdeo.problem import (
4
+ Constant,
5
+ Constraint,
6
+ ConstraintTypeEnum,
7
+ Objective,
8
+ ObjectiveTypeEnum,
9
+ Problem,
10
+ TensorConstant,
11
+ TensorVariable,
12
+ Variable,
13
+ VariableTypeEnum,
14
+ )
15
+
16
+
17
+ def multi_valued_constraint_problem() -> Problem:
18
+ r"""Defines a test problem with a multi-valued constraint.
19
+
20
+ The problem has two objectives, two variables, and two constraints, the other of which, is multi-valued.
21
+ The problem is defined as follows:
22
+ \[
23
+ \begin{aligned}
24
+ \text{Min} \quad
25
+ & f_1(x_1, x_2, y) = x_1^2 + x_2^2 + y^2, \\[4pt]
26
+ \text{Min} \quad
27
+ & f_2(x_1, x_2, y) = (x_1 - 2)^2 + (x_2 - 1)^2 + (y - 1)^2, \\[6pt]
28
+ \text{subject to} \quad
29
+ & g(x_1, x_2, y) = x_1^2 + x_2 + y - 2 \le 0, \\[4pt]
30
+ & G(x_1, x_2) = A
31
+ \begin{bmatrix}
32
+ x_1 \\[2pt]
33
+ x_2
34
+ \end{bmatrix}
35
+ \le 0,
36
+ \quad
37
+ A =
38
+ \begin{bmatrix}
39
+ 1 & -1 \\[2pt]
40
+ -1 & -2
41
+ \end{bmatrix}.
42
+ \end{aligned}
43
+ \]
44
+
45
+
46
+ Returns:
47
+ Problem: the problem model.
48
+ """
49
+ xs = TensorVariable(
50
+ name="x",
51
+ symbol="X",
52
+ variable_type=VariableTypeEnum.real,
53
+ shape=[2, 1],
54
+ lowerbounds=-5.0,
55
+ upperbounds=5.0,
56
+ initial_values=0.1,
57
+ )
58
+
59
+ y = Variable(
60
+ name="y",
61
+ symbol="y",
62
+ variable_type=VariableTypeEnum.real,
63
+ lowerbound=-10.0,
64
+ upperbound=10.0,
65
+ initial_value=0.1,
66
+ )
67
+
68
+ a = TensorConstant(name="A", symbol="A", shape=[2, 2], values=[[1.0, -1.0], [-1.0, -2.0]])
69
+
70
+ one = Constant(name="one", symbol="one", value=1.0)
71
+
72
+ f_1_expr = "X[1, 1]**2 + X[2, 1]**2 + y**2"
73
+ f_2_expr = "(X[1, 1] - 2)**2 + (X[2, 1] - one)**2 + (y - one)**2"
74
+
75
+ g_1_expr = "X[1, 1]**2 + X[2, 1] + y - 2"
76
+ big_g_expr = "A @ X"
77
+
78
+ f_1 = Objective(
79
+ name="f1",
80
+ symbol="f_1",
81
+ func=f_1_expr,
82
+ objective_type=ObjectiveTypeEnum.analytical,
83
+ ideal=0.0,
84
+ nadir=150.0,
85
+ is_twice_differentiable=True,
86
+ )
87
+
88
+ f_2 = Objective(
89
+ name="f2",
90
+ symbol="f_2",
91
+ func=f_2_expr,
92
+ ideal=0.0,
93
+ nadir=206.0,
94
+ objective_type=ObjectiveTypeEnum.analytical,
95
+ is_twice_differentiable=True,
96
+ )
97
+
98
+ g_1 = Constraint(
99
+ name="g1", symbol="g_1", cons_type=ConstraintTypeEnum.LTE, func=g_1_expr, is_twice_differentiable=True
100
+ )
101
+
102
+ big_g = Constraint(
103
+ name="big_g",
104
+ symbol="G",
105
+ cons_type=ConstraintTypeEnum.LTE,
106
+ func=big_g_expr,
107
+ is_twice_differentiable=True,
108
+ is_linear=True,
109
+ is_convex=True,
110
+ )
111
+
112
+ return Problem(
113
+ name="Multi-valued-constraint problem",
114
+ description="Problem for testing problems with multi-valued constraints.",
115
+ constants=[a, one],
116
+ variables=[xs, y],
117
+ constraints=[g_1, big_g],
118
+ objectives=[f_1, f_2],
119
+ )