예제 #1
0
    print()
    
    if 'y' in shared:
        shared2 = True
    else:
        shared2 = False

    if 'y' in save:
        save2 = True
    else:
        save2 = False

    start2 = time()

    if algo2 == 'hc':
        hc = hillclimber.HillClimber(algo)
        if version is not None:
            hc.run(shared2, save2, version)
        else:
            hc.run(shared2, save2, None)
    else:
        sa = simulatedannealing.SimulatedAnnealing(algo)
        if version is not None:
            sa.run(0.005, 30, shared2, save2, version)
        else:
            sa.run(0.005, 30, shared2, save2, None)

    end2 = time()

    print(f"\nThe second algorithm found a solution in {round(end2 - start2, 3)} seconds.")
예제 #2
0
    # depth = df.DepthFirst(test_graph, transmitters.get_scheme(1)[0:4])
    # depth.run()
    #
    # print(f"Value of the configuration after Depth First: "
    #       f"{depth.graph.calculate_value()}")

    # --------------------------- Breadth First --------------------------------
    # breadth = bf.BreadthFirst(test_graph, transmitters.get_scheme(1)[0:4])
    # breadth.run()
    #
    # print(f"Value of the configuration after Breadth First: "
    #       f"{breadth.graph.calculate_value()}")

    # --------------------------- Hill Climber ---------------------------------
    print("Setting up Hill Climber...")
    climber = hc.HillClimber(random_graph, transmitters.get_scheme(1))

    print("Running Hill Climber...")
    climber.run(2000, verbose=True)

    print(f"Value of the configuration after Hill Climber: "
          f"{climber.graph.calculate_value()}")

    # --------------------------- Simulated Annealing --------------------------
    # It is very difficult to find a good starting temperature for SA. A rule to
    # help you find one is to use the maximum change in score that could happen
    # when mutating your state. In our case, this is 19, because the transmitter
    # maximum difference in score between the most expensive and the cheapest
    # transmitter is 19.

    print("Setting up Simulated Annealing...")
예제 #3
0
        for i in range(helper.repeats):
            if i % 10 == 0:
                print(f"{i}/{helper.repeats}")

            # run the random algorithm once for each iteration
            input_files = Map(helper.stations_data_file,
                              helper.connections_data_file)
            random = rd.Random(input_files, helper.duration,
                               helper.max_num_trajects,
                               helper.total_connections)
            random.run(1)

            # ---------------Hill climber---------------------
            # run the hill climber for each iteration of the random algorithm
            hillclimber = hc.HillClimber(random, input_files,
                                         helper.total_connections)
            hillclimber.run(100)

            # compare the outcomes of the hill climber and save the best outcome
            score_list.append(hillclimber.highscore)
            if hillclimber.highscore > best_score:
                best_score = hillclimber.highscore
                best_traject = hillclimber.hillclimber_solution

        print(f"Highscore: {best_score}, Solution: {best_traject}")

        # create the output files: optimal solution and list of scores, and visualisation
        helper.output(score_list, helper.map_size, helper.user_algorithm,
                      best_traject, best_score)
        vis.visualise(input_files, best_traject, helper.score_csv)