示例#1
0
文件: text.py 项目: bmetevier/pyshgp
def _all_chars(s):
    return [Char(c) for c in list(s)[::-1]]
示例#2
0
文件: text.py 项目: bmetevier/pyshgp
def _char_from_bool(b):
    if b:
        return Char("T"),
    return Char("F"),
示例#3
0
文件: text.py 项目: bmetevier/pyshgp
def _char_from_ascii(i):
    return Char(chr(i % 128)),
示例#4
0
def random_char():
    """Return a random character."""
    return Char(choice(_possible_chars))
     "in": {"int": [101]},
     "ex": {"int": [101, 101]}
 },
 {
     "instr": "float_dup_times",
     "in": {"int": [3], "float": [1.23]},
     "ex": {"int": [], "float": [1.23, 1.23, 1.23]}
 },
 {
     "instr": "bool_dup_times",
     "in": {"int": [sys.maxsize + 1], "bool": [True]},
     "ex": {"int": [], "bool": [True] * 500}
 },
 {
     "instr": "char_swap",
     "in": {"char": [Char("a"), Char("b")]},
     "ex": {"char": [Char("b"), Char("a")]}
 },
 {
     "instr": "str_rot",
     "in": {"str": ["A", "B", "C"]},
     "ex": {"str": ["B", "C", "A"]}
 },
 {
     "instr": "bool_flush",
     "in": {"bool": [True, False]},
     "ex": {"bool": []}
 },
 {
     "instr": "int_eq",
     "in": {"int": [5, 6], "bool": []},
y_train_synthetic = [target_function(x[0]) for x in X_train_synthetic]

X_train = X_train_edge + X_train_synthetic
y_train = y_train_edge + y_train_synthetic

X_test = [[synthetic_input()] for _ in range(100)]
y_test = [target_function(x[0]) for x in X_test]

# Spawner

instruction_set = (InstructionSet().register_by_type(
    ["int", "bool", "string", "char", "exec", "stdout"]).register_n_inputs(1))

spawner = GeneSpawner(
    instruction_set=instruction_set,
    literals=[Char(" "), Char("\n")],
    erc_generators=[lambda: Char(choice(_possible_chars)), synthetic_input],
)

# Estimator

est = PushEstimator(search="GA",
                    population_size=1000,
                    max_generations=100,
                    spawner=spawner,
                    last_str_from_stdout=True,
                    verbose=2)

if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO,
                        format="%(asctime)s %(levelname)s %(message)s",
 },
 {
     "instr": i_set["bool_dup_times"],
     "in": {
         "int": [sys.maxsize + 1],
         "bool": [True]
     },
     "ex": {
         "int": [],
         "bool": [True] * 500
     }
 },
 {
     "instr": i_set["char_swap"],
     "in": {
         "char": [Char("a"), Char("b")]
     },
     "ex": {
         "char": [Char("b"), Char("a")]
     }
 },
 {
     "instr": i_set["str_rot"],
     "in": {
         "str": ["A", "B", "C"]
     },
     "ex": {
         "str": ["B", "C", "A"]
     }
 },
 {
示例#8
0
 def test_push_type_of(self):
     lib = PushTypeLibrary()
     assert lib.push_type_of(7) == PushInt
     assert lib.push_type_of(True) == PushBool
     assert lib.push_type_of("ABC") == PushStr
     assert lib.push_type_of(Char("Z")) == PushChar
示例#9
0
 def test_long_char(self):
     with pytest.raises(PushError):
         Char("abc")
示例#10
0
 def test_empty_char(self):
     with pytest.raises(PushError):
         Char("")