From c15345867d9b7d100affcd0a269d6dd5f7241350 Mon Sep 17 00:00:00 2001 From: Joshua Burman Date: Tue, 14 Nov 2023 16:14:23 -0500 Subject: [PATCH] but when removing enemy constraints --- app/helpers/problem_helper.py | 20 +++++++++----------- app/models/item.py | 9 ++++++++- app/models/problem.py | 5 ++--- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/app/helpers/problem_helper.py b/app/helpers/problem_helper.py index 27f5629..84a6c85 100644 --- a/app/helpers/problem_helper.py +++ b/app/helpers/problem_helper.py @@ -3,21 +3,19 @@ from typing import Tuple from models.item import Item def sanctify(solved_items: [Item]) -> Tuple[list]: - sacred_ids = [] enemy_ids = [] # get all enemies for item in solved_items: - # if it has enemies, check if it exists as part of the solved items - for enemy_id in item.enemies: - # if it does, it's a true enemy - if enemy_id in (item.id for item in solved_items): - enemy_ids.append(enemy_id) - # remove enemy from solved items, - # lest it has this sacred item added to enemies - solved_items = [i for i in solved_items if i.id != enemy_id] + # if the item is already an an enemy + # then it's enemy is sacred + if item.id not in enemy_ids: + # if it has enemies, check if it exists as part of the solved items + for enemy_id in item.enemies: + # if it does, it's a true enemy + if enemy_id in (i.id for i in solved_items): + enemy_ids.append(enemy_id) - # the item is cleansed, now it's sacred - sacred_ids.append(item.id) + sacred_ids = [i.id for i in solved_items if i.id not in enemy_ids] return sacred_ids, enemy_ids \ No newline at end of file diff --git a/app/models/item.py b/app/models/item.py index b82dd03..2f34ed1 100644 --- a/app/models/item.py +++ b/app/models/item.py @@ -1,4 +1,4 @@ -from pydantic import BaseModel +from pydantic import BaseModel, validator from typing import List, Optional from models.attribute import Attribute @@ -16,6 +16,13 @@ class Item(BaseModel): b_param: float = 0.00 response: Optional[int] = None + @validator("enemies", pre=True) + def set_enemies(cls, v) -> List[id]: + if v == "": + return [] + enemies = list(filter(None, [int(enemy) for enemy in v.split(",")])) + return enemies + def iif(self, irt_model, theta): return ItemInformationFunction(irt_model).calculate(b_param=self.b_param, theta=theta) diff --git a/app/models/problem.py b/app/models/problem.py index 8976c64..fac4dec 100644 --- a/app/models/problem.py +++ b/app/models/problem.py @@ -8,7 +8,6 @@ from pulp import LpProblem, LpVariable, LpStatus, lpSum import logging from helpers.problem_helper import * - from helpers import service_helper from models.solution import Solution @@ -67,8 +66,8 @@ class Problem(BaseModel): logging.info('enemies found, adding constraints...') # remove old enemy/sacred constraints - self.problem.constraints.pop('Exclude_enemy_items') - self.problem.constraints.pop('Include_sacred_items') + if 'Exclude_enemy_items' in self.problem.constraints.keys(): self.problem.constraints.pop('Exclude_enemy_items') + if 'Include_sacred_items' in self.problem.constraints.keys(): self.problem.constraints.pop('Include_sacred_items') # add constraint to not allow enemy items self.problem += lpSum([