Skip to content

Commit

Permalink
Implement goals
Browse files Browse the repository at this point in the history
  • Loading branch information
ExcaliburZero committed Jul 7, 2024
1 parent 6ec0a08 commit fe62e9f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
11 changes: 8 additions & 3 deletions ttdlgc/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import typer

from .events import Event, Solution
from .milp import create_milp, extract_solution, Constraint
from .milp import create_milp, extract_solution, Constraint, Goal
from .simulation import Simulation

SUCCESS = 0
Expand All @@ -28,7 +28,12 @@ def solve(
typer.Option(help="Filepath to write the generated choices to."),
] = None,
constraint: Annotated[
Optional[list[Constraint]], typer.Option(help="Constraints to add to the MILP.")
Optional[list[Constraint]],
typer.Option(help="Hard constraints to add to the MILP."),
] = None,
goal: Annotated[
Optional[Goal],
typer.Option(help="Goal/preference to have the solver optimize towards."),
] = None,
verbose: bool = False,
) -> None:
Expand All @@ -42,7 +47,7 @@ def solve(
if constraint is None:
constraint = []

problem = create_milp(events, constraint)
problem = create_milp(events, constraint, goal)
logger.debug(problem)

solver = pulp.PULP_CBC_CMD(msg=0)
Expand Down
19 changes: 15 additions & 4 deletions ttdlgc/milp.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ class Constraint(enum.Enum):
ChaosLv5AtEnd = "ChaosLv5AtEnd"


class Goal(enum.Enum):
MaximizeLaw = "MaximizeLaw"
MaximizeGrey = "MaximizeGrey"
MaximizeChaos = "MaximizeChaos"


def extract_solution(events: list[Event], problem: pulp.LpProblem) -> Solution:
variables = problem.variablesDict()

Expand Down Expand Up @@ -72,7 +78,7 @@ def extract_solution(events: list[Event], problem: pulp.LpProblem) -> Solution:


def create_milp(
events: list[Event], constraints: Iterable[Constraint]
events: list[Event], constraints: Iterable[Constraint], goal: Optional[Goal]
) -> pulp.LpProblem:
problem = pulp.LpProblem("LGC_Alignment", pulp.LpMaximize)

Expand All @@ -95,9 +101,14 @@ def create_milp(
chaos_chapter_impacts: ImpactsDict = collections.defaultdict(lambda: [])

# Objective function
problem += law_chapter_variables[-1], "Maximize law alignment"
# problem += grey_chapter_variables[-1], "Maximize grey alignment"
# problem += chaos_chapter_variables[-1], "Maximize chaos alignment"
if goal is None:
problem += 0 == 0, "No goal"
elif goal == Goal.MaximizeLaw:
problem += law_chapter_variables[-1], "Maximize law alignment"
elif goal == Goal.MaximizeGrey:
problem += grey_chapter_variables[-1], "Maximize grey alignment"
elif goal == Goal.MaximizeChaos:
problem += chaos_chapter_variables[-1], "Maximize chaos alignment"

# Chapter 5 route
law_route = pulp.LpVariable("chapter_5_route_law", cat=pulp.const.LpBinary)
Expand Down

0 comments on commit fe62e9f

Please sign in to comment.