def decode_variable( self, variable: int) -> Tuple[Factor, Union[SimpleLevel, DerivedLevel]]: """Given a variable number from the SAT formula, this method will return the associated factor and level name. """ # Shift to zero-based index variable -= 1 if variable < self.grid_variables(): variable = variable % self.variables_per_trial() simple_factors = list( filter(lambda f: not f.has_complex_window, self.design)) simple_tuples = get_all_levels(simple_factors) return simple_tuples[variable] else: complex_factors = list( filter(lambda f: f.has_complex_window, self.design)) for f in complex_factors: start = self.first_variable_for_level(f, f.levels[0]) end = start + self.variables_for_factor(f) if variable in range(start, end): tuples = get_all_levels([f]) return tuples[(variable - start) % len(f.levels)] raise RuntimeError('Unable to find factor/level for variable!')
def first_variable_for_level(self, factor: Factor, level: Any) -> int: """Returns the first index for this variable in a trial sequence representing the given factor and level. (0-based.) """ if not isinstance(level, (SimpleLevel, DerivedLevel)): raise ValueError( f"Attempted to find first variable of non-Level object: {level}." ) if factor.has_complex_window: offset = 0 complex_factors = filter(lambda f: f.has_complex_window, self.design) for f in complex_factors: if f == factor: offset += f.levels.index(level) break else: offset += self.variables_for_factor(f) return self.grid_variables() + offset else: simple_factors = list( filter(lambda f: not f.has_complex_window, self.design)) simple_levels = get_all_levels(simple_factors) return simple_levels.index((factor, level))
def assert_atmostkinarow(c: at_most_k_in_a_row, experiments: List[dict]) -> None: if isinstance(c.level, Factor): __assert_atmostkinarow_factor(c.k, c.level, experiments) else: level_tuples = get_all_levels([c.level]) for t in level_tuples: __assert_atmostkinarow_pair(c.k, t, experiments)
def first_variable_for_level(self, factor: Factor, level: Any) -> int: if (type(level) is not SimpleLevel and type(level) is not DerivedLevel): print("Attempt to find first variable for a non-level object " + str(level)) if factor.has_complex_window(): offset = 0 complex_factors = filter(lambda f: f.has_complex_window(), self.design) for f in complex_factors: if f == factor: offset += f.levels.index(level) break else: offset += self.variables_for_factor(f) return self.grid_variables() + offset else: simple_factors = list( filter(lambda f: not f.has_complex_window(), self.design)) simple_levels = get_all_levels(simple_factors) return simple_levels.index((factor, level))