def createSpecimen(self): stake = messup_stake(V2dList.from_dalmatian_string(choice(self.pool["stakes"]), sep=",")) spaghetti = randint(1, 2) is 1 fxWeight = FractionList.from_string(self.pool["fx-weights"]).choice() deltas = V2dList.from_dalmatian_list(self.fractionList.signed_sample_list(len(stake), 2)) tweaks = self.fractionList.signed_sample_list(len(stake), 5) points = stake + (deltas*fxWeight) product = ProductionGame(chainlength = len(points) -1) product.set_constants("ZMLCQST").set_vars("IJK") product.init_with_random_rules(levels = 2, keyrules = self.pool["rules"]) product.produce() product_obj = product.to_obj() brush = toBrush(product.core_chain(), points, fxWeight, tweaks, spaghetti) frequency_info = ", ".join(["{}:{}".format(k,v) for k, v in brush.action_frequency().items()]) ruleInfo = ", ".join([r["s"] + "->" + r["r"] for r in product_obj["rules"]]) summary = "Brush based on a stake of {} points, a weight of {} and the following rules {} starting with {} resulting in frequency {}".format(len(stake), fxWeight, ruleInfo , product_obj["start"], frequency_info) return { "id": self.incId(), "fx-weight": str(fxWeight), "stake": stake.to_dalmatian_list(), "spaghetti": spaghetti, "deltas": deltas.to_dalmatian_list(), "tweaks": tweaks, "points": points.to_dalmatian_list(), "product": product_obj, "brush": brush.to_dalmatian_string(), "brush-svg": brush.to_svg_string(BRUSH_WIDTH), "brush-cartesian": brush.to_core_cartesian_string(BRUSH_WIDTH), "summary": summary, "tags": "" }
def test_create(self): product = ProductionGame(chainlength = 60) product.set_constants("LQC").set_vars("IJK") product.init_with_random_rules(levels = 2, keyrules = ["L", "QQ", "C"]) product.produce() result = product.to_obj() self.assertEqual(len(result["rules"]), 3) self.assertGreaterEqual(len(result["chain"]), len(result["core-chain"])) self.assertGreaterEqual(len(result["core-chain"]), 60)
def test_breeding(self): obj1 = { "constants": "ABC", "variables": "IJ", "start": "AIAI", "rules": [ { "s": "I", "r": "BCJJ" }, { "s": "J", "r": "ABII" } ] } obj2 = { "constants": "AD", "variables": "JK", "start": "DJJD", "rules": [ { "s": "J", "r": "AJJA" }, { "s": "K", "r": "DKDK" } ] } product = ProductionGame.from_crossover(obj1, obj2, 30) product.produce() expected_rules = [ { "s": "I", "r": "BCJJ" }, { "s": "J", "r": "ABIA" }, { "s": "K", "r": "DKDK" } ] self.assertEqual(product.to_obj()["constants"], "ABCD") self.assertEqual(product.to_obj()["variables"], "IJK") self.assertEqual(product.to_obj()["start"], "DIAD") self.assertEqual(product.to_obj()["rules"], expected_rules)
def test_convert(self): product = ProductionGame(chainlength = 60) product.set_constants("LQC").set_vars("IJK") product.init_with_random_rules(levels = 2, keyrules = ["L", "QQ", "C"]) product.produce() self.assertEqual(ProductionGame.from_obj(product.to_obj()), product)
def mutation_specimen(self, specimen): # Create L-System product = ProductionGame.from_obj(specimen["product"]) product.produce() product_obj = product.to_obj() # Convert chain to brushstokes tortugaconfig = TortugaConfig().set_magnitude_page_ratio( self.init.magnitude_page_ratio) tortugaconfig.set_scale_magnitude_ratio_string( self.init.scale_magnitude_ratio) tortugaconfig.set_brushstoke_angle_offset( self.init.brushstoke_angle_offset) tortugaconfig.set_xy(self.init.xy) angles = mutate_fraction(specimen["angles"], self.pool.angles) magnitudes = mutate_fraction(specimen["magnitudes"], self.pool.magnitudes) tortugaconfig.set_angles_string(angles) tortugaconfig.set_magnitudes_string(magnitudes) tortugaconfig.set_brush_ids(self.init.brushids) tortugaconfig.set_chain(product.chain) brushstokes = TortugaProducer(tortugaconfig).produce() bstats = V2dList([bs.xy for bs in brushstokes]) # Create stencil aka DalmatianMedia stencil = DalmatianMedia(DlmtHeaders().set_brush_page_ratio( self.init.brush_page_ratio)) stencil.add_view_string( "view i:1 lang en xy 0 0 width 1 height 1 flags o tags all but [ ] -> everything" ) stencil.add_tag_description_string( "tag i:1 lang en same-as [] -> default") for brush in self.init.brushes: stencil.add_brush_string(brush) stencil.set_brushstrokes(brushstokes) allbr = stencil.page_brushstroke_list_for_view_string( "view i:1 lang en xy 0 0 width 1 height 1 flags o tags all but [ ] -> everything" ) fitbr = stencil.page_brushstroke_list_for_view_string( "view i:1 lang en xy 0 0 width 1 height 1 flags O tags all but [ ] -> everything" ) fitness = Fraction(len(fitbr), len(allbr)) ruleInfo = ", ".join( [r["s"] + "->" + r["r"] for r in product_obj["rules"]]) correlation = bstats.get_correlation() medianpoint = bstats.get_median_range(8) if correlation > 0.9 or correlation < -0.9: print("C", end="") return None if float(fitness) < 0.8: print("F", end="") return None if medianpoint.x < self.init.min_median_range.x: print("X", end="") return None if medianpoint.y < self.init.min_median_range.y: print("Y", end="") return None summary = "Stencil manually selected from carefully crafted generations using a Lindenmayer system approach based on angles [ {} ], magnitudes [ {} ] and the rules {} starting with {} resulting in {} brushstokes with a visibility of {:.2%}, correlation of {} and a median range of {}".format( angles, magnitudes, ruleInfo, product_obj["start"], len(brushstokes), float(fitness), correlation, medianpoint.to_float_string()) return { "id": self.inc_id(), "product": product_obj, "angles": angles, "magnitudes": magnitudes, "stencil": stencil.to_obj(), "summary": summary, "tags": "" }