combination based bundle solving

This commit is contained in:
spushy
2022-02-09 01:13:49 -05:00
parent 744abbb7b8
commit 8ce5e6e540
4 changed files with 139 additions and 74 deletions

View File

@ -1,6 +1,10 @@
from pydantic import BaseModel
from typing import List
from models.item import Item
class Bundle(BaseModel):
id: int
count: int
items: List[Item]
type: str

View File

@ -1,3 +1,5 @@
import logging
from pydantic import BaseModel
from typing import List, Optional
@ -20,17 +22,19 @@ class SolverRun(BaseModel):
advanced_options: Optional[AdvancedOptions]
engine: str
def get_item(self, item_id):
def get_item(self, item_id: int) -> Item or bool:
for item in self.items:
if str(item.id) == item_id:
return item
return False
def remove_items(self, items):
def remove_items(self, items: list[Item]) -> bool:
self.items = [item for item in self.items if item not in items]
return True
def generate_bundles(self):
logging.info('Generating Bundles...')
bundle_constraints = (constraint.reference_attribute for constraint in self.constraints if constraint.reference_attribute.type == 'bundle')
for bundle_constraint in bundle_constraints:
@ -53,16 +57,24 @@ class SolverRun(BaseModel):
self.bundles.append(Bundle(
id=attribute_id,
count=1,
items=[item],
type=type_attribute
))
else:
self.bundles[bundle_index].count += 1
self.bundles[bundle_index].items.append(item)
else:
self.bundles = [Bundle(
id=attribute_id,
count=1,
items=[item],
type=type_attribute
)]
def get_constraint(self, name):
def get_constraint(self, name: str) -> Constraint or None:
return next((constraint for constraint in self.constraints if constraint.reference_attribute.id == name), None)
# temp function until we build out bundles to more than just for cases
# for now it treats "bundle" attributes as a single unique constraint
def get_constraint_by_type(self, type: str) -> Constraint or None:
return next((constraint for constraint in self.constraints if constraint.reference_attribute.type == type), None)