trial for variability

This commit is contained in:
Joshua Burman
2022-03-25 13:58:31 -04:00
parent 1f00e1e1bc
commit 11a5112812
2 changed files with 147 additions and 79 deletions

View File

@ -2,6 +2,7 @@ from pydantic import BaseModel
from typing import List, Literal, Optional
import logging
import random
from models.item import Item
from models.constraint import Constraint
@ -89,7 +90,7 @@ class SolverRun(BaseModel):
# temporary compensator for bundle item limits, since we shouldn't be using cases with less than 3 items
# ideally this should be in the bundles model as a new attribute to handle "constraints of constraints"
logging.info('Removing bundles with items < 3')
for k,v in enumerate(self.bundles):
for k, v in enumerate(self.bundles):
bundle = self.bundles[k]
if bundle.count < 3: del self.bundles[k]
@ -103,4 +104,26 @@ class SolverRun(BaseModel):
# since the only bundles are based on passage id currently
# in the future when we have more than just passage based bundles
# we'll need to develop a more sophisticated way of handling this concern
return [item for item in self.items if item.passage_id == None]
bundle_constraints = (
constraint.reference_attribute for constraint in self.constraints
if constraint.reference_attribute.type == 'bundle')
if len(bundle_constraints) > 0:
return [item for item in self.items if item.passage_id == None]
else:
return self.items
def select_items_by_percent(self, percent) -> list[Item]:
items = self.unbundled_items()
total_items = len(items)
selected_items_amount = round(total_items - (total_items *
(percent / 100)))
return random.sample(items, selected_items_amount)
def select_bundles_by_percent(self, percent) -> list[Bundle]:
total_bundles = len(self.bundles)
selected_bundles_amount = round(total_bundles - (total_bundles *
(percent / 100)))
return random.sample(self.bundles, selected_bundles_amount)