Example #1
0
 def test_solve(self):
     grp = Groupoid(data=[1, 1, 2, 0, 2, 0, 0, 2, 1])
     to = TermOperation(grp, term_variables=["x", "y", "z"])
     self.assertEqual(to.solve("000**"), 1)
     grp = Groupoid(data=[2, 1, 2, 1, 0, 0, 0, 0, 1])
     to = TermOperation(grp, term_variables=["x", "y", "z"])
     self.assertEqual(to.solve("0011***"), 2)
Example #2
0
 def test_is_solution(self):
     grp = Groupoid()
     to = TermOperation(grp)
     self.assertTrue(
         to.is_solution([[0], [1], [0], [2]], [[0], [1], [0], [2]]))
     self.assertTrue(
         to.is_solution([[0], [1], [0], [2]],
                        [[0, 1, 2], [0, 1], [0, 1], [0, 2]]))
Example #3
0
 def test_compute(self):
     grp = Groupoid(data=[1, 1, 2, 0, 2, 0, 0, 2, 1])
     to = TermOperation(grp, term_variables=["x", "y", "z"])
     to.target = to.get_ternary_descriminator_target_array()
     sol = to.compute(
         "zzxy**yy***zyy*zz*xz*xz**zyz**zy*yx***yz***zxz***xy*y***zz***zz*y"
         "**y*zy***x**")
     for i in range(0, len(to.target)):
         self.assertEqual(sol[i], to.target[i])
     self.assertNotEqual(to.compute("xy*z*yz**"), to.target)
Example #4
0
 def test_compute_validity_array(self):
     grp = Groupoid(data=[1, 1, 2, 0, 2, 0, 0, 2, 1])
     to = TermOperation(grp, term_variables=["x", "y", "z"])
     has_validity_array, validity_array = to.compute_validity_array(
         "yxy**xx**F*", [[0], [1], [2], [0], [0], [0], [0], [0], [0], [1],
                         [1], [1], [0], [1], [2], [1], [1], [1], [2], [2],
                         [2], [2], [2], [2], [0], [1], [2]])
     self.assertTrue(has_validity_array)
     self.assertEqual(validity_array,
                      [[0], [2], [1], [0], [0], [0], [0], [0], [0], [0, 1],
                       [0, 1], [0, 1], [0], [2], [1], [2], [2], [2], [1],
                       [1], [1], [1], [1], [1], [0], [2], [1]])
Example #5
0
    def test_r_array(self):
        grp = Groupoid(data=[2, 1, 2, 1, 0, 0, 0, 0, 1])
        to = TermOperation(grp)
        r_array = to.r_array([[0], [1], [0], [2]],
                             [[0, 1], [0], [0, 1, 2], [0, 2]])
        self.assertEqual(r_array, [[1], [1, 2], [0, 1, 2], [0, 1]])

        r_array = to.r_array([[0], [1], [0], [2]],
                             [[1], [0, 1, 2], [0, 1, 2], [0]])
        self.assertEqual(r_array, [[1], [0, 1, 2], [0, 1, 2], [0, 1]])

        r_array = to.r_array([[0], [1], [0], [2]], [[1, 2], [1], [1], [0]])
        self.assertEqual(r_array, [[0, 1, 2], [0], [1], [0, 1]])
Example #6
0
def main():
    args = parse_argments()
    # create groupoid table
    grp = Groupoid(args.groupoid)

    # setup term operation
    to_options = {}
    if args.term_variables:
        to_options["term_variables"] = args.term_variables
    to = TermOperation(grp,
                       **to_options)
    if args.target:
        to.target = \
            [[int(v) for v in t.split(",")] for t in args.target]
    elif args.target_random:
        to.target = to.get_random_target_array()
    elif args.target_ternary_descriminator:
        to.target = to.get_ternary_descriminator_target_array()

    if args.target_free_count > 0:
        to.target = to.get_filled_target_array(to.target,
                                               args.target_free_count)

    mtgm = args.male_term_generation_method
    mintl = args.min_term_length
    maxtl = args.max_term_length

    verbose = args.verbose
    print_summary = args.print_summary

    # BEAM specific arguments
    include_validity_array = args.include_validity_array
    beam_width = args.beam_width
    promotion_child_count = args.promotion_child_count

    prob = args.probability
    algorithm = args.algorithm
    if algorithm == "DDA":
        if include_validity_array:
            raise ValueError("The --include-validity-array (-iva) option "
                             "only applies to the BEAM algorithm.")
        elif beam_width:
            raise ValueError("The --beam-width (-bw) option only applies to "
                             "the BEAM algorithm.")
        elif promotion_child_count:
            raise ValueError("The --promotion_child_count (-pcc) option only "
                             "applies to the BEAM algorithm.")
        # run the deep drilling algorithm
        dda = DeepDrillingAlgorithm(grp, to,
                                    male_term_generation_method=mtgm,
                                    term_expansion_probability=prob)
        dda.run(verbose=verbose, print_summary=print_summary)
    elif algorithm == "BEAM":
        if include_validity_array and not verbose:
            raise ValueError("The --verbose (-v) option must be set for the "
                             "--include-validity-array (-iva) option "
                             "to apply.")
        if beam_width is None:
            beam_width = 3
        if promotion_child_count is None:
            promotion_child_count = 2
        if promotion_child_count > beam_width:
            raise ValueError("The --promotion_child_count (-pcc) value must "
                             "less than or equal to the beam width.")

        # run the beam algorithm
        beam = BeamEnumerationAlgorithm(grp,
                                        to,
                                        male_term_generation_method=mtgm,
                                        min_term_length=mintl,
                                        max_term_length=maxtl,
                                        term_expansion_probability=prob,
                                        beam_width=beam_width,
                                        promotion_child_count=
                                        promotion_child_count)
        beam.run(verbose=verbose, print_summary=print_summary,
                 include_validity_array=include_validity_array)