Exemple #1
0
def input_greedy(batteries, houses, command):
    """
    This algorithm chooses randomly a house and connects it to a battery based
    on best (shortest) distance. It asks for number of repeats to run the
    algorithm. It saves all solutions to one .csv file and it writes the
    best price of all repeats to another .csv file. The best price is
    also visualized.
    """

    dist, distdict, lowbprice = distancearr(batteries, houses)
    print("run how many times?")
    repeats = int(input("> "))
    best = Node()
    best.price = 100000
    for i in range(0, repeats):
        greedy(dist, batteries, houses)
        price = price_calc(batteries, distdict)
        if price < best.price:
            best.batts = [[], [], [], [], []]
            best.fillnode(batteries, houses, price)
        write_to_csv(argv[1], command, price)
        reset(batteries, houses)
    savefig = bestplot(argv[1], command, best.price)
    print(f"Best price found: {best.price}")
    if savefig is True:
        nodetoclasses(batteries, houses, best)
        visualize(batteries, houses, argv[1], command, best.price)
Exemple #2
0
def input_kmeans(batteries, houses, command):
    """
    this algorithm takes the desired amount of clusters between 5 and 17 as
    input. The algorithm returns a grid with the desired amount of clusters
    on (local) optimal positions.
    """

    bestprice = 100000
    for k in range(5, 17):
        clusters, connectedhomes = KmeansClusterdistance(houses, batteries, k)
        b, h = clustertoclasses(batteries, houses, clusters, connectedhomes)
        if b is False:
            k -= 1
            continue
        batteries = b
        houses = h
        dist, distdict, lowbprice = distancearr(batteries, houses)
        price = price_calc(batteries, distdict)
        if price < bestprice:
            print(price)
            print(k)
            bestprice = price
            bestbat = batteries
    batteries = bestbat
    visualize(batteries, houses, argv[1], command, bestprice)
Exemple #3
0
def input_hillclimber(batteries, houses, command):
    """
    Iterates over all houses and checks if an improvement can be made by
    switching the connections of 2 houses. It asks for number of repeats to run
    the algorithm. It saves all solutions to one .csv file and it writes the
    best price of all repeats to another .csv file. The best price is
    also visualized.
    """

    dist, distdict, lowbprice = distancearr(batteries, houses)
    print("base: greedy or random")
    base = (input("> ")).lower()
    if base != "greedy" and base != "random":
        return False
    command = base + "_" + command
    print("run how many times?")
    repeats = int(input("> "))
    best = Node()
    best.price = 100000
    for i in range(0, repeats):
        if base == "greedy":
            greedy(dist, batteries, houses)
        elif base == "random":
            random_alg(distdict, batteries, houses)
        hillclimber(dist, distdict, batteries, houses)
        price = price_calc(batteries, distdict)
        if price < best.price:
            best.batts = [[], [], [], [], []]
            best.fillnode(batteries, houses, price)
        write_to_csv(argv[1], command, price)
        reset(batteries, houses)
    savefig = bestplot(argv[1], command, best.price)
    print(f"Best price found: {best.price}")
    nodetoclasses(batteries, houses, best)
    if savefig is True:
        visualize(batteries, houses, argv[1], command, best.price)
        battery_optimization(batteries)
        dist, distdict, lowbprice = distancearr(batteries, houses)
        price = price_calc(batteries, distdict)
        print(f"Best price found with optimize: {price}")
        visualize(batteries, houses, argv[1], command, price)
Exemple #4
0
def input_bfs(batteries, houses, command):
    """
    Only neighbourhood 4 should be tried, because the state space of the other
    neighbourhoods is too big.
    """

    node = Node()
    best = Node()
    dist, distdict, lowbprice = distancearr(batteries, houses)
    best.price = 100000
    best = bfs(node, batteries, houses, distdict, best)
    nodetoclasses(batteries, houses, best)
    price = price_calc(batteries, distdict)
    print(f"Best price found: {price}")
    visualize(batteries, houses, argv[1], command)
Exemple #5
0
def input_randclimber(batteries, houses, command):
    """
    A version of hillclimber which switches 2 random houses until no
    improvements are made for the set number of times. It asks for number of
    repeats to run the algorithm. It saves all solutions to one .csv file and
    it writes the best price of all repeats to another .csv file. The best
    price is also visualized.
    """

    dist, distdict, lowbprice = distancearr(batteries, houses)
    print("base: greedy or random")
    base = (input("> ")).lower()
    if base != "greedy" and base != "random":
        return False
    print("repeat until no change for how many times?")
    repetitions = int(input("> "))
    command = base + "_" + command + str(repetitions)
    print("run how many times?")
    repeats = int(input("> "))
    best = Node()
    best.price = 100000
    for i in range(0, repeats):
        if base == "greedy":
            greedy(dist, batteries, houses)
        elif base == "random":
            random_alg(distdict, batteries, houses)
        randclimber(repetitions, distdict, batteries, houses)
        price = price_calc(batteries, distdict)
        if price < best.price:
            best.batts = [[], [], [], [], []]
            best.fillnode(batteries, houses, price)
        write_to_csv(argv[1], command, price)
        reset(batteries, houses)
    savefig = bestplot(argv[1], command, best.price)
    print(best.price)
    if savefig is True:
        nodetoclasses(batteries, houses, best)
        visualize(batteries, houses, argv[1], command, best.price)