Esempio n. 1
0
def get_data(n, ic=None, apply_subclass=False):
    if apply_subclass:
        n = cca.get_subclass(n)

    if ic == None:
        ic = [rd.randrange(0, k) for _ in range(length)]

    te = ca.cellularautomata(n, k, r, ic, t, transient)

    x_result = cca.power_spectrum(cca.get_fft(te))
    y_result = [0 for _ in range(4)]
    y_result[cca.wolfram_class(n) - 1] = 1

    # Get data
    return x_result, y_result
Esempio n. 2
0
def feedforward_classify_space_rule(k, r, size, total_ics, model_path, output_file, output_encoding='utf-8'):
    t = 3*size
    transient = 2*size

    m = 2*r + 1
    rule_size = int(pow(k, pow(k, m)))

    train_data_size = total_ics

    # Create the Estimator
    print("Create the Estimator")
    ca_classifier = tf.estimator.Estimator(
        model_fn=model_function_with_dropout, model_dir=model_path)

    # Predict CA in ray 1 space
    with open(output_file, mode='w', encoding=output_encoding) as a_file:
        for n in range(rule_size):
            print("Rule:", n)

            eval_data = make_set_pair_ne_label(train_data_size, k, r, t, transient)

            eval_input_fn = tf.estimator.inputs.numpy_input_fn(
                x={"x": np.array(eval_data[0], dtype=np.float32)},
                num_epochs=1,
                shuffle=False)

            result_predict = ca_classifier.predict(eval_input_fn, predict_keys=None, hooks=None)

            eval_prob = [p['probabilities'] for p in result_predict]
            eval_prob = [np.sum(line) for line in np.transpose(eval_prob)]
            eval_prob = eval_prob / np.sum(eval_prob)

            a_file.write(str(n))
            a_file.write("\t")
            a_file.write(str(cca.wolfram_class(n)))
            a_file.write("\t")
            a_file.write(str(np.argmax(eval_prob) + 1))
            a_file.write("\t")
            for val in eval_prob:
                a_file.write(str(val))
                a_file.write("\t")
            a_file.write("\n")

            a_file.flush()
Esempio n. 3
0
def make_set_pair_ne_label(total_ics, k, r, t, transient = 0):
    '''
    '''
    ics = cca.init_conditions_numbers(k, (t - transient), total_ics)

    size = (t - transient)

    data = []
    label = []
    rules = []

    for ic in ics:
        classes = cca.WolframClasses[rnd.randint(0, 3)]
        n = classes[rnd.randint(0, len(classes) - 1)]

        te = ca.cellularautomata(n, k, r, ca.from_number_fix(ic, k, size), t, transient)

        plain_ca = ca.plain(nspec.te2ne(te, k, r))

        data.append(plain_ca)
        label.append(cca.wolfram_class(n) - 1)
        rules.append(n)
    
    return data, label, rules
Esempio n. 4
0
    print("Saving Neural Network...")
    save_path = saver.save(sess, checkpoint_prefix)

    print("Predicting class...")
    print("States: ", k, " Ray: ", r)

    with open(checkpoint_prefix + ".txt", "w+") as f:
        n = 0
        k = 2
        r = 1
        rule_size = int(np.power(k, np.power(k, 2 * r + 1)))

        for n in range(rule_size):
            prob = predict_class(sess, n, k, r, prediction)

            print(n, "/", rule_size, cca.wolfram_class(n), prob)

            f.write(str(n))
            f.write(str('\t'))
            f.write(str(cca.wolfram_class(n)))
            for p in prob:
                f.write(str('\t'))
                f.write(str(p))
            f.write(str('\n'))

            if n % 10 == 0:
                f.flush()

    print()
    print("Finish.")