typos, factoring out functionality
This commit is contained in:
parent
8667bec8d5
commit
4e8c8416b6
23
app/helpers/problem_helper.py
Normal file
23
app/helpers/problem_helper.py
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
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]
|
||||||
|
|
||||||
|
# the item is cleansed, now it's sacred
|
||||||
|
sacred_ids.append(item.id)
|
||||||
|
|
||||||
|
return sacred_ids, enemy_ids
|
@ -7,7 +7,9 @@ from pulp import LpProblem, LpVariable, LpStatus, lpSum
|
|||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from helpers import service_helper, irt_helper
|
from helpers.problem_helper import *
|
||||||
|
|
||||||
|
from helpers import service_helper
|
||||||
|
|
||||||
from models.solution import Solution
|
from models.solution import Solution
|
||||||
from models.item import Item
|
from models.item import Item
|
||||||
@ -59,29 +61,14 @@ class Problem(BaseModel):
|
|||||||
# get items from solution
|
# get items from solution
|
||||||
solved_items, _ = service_helper.solution_items(self.problem.variables(), solver_run)
|
solved_items, _ = service_helper.solution_items(self.problem.variables(), solver_run)
|
||||||
|
|
||||||
sacred_ids = []
|
sacred_ids, enemy_ids = sanctify(solved_items)
|
||||||
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]
|
|
||||||
|
|
||||||
# the item is cleansed, now it's sacred
|
|
||||||
sacred_ids.append(item.id)
|
|
||||||
|
|
||||||
if enemy_ids:
|
if enemy_ids:
|
||||||
logging.info('enemies found, adding constraints...')
|
logging.info('enemies found, adding constraints...')
|
||||||
|
|
||||||
# remove old enemy/sacred constraints
|
# remove old enemy/sacred constraints
|
||||||
self.problem.constrants.pop('Exclude_enemy_items')
|
self.problem.constraints.pop('Exclude_enemy_items')
|
||||||
self.problem.constrants.pop('Include_sacred_items')
|
self.problem.constraints.pop('Include_sacred_items')
|
||||||
|
|
||||||
# add constraint to not allow enemy items
|
# add constraint to not allow enemy items
|
||||||
self.problem += lpSum([
|
self.problem += lpSum([
|
||||||
|
Loading…
x
Reference in New Issue
Block a user