Beispiel #1
0
class Simulation(object):
    """The overall simulation."""
    def __init__(self, size=100):
        logging.info("Creating maze environment")
        self._maze = Maze().withDecisionReward(1)\
            .withGoalReward(20)\
            .withRevisitPenalty(2)\
            .withOutOfBoundsPenalty(2)

        logging.info("Creating gene builder")
        self._gb = GeneBuilder().withGeneset(self._maze.genes).withLength(8)\
            .withMutation(0.03)\
            .withCrossover(crossover_random)

        self._population_size = size
        self._population = None
        self._new_population = Generation(self._population_size, self._gb).from_random()
        self._new_population.log()

        self.generation = 0

    def _make_new_generation(self):
        self._new_population = Generation(self._population_size, self._gb).from_previous_generation(self._population)
        self.generation += 1

    @property
    def best_fitness(self):
        return self._population.best_fitness

    @property
    def best_genes(self):
        return self._population.best

    @property
    def mean(self):
        return self._population.mean_fitness

    def step_generation(self):
        self._population = self._new_population
        logging.info("Testing..")
        self._population.test(self._maze)
        logging.info("Breeding...")
        self._make_new_generation()
def main():
    comp = Computer()

    done = False

    successes = []
    gen_number = 0

    while not done:
        if gen_number % 10 == 0:
            print("Generation: {}".format(gen_number))
        gen_number += 1
        gen = Generation(10, [1, 2, 3, 4, 5])

        gen.run_all()

        successes += [result.program for result in gen.program_results if 3 in result.output]
        if len(successes) > 1:
            done = True

    pprint(successes)
Beispiel #3
0
    def __init__(self, size=100):
        logging.info("Creating maze environment")
        self._maze = Maze().withDecisionReward(1)\
            .withGoalReward(20)\
            .withRevisitPenalty(2)\
            .withOutOfBoundsPenalty(2)

        logging.info("Creating gene builder")
        self._gb = GeneBuilder().withGeneset(self._maze.genes).withLength(8)\
            .withMutation(0.03)\
            .withCrossover(crossover_random)

        self._population_size = size
        self._population = None
        self._new_population = Generation(self._population_size, self._gb).from_random()
        self._new_population.log()

        self.generation = 0
Beispiel #4
0
 def _make_new_generation(self):
     self._new_population = Generation(self._population_size, self._gb).from_previous_generation(self._population)
     self.generation += 1
Beispiel #5
0
 #fname = "Harvard1.mat"
 #fname = "Facebook-New-Orleans.edges.gz"
 #fname = 'soc-Epinions1.edges.gz'
 #fname = "email-Enron.edges.gz"
 #fname = "as-caida20071105.edges.gz"    
 
 run_case = 1
 
 ###### Full graph - 2K with triangles + Improved MCMC 
 if run_case == 1:
     myest = Estimation()
     myest.load_graph(fname)
     myest.calcfull_CCK()
     myest.calcfull_JDD()
     
     mygen = Generation()
     mygen.set_JDD( myest.get_JDD('full') )
     mygen.set_KTRI( myest.get_KTRI('full') ) 
     
     mygen.construct_triangles_2K()
     mygen.mcmc_improved_2_5_K(nmae_threshold=0.03)
     mygen.save_graphs('%s_2KT+ImpMCMC_Full' % fname)
 #######################################################
 ###### Full graph - 2K simple +  MCMC  
 elif run_case == 2:
     myest = Estimation()
     myest.load_graph(fname)
     myest.calcfull_CCK()
     myest.calcfull_JDD()
     
     mygen = Generation()
Beispiel #6
0
import pyglet
from pyglet.window import Window
from pyglet.window import key
from pyglet import shapes

import math

from Generation import Generation

from track import tracklines

window = Window(960, 700)
pyglet.gl.glClearColor(1, 1, 1, 1)

gen = Generation()

for car in gen.players:
    window.push_handlers(car.key_handler)


@window.event
def on_draw():
    window.clear()
    gen.draw()
    for line in tracklines:
        line.draw()

    # for car in gen.players:
    #     car.draw()

    # for car in gen.players:
Beispiel #7
0
def csv_test_unparallel():
    eg.__eigen_info = True
    print("Strategy," + "Graph," + str("EigErr") + "," + str("DegCorr") + "," +
          str("ClustRatio") + "," + str("EigVErr") + "," +
          str("NodeDistCorr") + "," + str("DegBetCorr") + "," +
          str("KCoreCorr") + "," + str("CommNeighDist") + "," +
          str("PartRatio") + "," + str("AvgNeighDegCorr") + "," +
          str("Connected"),
          file=sys.stderr)
    for filo in os.listdir("/home/baldo/tmp/graph_generator/PL200/"):
        with open("/home/baldo/tmp/graph_generator/PL200/" + filo, "r") as net:
            eg.info(filo)
            eg.info("Loading graph..")
            G = nx.read_weighted_edgelist(net)  # , delimiter=":")
            # G = read_graphml(net)
            A = nx.to_numpy_matrix(G)
            n = nm.shape(A)[0]
            joint_degrees = nx.algorithms.mixing.degree_mixing_dict(G)
            eg.info("Computing centrality..")
            x, l = eg.eigen_centrality(A)

            for i in range(10):
                eg.info("Run: " + str(i))

                eg.info("Building JDM graph..")
                H = joint_degree_graph(joint_degrees)
                B = nx.to_numpy_matrix(H)
                write_statistics(A, B, "2k", filo, x, l)

                eg.info("Building degree sequence graph..")
                H = nx.random_degree_sequence_graph((nx.degree(G).values()))
                B = nx.to_numpy_matrix(H)
                write_statistics(A, B, "1k", filo, x, l)

                precision = 0.01
                eg.info("Building eigen " + str(precision) + " graph..")
                B = eg.build_matrix(x, l, precision)
                write_statistics(A, B, "eig" + str(precision), filo, x, l)

                precision = 0.001
                eg.info("Building eigen " + str(precision) + " graph..")
                B = eg.build_matrix(x, l, precision)
                write_statistics(A, B, "eig" + str(precision), filo, x, l)

                precision = 0.0001
                eg.info("Building eigen " + str(precision) + " graph..")
                B = eg.build_matrix(x, l, precision)
                write_statistics(A, B, "eig" + str(precision), filo, x, l)

                m = 0.25
                eg.info("Building spectral " + str(m) + " graph..")
                B = eg.sample_simm_matrix(A, int(round(n * m)))
                write_statistics(A, B, "spectre" + str(m), filo, x, l)

                m = 0.5
                eg.info("Building spectral " + str(m) + " graph..")
                B = eg.sample_simm_matrix(A, int(round(n * m)))
                write_statistics(A, B, "spectre" + str(m), filo, x, l)

                m = 0.75
                eg.info("Building spectral " + str(m) + " graph..")
                B = eg.sample_simm_matrix(A, int(round(n * m)))
                write_statistics(A, B, "spectre" + str(m), filo, x, l)

                m = 0.9
                eg.info("Building spectral " + str(m) + " graph..")
                B = eg.sample_simm_matrix(A, int(round(n * m)))
                write_statistics(A, B, "spectre" + str(m), filo, x, l)

                m = 0.95
                eg.info("Building spectral " + str(m) + " graph..")
                B = eg.sample_simm_matrix(A, int(round(n * m)))
                write_statistics(A, B, "spectre" + str(m), filo, x, l)

                eg.info("Building D2.5 graph..")
                test25 = Estimation()
                gen25 = Generation()
                test25.load_graph("", graph=G)
                test25.calcfull_CCK()
                test25.calcfull_JDD()
                gen25.set_JDD(test25.get_JDD('full'))
                gen25.set_KTRI(test25.get_KTRI('full'))
                gen25.construct_triangles_2K()
                gen25.mcmc_improved_2_5_K(error_threshold=0.05)
                H = gen25.G
                B = nx.to_numpy_matrix(H)
                write_statistics(A, B, "25k", filo, x, l)
Beispiel #8
0
def launcher():
    with Generation() as gen:
        with PyQtToolBox.App(style=qdarkstyle.load_stylesheet_pyqt5(),
                             excepthook=True):
            launcher = WindowLauncher(gen)
        launcher.close_all()
Beispiel #9
0
def graph_worker_oneshot(inputlist, queue, print_queue):
    for duty in inputlist:
        name = duty[0]
        G = duty[1]
        algo = duty[2]
        param = duty[3]

        A = nx.to_numpy_matrix(G)

        eg.info("Setup completed")
        start_time = time.time()

        if algo == "1k":
            H = nx.random_degree_sequence_graph((nx.degree(G).values()))
            # B = nx.to_numpy_matrix(H)

        elif algo == "2k":
            joint_degrees = nx.algorithms.mixing.degree_mixing_dict(G)
            H = joint_degree_graph(joint_degrees)
            # B = nx.to_numpy_matrix(H)

        elif algo == "eig":
            precision = float(param)
            # B = eg.build_matrix(x, l, precision)
            x, l = eg.eigen_centrality(A)
            B = eg.generate_matrix(x, l * x, precision, gu.get_degrees(A))
            H = None
            algo += str(precision)

        elif algo == "modeig":
            precision = float(param)
            B = eg.synthetic_modularity_matrix(A, precision)
            H = None
            algo += str(precision)

        elif algo == "spectre":
            m = float(param)
            n = nm.shape(A)[0]
            B = eg.sample_simm_matrix2(A, int(round(n * m)))
            H = gu.simm_matrix_2_graph(B)
            while nx.is_isomorphic(G, H):
                B = eg.sample_simm_matrix2(A, int(round(n * m)))
                H = gu.simm_matrix_2_graph(B)
            algo += str(m)

        elif algo == "laplacian":
            m = float(param)
            n = nm.shape(A)[0]
            B = eg.laplacian_clone_matrix(A, int(round(n * m)))
            H = gu.simm_matrix_2_graph(B)
            while nx.is_isomorphic(G, H):
                B = eg.sample_simm_matrix2(A, int(round(n * m)))
                H = gu.simm_matrix_2_graph(B)
            algo += str(m)

        elif algo == "modspec":
            m = float(param)
            n = nm.shape(A)[0]
            B = eg.modspec_clone_matrix(A, int(round(n * m)))
            H = None
            algo += str(m)

        elif algo == "franky":
            m = float(param)
            n = nm.shape(A)[0]
            B = eg.franky_clone_matrix(A, int(round(n * m)))
            H = None
            algo += str(m)

        elif algo == "modularity":
            m = float(param)
            n = nm.shape(A)[0]
            B = eg.modularity_clone_matrix(A, int(round(n * m)))
            H = gu.simm_matrix_2_graph(B)
            while nx.is_isomorphic(G, H):
                B = eg.modularity_clone_matrix(A, int(round(n * m)))
                H = gu.simm_matrix_2_graph(B)
            algo += str(m)

        elif algo == "25k":
            test25 = Estimation()
            gen25 = Generation()
            test25.load_graph("", graph=G)
            test25.calcfull_CCK()
            test25.calcfull_JDD()
            gen25.set_JDD(test25.get_JDD('full'))
            gen25.set_KTRI(test25.get_KTRI('full'))
            gen25.construct_triangles_2K()
            gen25.mcmc_improved_2_5_K(error_threshold=0.05)
            H = gen25.G
            # B = nx.to_numpy_matrix(H)

        eg.info("Graph Generated")

        stat = get_statistics1(G, H, time.time() - start_time)
        s = algo + "," + name + "," + str(len(G.nodes()))
        for el in stat:
            s += "," + str(el)
        print_queue.put(s)
        print_queue.put("\n")
        gc.collect()
Beispiel #10
0
def graph_worker(inputlist, queue, print_queue):
    for filo in inputlist:
        if filo.split(".")[-1] == "graphml":
            G = read_graphml(filo)
        else:
            G = nx.read_weighted_edgelist(filo)

        A = nx.to_numpy_matrix(G)
        n = nm.shape(A)[0]
        joint_degrees = nx.algorithms.mixing.degree_mixing_dict(G)
        x, l = eg.eigen_centrality(A)

        H = nx.random_degree_sequence_graph((nx.degree(G).values()))
        B = nx.to_numpy_matrix(H)
        print_queue.put(write_statistics(A, B, "1k", filo, x, l, output=False))
        print_queue.put("\n")

        H = joint_degree_graph(joint_degrees)
        B = nx.to_numpy_matrix(H)
        print_queue.put(write_statistics(A, B, "2k", filo, x, l, output=False))
        print_queue.put("\n")

        precision = 0.01
        B = eg.build_matrix(x, l, precision)
        print_queue.put(
            write_statistics(A,
                             B,
                             "eig" + str(precision),
                             filo,
                             x,
                             l,
                             output=False))
        print_queue.put("\n")

        precision = 0.001
        B = eg.build_matrix(x, l, precision)
        print_queue.put(
            write_statistics(A,
                             B,
                             "eig" + str(precision),
                             filo,
                             x,
                             l,
                             output=False))
        print_queue.put("\n")

        precision = 0.0001
        B = eg.build_matrix(x, l, precision)
        print_queue.put(
            write_statistics(A,
                             B,
                             "eig" + str(precision),
                             filo,
                             x,
                             l,
                             output=False))
        print_queue.put("\n")

        m = 0.25
        B = eg.sample_simm_matrix(A, int(round(n * m)))
        print_queue.put(
            write_statistics(A,
                             B,
                             "spectre" + str(m),
                             filo,
                             x,
                             l,
                             output=False))
        print_queue.put("\n")

        m = 0.5
        B = eg.sample_simm_matrix(A, int(round(n * m)))
        print_queue.put(
            write_statistics(A,
                             B,
                             "spectre" + str(m),
                             filo,
                             x,
                             l,
                             output=False))
        print_queue.put("\n")

        m = 0.75
        B = eg.sample_simm_matrix(A, int(round(n * m)))
        print_queue.put(
            write_statistics(A,
                             B,
                             "spectre" + str(m),
                             filo,
                             x,
                             l,
                             output=False))
        print_queue.put("\n")

        m = 0.9
        B = eg.sample_simm_matrix(A, int(round(n * m)))
        print_queue.put(
            write_statistics(A,
                             B,
                             "spectre" + str(m),
                             filo,
                             x,
                             l,
                             output=False))
        print_queue.put("\n")

        m = 0.95
        B = eg.sample_simm_matrix(A, int(round(n * m)))
        print_queue.put(
            write_statistics(A,
                             B,
                             "spectre" + str(m),
                             filo,
                             x,
                             l,
                             output=False))
        print_queue.put("\n")

        test25 = Estimation()
        gen25 = Generation()
        test25.load_graph("", graph=G)
        test25.calcfull_CCK()
        test25.calcfull_JDD()
        gen25.set_JDD(test25.get_JDD('full'))
        gen25.set_KTRI(test25.get_KTRI('full'))
        gen25.construct_triangles_2K()
        gen25.mcmc_improved_2_5_K(error_threshold=0.05)
        H = gen25.G
        B = nx.to_numpy_matrix(H)
        print_queue.put(write_statistics(A, B, "25k", filo, x, l,
                                         output=False))
        print_queue.put("\n")
    def main():

        average_incomes = []

        populations = []

        # 用于判断全局收敛的方差最小值,小于该值时认为种群达到全局收敛
        std_limition = 0.1

        currentAdjustGeneration = 41

        Standard.init_data()
        population = Standard.initialPopulation

        generation = 0

        time3 = datetime.now()

        # 十万代以内,不停迭代,直到某一代出现完全收敛
        while generation < Standard.maxgeneration:

            populations.append(population)

            print("第")
            print(generation)
            print("代")

            if (generation == 40):
                Standard.retainEliteNumbers = 1

            print("本代精英个数:", Standard.retainEliteNumbers)
            print("本代变异率:", Standard.mutationprob)

            # 每相隔30代 加一次精英
            if (generation - currentAdjustGeneration > 30
                    and Standard.retainEliteNumbers < 5):
                print("下面加精英")
                Standard.retainEliteNumbers += 1
                currentAdjustGeneration = generation

            # 对种群解码得到表现形
            decode = Generation.getDecode(population, Standard)

            # 得到适应度值和累计概率值
            fitnessValue, cum_proba, incomes, retain_population, elite_population, elite_incomes, generation_best_chrome = Generation.getFitnessValue(
                population, decode, Standard)

            # 选择新的种群,并计算平均收益
            newpopulations, average_income = Generation.selectNewPopulation(
                retain_population, population, cum_proba, fitnessValue,
                incomes, elite_population, elite_incomes, Standard)

            print("本代平均收益:", average_income)
            average_incomes.append(average_income)

            # 如果本代方差小于一定值,则认为达到了全局收敛,迭代终止
            if (Standard.stdarr[generation] < std_limition):
                break

            # 新种群交叉
            crossPopulations = Generation.crossNewPopulation(
                newpopulations, Standard.prob)

            # 变异操作
            mutationpopulation = Generation.mutation(crossPopulations,
                                                     Standard.mutationprob)

            # 将父母和子女合并为新的种群
            population = np.vstack((newpopulations, mutationpopulation))

            generation += 1

            print("本代演化结束,当前平均收益数组:", average_incomes)

            time4 = datetime.now()
            print("本代耗时:", time4 - time3)

            time3 = time4

        # 获取最后要输出的平均收益和各项参数
        final_average_income = average_income
        best_chrome = generation_best_chrome
        """
        print(average_incomes)
        x = [i for i in range(generation)]
        y = [average_incomes[i] for i in range(generation)]
        plt.plot(x, y)
        plt.show()
        """
        # 返回最终平均收益,以及最后一代的最优染色体

        best_chrome_dealed = best_chrome[0]

        for index, item in enumerate(best_chrome_dealed):
            if (index in Standard.intgerIndex):
                best_chrome_dealed[index] = np.ceil(item)

        return final_average_income, best_chrome_dealed, generation
Beispiel #12
0
    #fname = "web-NotreDame.edges.gz"
    #fname = "web-Google.edges.gz"
    #fname = "wiki-Talk.edges.gz"
    run_case = 1
    error_threshold = 0.05

    ###### Full graph - 2K with triangles + Improved MCMC
    if run_case == 1:
        myest = Estimation()
        myest.load_graph(fname)

        myest.calcfull_CCK()
        myest.calcfull_JDD()
        myest.estimation_summary()

        mygen = Generation()
        mygen.set_JDD(myest.get_JDD('full'))
        mygen.set_KTRI(myest.get_KTRI('full'))

        mygen.construct_triangles_2K()
        mygen.mcmc_improved_2_5_K(error_threshold=error_threshold)
        mygen.save_graphs('%s_2KT+ImpMCMC_Full' % fname)
    #######################################################
    ###### Full graph - 2K simple +  MCMC
    elif run_case == 2:
        myest = Estimation()
        myest.load_graph(fname)

        myest.calcfull_CCK()
        myest.calcfull_JDD()
        myest.estimation_summary()
Beispiel #13
0
 def __init__(self, neuro_options):
     self.gens = []
     self.neuro_options = neuro_options
     self.current_generation = Generation(self.neuro_options)