Skip to content
Merged
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
6 changes: 6 additions & 0 deletions changelog_entry.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- bump: patch
changes:
added:
- Policy and Dynamic classes now support addition operator (__add__) to combine policies and dynamics.
- Parameter values are appended when combining policies/dynamics.
- Simulation modifiers are chained in sequence when combining policies/dynamics.
26 changes: 26 additions & 0 deletions src/policyengine/core/dynamic.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,29 @@ class Dynamic(BaseModel):
simulation_modifier: Callable | None = None
created_at: datetime = Field(default_factory=datetime.now)
updated_at: datetime = Field(default_factory=datetime.now)

def __add__(self, other: "Dynamic") -> "Dynamic":
"""Combine two dynamics by appending parameter values and chaining simulation modifiers."""
if not isinstance(other, Dynamic):
return NotImplemented

# Combine simulation modifiers
combined_modifier = None
if self.simulation_modifier is not None and other.simulation_modifier is not None:

def combined_modifier(sim):
sim = self.simulation_modifier(sim)
sim = other.simulation_modifier(sim)
return sim

elif self.simulation_modifier is not None:
combined_modifier = self.simulation_modifier
elif other.simulation_modifier is not None:
combined_modifier = other.simulation_modifier

return Dynamic(
name=f"{self.name} + {other.name}",
description=f"Combined dynamic: {self.name} and {other.name}",
parameter_values=self.parameter_values + other.parameter_values,
simulation_modifier=combined_modifier,
)
26 changes: 26 additions & 0 deletions src/policyengine/core/policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,29 @@ class Policy(BaseModel):
simulation_modifier: Callable | None = None
created_at: datetime = Field(default_factory=datetime.now)
updated_at: datetime = Field(default_factory=datetime.now)

def __add__(self, other: "Policy") -> "Policy":
"""Combine two policies by appending parameter values and chaining simulation modifiers."""
if not isinstance(other, Policy):
return NotImplemented

# Combine simulation modifiers
combined_modifier = None
if self.simulation_modifier is not None and other.simulation_modifier is not None:

def combined_modifier(sim):
sim = self.simulation_modifier(sim)
sim = other.simulation_modifier(sim)
return sim

elif self.simulation_modifier is not None:
combined_modifier = self.simulation_modifier
elif other.simulation_modifier is not None:
combined_modifier = other.simulation_modifier

return Policy(
name=f"{self.name} + {other.name}",
description=f"Combined policy: {self.name} and {other.name}",
parameter_values=self.parameter_values + other.parameter_values,
simulation_modifier=combined_modifier,
)