def iteration(self) -> None: '''Updates 'c' and 'wsoup'.''' ltsoup.decay() wsoup.decay() p = self.choose_painter() p.mpaint(self) p.boost_target(self) self.deboost(p) lo('NEW_W', f'{p.weight(self):8.3f}') self.pr()
def little_run() -> None: global rmem #rmem = RMemSalt(nsalt=10, niters=100) rmem = RMemSeqSalt.make_instance(niters=1000) eqnmaker = EquationMaker() lo('HERE1') rmem.absorb_canvases(eqnmaker()) lo('HERE2') rmem.run1([2, '+', None, None, 5])
def iteration(self) -> None: self.decay() painters = self.all_painters() weights = [self.weight_of(self, p) for p in painters] # TODO What if no painters or 0 weight? #pts(painters, key=short) i = choices(range(len(painters)), weights)[0] lo('CHOSE', f'{short(painters[i])} {weights[i]:8.3f}') p = painters[i] p.mpaint(self) p.boost_target(self) self.deboost(p)
def mk_func_args(cls, func: Callable, args: Args) -> Dict[str, Any]: d: Dict[str, Any] = {} args = as_argsmap(args) for param_name, param_type in cls.params_of(func): value = args.get(param_name, None) if not is_type_instance(value, param_type): # TODO Distinguish between missing argument and argument of # wrong type? lo('WRONGTYPE', param_name, param_type, value) print('???') lo('ARGS', type(args), short(args)) raise NotImplementedError # TODO MissingArgument else: d[param_name] = value return d
def xpg0() -> None: '''Simple experiment with global parameters: counts of certain types of Funcs appended to the Canvas.''' global rmem rmem = RMemAbs() #eqn = (1, '+', 1, '=', 2) eqn = (2, '+', 1, '=', 3) count_columns = [0, 0] add1 = rmem.add_n(1) for i in range(10): startc: CanvasAble = tuple( count_columns) + eqn # (count(same), count(+1)) lo(startc) painters = rmem.canvas_to_painters(startc) count_columns[0] = sum(1 for (a, b, f) in painters if f == rmem.same) count_columns[1] = sum(1 for (a, b, f) in painters if f == add1) lo(tuple(count_columns) + eqn)
def run(self, vv: Optional[int] = None) -> FidelityTestResult: '''Run a full test: all canvases and cues.''' vv: int = self.vv if vv is None else vv seed = reseed(self.seed) num_tests = 0 # number of tests actually run results: Dict[Tuple[BaseValue, ...], int] = defaultdict(int) if vv >= 1 and self.tspec: print(self.tspec) # Run the tests start_time = perf_counter() while num_tests <= self.nsamples: canvas = choice(self.initial_canvases) for _ in range(self.n_per_cue): num_tests += 1 cue = self.canvas_to_cue( canvas) # type: ignore[misc, operator] if vv >= 3: lo(' CUE', cue) got = as_tuple(self.rmem.regenerate(canvas=cue)) if vv >= 3: lo(' GOT', got) if vv >= 4: pr(self.rmem.lsteps) yes = self.is_success(canvas, got) # type: ignore[misc, operator] if yes: results[as_tuple(canvas)] += 1 if vv == 1: print('+' if yes else '.', end='', flush=True) duration = perf_counter() - start_time if vv == 1: print(flush=True) return FidelityTestResult( tspec=self.tspec, rmem=self.rmem, cue_maker=self.canvas_to_cue, # type: ignore[misc, arg-type] results=results, # type: ignore[arg-type] duration=duration, num_tests=num_tests, seed=seed)
def eqn_test( # TODO rename eqns_test show: bool = False, n_per_eqn: int = 3, n_eqns: int = 20, niters: int = 50, seed: int = None, operands=range(1, 11), operators=('+', '-', 'x', '/'), rm: Union[RMem, Type[RMem]] = RMemAbs, npartial: int = 3, ) -> EqnCounter: reseed(seed) full_table = tuple(make_eqns(operands=operands, operators=operators)) l = len(full_table[0]) rmem: RMem if isclass(rm): #rmem = rm.make_from(full_table) rmem = rm().absorb_canvases(full_table) else: rmem = rm # type: ignore[assignment] rmem.absorb_canvases(full_table) counter: EqnCounter = defaultdict(int) for eqn in sample_without_replacement(full_table, k=n_eqns): if show: print(eqn) for i in range(n_per_eqn): startc = partial_eqn(eqn, k=npartial) if show: lo('CUE', startc) got = rmem.regenerate(canvas=startc, niters=niters).as_tuple() if show: lo('GOT', got) if got[-len(eqn):] == eqn: counter[eqn] += 1 if show: print() else: print('.', end='') if not show: print() return counter
def quick_test(self, rmem_cls: Type[RMem], show: bool = False, **kwargs) -> int: '''Runs a quick test on an RMem, giving it a standard small set of canvases to absorb, and checking its ability to reconstruct a few equations.''' reseed(0) rmem = rmem_cls(**kwargs).absorb_canvases(self.eqns) #print(rmem.termination_threshold, rmem_cls.niters, rmem.niters) # type: ignore[attr-defined] num_correct = 0 for cc in self.ccs: for i in range(40): got = rmem.regenerate(cc.cue).as_tuple() if show: lo(cc.cue, got) if got[-len(cc.cue):] == cc.cue: num_correct += 1 if show: lo(f'num_correct = {num_correct}') return num_correct
def params_of(func: Callable) -> Iterable[Tuple[str, TypeAnnotation]]: # NEXT It looks like get_type_hints() needs *all* the types in the # global namespace. I might have to move params_of(), or a special # get_type_hints(), to its own file, which imports *everything*. Yuck! # func.__annotations__ holds strings rather than types if func was # imported. try: type_hints = get_type_hints( func, globalns=func.__globals__, localns=globals()) # type: ignore[attr-defined] except NameError: lo('PARAMS_OF', func, func.__annotations__) raise """ lo('PARAMS_OF', func, func.__annotations__) type_hints = func.__annotations__ """ for param_name in inspect.signature(func).parameters: if param_name == 'return': continue # disregard return type yield (param_name, type_hints.get(param_name, Any))
def xpg() -> Set[FrozenSet[Tuple]]: funcs = ( RMemFuncs.same, RMemFuncs.add_n(1), RMemFuncs.mul_by(2), RMemFuncs.add_n(2), RMemFuncs.sub_n( 1), # putting this one in lengthens the cycles by a lot ) lcsets: Set[FrozenSet[Tuple]] = set() # limit cycles, each unordered #for eqn in make_eqns(operands=range(1, 4), operators=['+', '-']): #for eqn in make_eqns(): #for eqn in [(9, '+', 9, '=', 18)]: for eqn in [(2, '+', 1, '=', 3)]: for startn in range(1000): #start_columns = (startn,) * len(funcs) start_columns = tuple(randrange(10) for _ in range(len(funcs))) lo(eqn) lc = find_limit_cycle(eqn, funcs, start_columns, show=False) lcsets.add(frozenset(lc)) print() return lcsets
def run(self, vv: Optional[int] = None) -> FidelityTestResult: vv: int = self.vv if vv is None else vv seed = reseed(self.seed) num_tests = 0 # number of tests actually run results: Dict[Tuple[BaseValue, ...], int] = defaultdict(int) rmem, initial_canvases_f, initial_canvases, cue_maker = \ self.make_setup() if vv >= 1: print() print( f'{short(rmem):40} niters={rmem.niters} {short(initial_canvases_f)} {short(cue_maker)}' ) #initial_canvases = set(initial_canvases) num_initial_canvases = len(initial_canvases) # Run the tests start_time = perf_counter() for canvas in sample_without_replacement(initial_canvases, k=self.nsamples): if vv >= 2: lo(canvas) for _ in range(self.n_per_sample): num_tests += 1 cue = cue_maker(canvas) if vv >= 3: lo(' CUE', cue) got = as_tuple(self.run1(cue, rmem, vv=vv)) if vv >= 3: lo(' GOT', got) yes = got[-len(canvas):] == canvas if yes: results[canvas] += 1 if vv == 1: print('+' if yes else '.', end='', flush=True) duration = perf_counter() - start_time if vv == 1: print(flush=True) return FidelityTestResult( tspec=self, rmem=rmem, initial_canvases_f=initial_canvases_f, cue_maker=cue_maker, results=results, # type: ignore[arg-type] duration=duration, num_tests=num_tests, num_initial_canvases=num_initial_canvases, seed=seed)
def boost_target(self, m: Model) -> None: m.boost(self.qpainter) lo('BOOSTED', f'{self.qpainter.weight(m):8.3f}', self.qpainter)
def xpgfid() -> None: '''Fidelity test: Does adding global count-columns enable the regeneration process to restore the original equation more often?''' # New absorb_canvas(): # For each pset in the limit cycle: # raw_absorb it # Measure fidelity on grade-school table without count-columns. # Measure fidelity on grade-school table with count-columns. cls = type('RM', (WithCountColumns, RMem), {}) rmem = cls() #eqn = (2, '+', 1, '=', 3) eqns = list(make_eqns(operands=range(1, 4), operators=['+'])) for eqn in eqns: rmem.absorb_canvas(eqn) for eqn in eqns: startc = (None, None) + partial_eqn(eqn, k=4) got = rmem.run_gset(startc, niters=1000) lo(eqn) lo(startc) lo(got) lo() pr(rmem.lsteps) print() lo(startc) lo(eqn)
def log(self, f: Indenting, **kwargs) -> None: lo(self.__class__, kwargs.get('cellref', None))