Ejemplo n.º 1
0
    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)
Ejemplo n.º 2
0
    def crossover_specimens(self, specimen1, specimen2):
        # Create L-System
        product = ProductionGame.from_crossover(specimen1["product"],
                                                specimen2["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 = crossover_fractions(specimen1["angles"], specimen2["angles"])
        magnitudes = crossover_fractions(specimen1["magnitudes"],
                                         specimen2["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": ""
        }