Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.10"
python-version: "3.12"

- name: Install Poetry
run: |
Expand Down
10 changes: 5 additions & 5 deletions justice/util/EMA_model_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ def model_wrapper_emodps(**kwargs):
weights = np.zeros(weights_shape)

for i in range(centers_shape[0]):
centers[i] = kwargs.pop(f"center {i}")
radii[i] = kwargs.pop(f"radii {i}")
centers[i] = kwargs.pop(f"center_{i}")
radii[i] = kwargs.pop(f"radii_{i}")

for i in range(weights_shape[0]):
weights[i] = kwargs.pop(f"weights {i}")
weights[i] = kwargs.pop(f"weights_{i}")

# Populating the decision variables
centers_flat = centers.flatten()
Expand Down Expand Up @@ -183,10 +183,10 @@ def _compute_inverse_range(min_value: float, max_value: float) -> float:


def _extract_vector(kwargs_dict, prefix, size, macro_idx):
"""Pull a flat vector of length `size` from kwargs named `{prefix} {macro_idx} {i}`."""
"""Pull a flat vector of length `size` from kwargs named `{prefix}_{macro_idx}_{i}`."""
vector = np.empty(size, dtype=float)
for i in range(size):
vector[i] = kwargs_dict.pop(f"{prefix} {macro_idx} {i}")
vector[i] = kwargs_dict.pop(f"{prefix}_{macro_idx}_{i}")
return vector


Expand Down
6 changes: 3 additions & 3 deletions justice/util/pareto_nash_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ def build_policy_bank_from_5row_csv(
row = df.iloc[a]
for i in range(n_agents):
centers = np.array(
[row[f"center {i} {j}"] for j in range(centers_len)], dtype=np.float64
[row[f"center_{i}_{j}"] for j in range(centers_len)], dtype=np.float64
)
radii = np.array(
[row[f"radii {i} {j}"] for j in range(radii_len)], dtype=np.float64
[row[f"radii_{i}_{j}"] for j in range(radii_len)], dtype=np.float64
)
weights = np.array(
[row[f"weights {i} {j}"] for j in range(weights_len)], dtype=np.float64
[row[f"weights_{i}_{j}"] for j in range(weights_len)], dtype=np.float64
)
policy_bank[i, a, :] = np.concatenate([centers, radii, weights])

Expand Down
843 changes: 272 additions & 571 deletions poetry.lock

Large diffs are not rendered by default.

15 changes: 10 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ description = "JUSTICE is an open-source Integrated Assessment Modeling Framewor
authors = [{ name = "PBiswas", email = "p.biswas@tudelft.nl" }]
license = { text = "BSD-3-Clause" }
readme = "README.md"
requires-python = ">=3.10, <4"
requires-python = ">=3.12, <4"
dependencies = [
"fair (==2.1.3)",
"h5py (==3.13.0)",
"platypus-opt (==1.4.1)",
"ema-workbench (==2.5.3)",
"ema-workbench (>=3.0.0)",
"scipy (>=1.10.0)",
"deap (>=1.4.0)",
"ipyparallel (>=8.6.1)",
Expand All @@ -19,16 +19,21 @@ dependencies = [
"seaborn (>=0.13.0)",
"tyro (>=0.9.24)",
"wandb (==0.20.1)",
"numpy (==1.24.3)",
"numpy (>=1.26.0, <2.0)",
"pandas (>=2.1.0)",
"python-cdd (==0.0.98)",
"pytest (>=8.0)",
]

[project.optional-dependencies]
rl = [
"torch (==2.4.1)",
"SuperSuit (==3.9.3)",
"pettingzoo (==1.24.3)",
"python-cdd (==0.0.98)",
"momaland (==0.1.1)",
"gymnasium (==0.28.1)",
"morl-baselines (==1.0.0)",
"tensorboard (==2.19.0)",
"pytest (>=8.0)",
]

[build-system]
Expand Down
94 changes: 32 additions & 62 deletions run_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import numpy as np
from ema_workbench import (
Model,
Policy,
RealParameter,
ScalarOutcome,
CategoricalParameter,
Expand All @@ -28,14 +27,9 @@
SequentialEvaluator,
MPIEvaluator,
Constant,
Scenario,
perform_experiments,
)
from ema_workbench.em_framework.optimization import (
ArchiveLogger,
EpsilonProgress,
EpsNSGAII,
Sample,
)
from platypus import EpsNSGAII

from justice.util.EMA_model_wrapper import (
model_wrapper_emodps,
Expand Down Expand Up @@ -298,12 +292,12 @@ def run_optimization_adaptive(
centers_shape = n_rbfs * n_inputs
weights_shape = len(data_loader.REGION_LIST) * n_rbfs

centers = [RealParameter(f"center {i}", -1.0, 1.0) for i in range(centers_shape)]
centers = [RealParameter(f"center_{i}", -1.0, 1.0) for i in range(centers_shape)]
radii = [
RealParameter(f"radii {i}", SMALL_NUMBER, 1.0) for i in range(centers_shape)
RealParameter(f"radii_{i}", SMALL_NUMBER, 1.0) for i in range(centers_shape)
]
weights = [
RealParameter(f"weights {i}", SMALL_NUMBER, 1.0) for i in range(weights_shape)
RealParameter(f"weights_{i}", SMALL_NUMBER, 1.0) for i in range(weights_shape)
]
model.levers = centers + radii + weights

Expand All @@ -316,7 +310,7 @@ def run_optimization_adaptive(
),
]

reference_scenario = Scenario("reference", ssp_rcp_scenario=reference_index)
reference_scenario = Sample("reference", ssp_rcp_scenario=reference_index)

filename = f"{social_welfare_function.value[1]}_{nfe}_{seed}.tar.gz"
timestamp = datetime.datetime.now().strftime("%Y_%m") # _%d_%H_%M_%S
Expand All @@ -332,18 +326,6 @@ def run_optimization_adaptive(
os.makedirs(directory_name, exist_ok=True)

rank = _mpi_rank()
if rank == 0:
convergence = [
ArchiveLogger(
directory_name,
[lever.name for lever in model.levers],
[outcome.name for outcome in model.outcomes],
base_filename=filename,
),
EpsilonProgress(),
]
else:
convergence = []

if optimizer == Optimizer.EpsNSGAII:
algorithm_class = EpsNSGAII
Expand All @@ -368,7 +350,8 @@ def run_optimization_adaptive(
nfe=nfe,
epsilons=epsilons,
reference=reference_scenario,
convergence=convergence,
filename=filename,
directory=directory_name,
population_size=population_size,
algorithm=algorithm_class,
)
Expand All @@ -379,7 +362,8 @@ def run_optimization_adaptive(
nfe=nfe,
epsilons=epsilons,
reference=reference_scenario,
convergence=convergence,
filename=filename,
directory=directory_name,
population_size=population_size,
algorithm=algorithm_class,
)
Expand All @@ -390,7 +374,8 @@ def run_optimization_adaptive(
nfe=nfe,
epsilons=epsilons,
reference=reference_scenario,
convergence=convergence,
filename=filename,
directory=directory_name,
population_size=population_size,
algorithm=algorithm_class,
)
Expand Down Expand Up @@ -524,15 +509,15 @@ def run_optimization_momadps(
levers = []
for macro_idx in range(n_macro_regions):
levers.extend(
RealParameter(f"center {macro_idx} {i}", -1.0, 1.0)
RealParameter(f"center_{macro_idx}_{i}", -1.0, 1.0)
for i in range(centers_len)
)
levers.extend(
RealParameter(f"radii {macro_idx} {i}", SMALL_NUMBER, 1.0)
RealParameter(f"radii_{macro_idx}_{i}", SMALL_NUMBER, 1.0)
for i in range(radii_len)
)
levers.extend(
RealParameter(f"weights {macro_idx} {i}", SMALL_NUMBER, 1.0)
RealParameter(f"weights_{macro_idx}_{i}", SMALL_NUMBER, 1.0)
for i in range(weights_len)
)
model.levers = levers
Expand Down Expand Up @@ -570,7 +555,7 @@ def run_optimization_momadps(
),
]

reference_scenario = Scenario(
reference_scenario = Sample(
"reference", ssp_rcp_scenario=reference_ssp_rcp_scenario_index
)

Expand All @@ -589,15 +574,6 @@ def run_optimization_momadps(
rank = _mpi_rank()
lever_names = [lever.name for lever in model.levers]
outcome_names = [outcome.name for outcome in model.outcomes]
if rank == 0:
convergence = [
ArchiveLogger(
directory_name, lever_names, outcome_names, base_filename=filename
),
EpsilonProgress(),
]
else:
convergence = []

optimizer_map = {
Optimizer.EpsNSGAII: EpsNSGAII,
Expand Down Expand Up @@ -628,7 +604,8 @@ def run_optimization_momadps(
nfe=nfe,
epsilons=epsilons,
reference=reference_scenario,
convergence=convergence,
filename=filename,
directory=directory_name,
population_size=population_size,
algorithm=algorithm_class,
)
Expand Down Expand Up @@ -768,10 +745,10 @@ def run_single_agent_momadps(
for macro_idx in range(n_macro_regions):
action_row = policy_bank_df.iloc[actions[macro_idx]]
for i in range(centers_len):
fixed_centers[macro_idx, i] = action_row[f"center {macro_idx} {i}"]
fixed_radii[macro_idx, i] = action_row[f"radii {macro_idx} {i}"]
fixed_centers[macro_idx, i] = action_row[f"center_{macro_idx}_{i}"]
fixed_radii[macro_idx, i] = action_row[f"radii_{macro_idx}_{i}"]
for i in range(weights_len):
fixed_weights[macro_idx, i] = action_row[f"weights {macro_idx} {i}"]
fixed_weights[macro_idx, i] = action_row[f"weights_{macro_idx}_{i}"]

model.constants = [
Constant("n_regions", n_regions),
Expand Down Expand Up @@ -814,14 +791,14 @@ def run_single_agent_momadps(

levers = []
for i in range(centers_len):
levers.append(RealParameter(f"center {variable_macro_index} {i}", -1.0, 1.0))
levers.append(RealParameter(f"center_{variable_macro_index}_{i}", -1.0, 1.0))
for i in range(radii_len):
levers.append(
RealParameter(f"radii {variable_macro_index} {i}", SMALL_NUMBER, 1.0)
RealParameter(f"radii_{variable_macro_index}_{i}", SMALL_NUMBER, 1.0)
)
for i in range(weights_len):
levers.append(
RealParameter(f"weights {variable_macro_index} {i}", SMALL_NUMBER, 1.0)
RealParameter(f"weights_{variable_macro_index}_{i}", SMALL_NUMBER, 1.0)
)
model.levers = levers

Expand All @@ -834,7 +811,7 @@ def run_single_agent_momadps(
ScalarOutcome("fraction_above_threshold", kind=ScalarOutcome.MINIMIZE),
]

reference_scenario = Scenario(
reference_scenario = Sample(
"reference", ssp_rcp_scenario=reference_ssp_rcp_scenario_index
)

Expand All @@ -852,17 +829,9 @@ def run_single_agent_momadps(
os.makedirs(directory_name, exist_ok=True)

rank = _mpi_rank()

lever_names = [lever.name for lever in model.levers]
outcome_names = [outcome.name for outcome in model.outcomes]
if rank == 0:
convergence = [
ArchiveLogger(
directory_name, lever_names, outcome_names, base_filename=filename
),
EpsilonProgress(),
]
else:
convergence = []

set_ema_context(
model=model,
Expand All @@ -884,7 +853,8 @@ def run_single_agent_momadps(
nfe=nfe,
epsilons=epsilons,
reference=reference_scenario,
convergence=convergence,
filename=filename,
directory=directory_name,
population_size=population_size,
algorithm=algorithm_class,
)
Expand Down Expand Up @@ -918,7 +888,7 @@ def build_random_policy(model, seed=1234):
for lever in model.levers:
low, high = lever.lower_bound, lever.upper_bound
policy_data[lever.name] = rng.uniform(low, high)
return Policy("random_policy", **policy_data)
return Sample("random_policy", **policy_data)


def setup_model_from_config(config_path, mapping_base_path="data/input"):
Expand Down Expand Up @@ -1015,15 +985,15 @@ def setup_model_from_config(config_path, mapping_base_path="data/input"):
levers = []
for macro_idx in range(n_macro_regions):
levers.extend(
RealParameter(f"center {macro_idx} {i}", -1.0, 1.0)
RealParameter(f"center_{macro_idx}_{i}", -1.0, 1.0)
for i in range(centers_len)
)
levers.extend(
RealParameter(f"radii {macro_idx} {i}", SMALL_NUMBER, 1.0)
RealParameter(f"radii_{macro_idx}_{i}", SMALL_NUMBER, 1.0)
for i in range(radii_len)
)
levers.extend(
RealParameter(f"weights {macro_idx} {i}", SMALL_NUMBER, 1.0)
RealParameter(f"weights_{macro_idx}_{i}", SMALL_NUMBER, 1.0)
for i in range(weights_len)
)
model.levers = levers
Expand Down
Loading
Loading