def hillClimberMoveBatteries(env, iterations, chooseConstraint, mutation,
                             moves, coolingSchedule):

    # keep list with scores and upperbound
    costs = []

    # create the array of batteries for model
    hillClimberEnv = env

    # initial hillclimber score after randomizing batteries
    randomizeBatteries(hillClimberEnv.batteries)
    hillClimberModel = runRandom(env)
    firstModel = chooseClimbModel(2, hillClimberModel, 2, 1)
    firstModel.calculateCosts(hillClimberEnv.distanceTable)
    costs.append(firstModel)

    beginTemp = 1000
    endTemp = 1
    iteration = 1

    # keep searching unit stopcriterium is reached
    while iteration <= iterations:

        # calculate current temp (linear, exponential, sigmoidal)
        currentTemp = chooseCoolingSchedule(beginTemp, endTemp, iteration,
                                            iterations, coolingSchedule)

        # make random move for a new state and calculate costs with help from hillclimber
        neighbourEnv = pickRandomNeighbour(hillClimberEnv)
        neighbourModel = runRandom(env)
        neighbourModel = chooseClimbModel(chooseConstraint, neighbourModel,
                                          mutation, moves)
        neighbourModel.calculateCosts(neighbourEnv.distanceTable)

        # calculate difference between new state and previous state
        deltaNodes = neighbourModel.cost - costs[-1].cost

        # accept new state if lower costs or if loss is accepted by chance
        if deltaNodes < 0 or checkNode(iteration, iterations, deltaNodes,
                                       currentTemp):
            costs.append(neighbourModel)
            hillClimberEnv = neighbourEnv

        # append previous state if new state is rejected
        else:
            costs.append(costs[-1])
        iteration += 1

    # make list of costs for plotting purposes
    costsList = []
    for node in costs:
        costsList.append(node.cost)

    return makeHillClimberPakackage(hillClimberEnv, costs[-1], costsList)
示例#2
0
def generateInitialPop(env, popSize):

    # initialize population
    population = []

    # append random individuals to population
    for i in range(0, popSize):
        population.append(runRandom(env))

    return population
示例#3
0
def doRandom(env, choice):
    print("How many times would you like to perform a random sampling?")
    iterations = int(input())
    costs = []
    for i in range(0, iterations):
        model = runRandom(env)
        model.setName("random sampling", i)
        model.printResult()
        costs.append(model.cost)
        visVillage(env, model)

    plotHistMultiple([costs], ["random sampling"], choice, iterations,
                     env.village)
示例#4
0
def menu(choice, env):

    if choice == 1:
        doRandom(env, choice)

    elif choice == 2:
        doDepthFirst(env)

    elif choice == 3:
        model = runRandom(env)
        doHillClimber(env, model)

    elif choice == 4:
        model = runRandom(env)
        doSimAnneal(env, model)

    elif choice == 5:
        doEvolution(env)

    elif choice == 6:
        doKmeans(env)

    elif choice == 7:
        doHillClimberMoveBatteries(env)
示例#5
0
def depthFirstBnB(env):

    # create the array of batteries for model
    modelBatteries = env.createModelBatteries()

    # initialize algorithm model
    depthFirstModel = Model(modelBatteries)

    # initiliaze upperbound at random cost, levels and solution
    model = runRandom(env)
    model.calculateCosts(env.distanceTable)
    upperBound = model.cost
    levels = len(env.houses)
    solution = []

    # create stack with root houseNode
    depthFirstStack = []
    depthFirstStack.append(np.array([0]))

    while len(depthFirstStack) > 0:

        # select last item and pop it
        node = depthFirstStack.pop()

        # create array to put best childnode on top of the stack
        tempCostNodes = []

        # create all children
        lenModelBatteries = len(modelBatteries)
        for i in range(0, lenModelBatteries):

            # create new houseNode
            newNode = np.append(node, modelBatteries[i].idBattery)

            # check for legit solution if all houses are connected
            if (len(newNode) - 1) == levels:
                if checkCapacity(newNode, env.batteries, modelBatteries,
                                 env.houses):
                    newModel = makeModel(newNode, env)

                    # set new upperbound for Branch-n-Bound and add solution to list
                    if newModel.cost < upperBound:
                        upperBound = newModel.cost
                        solution = newNode.tolist()

            # check if there isn't over capacity
            else:
                if checkCapacity(newNode, env.batteries, modelBatteries,
                                 env.houses):
                    newModel = makeModel(newNode, env)

                    # check if current node isn't already higher than upperbound
                    if newModel.cost < upperBound:
                        tempCostNodes.append([
                            env.distanceTable[len(newNode) - 1][newNode[-1]],
                            newNode
                        ])

                else:
                    continue

        # childnode with lowest cost on top of the stack
        tempCostNodes = sorted(tempCostNodes, key=itemgetter(0), reverse=True)
        tempCostNodes = [node[1] for node in tempCostNodes]
        depthFirstStack.extend(tempCostNodes)

    # make depth first model and update battery capacities
    lenSolution = len(solution)
    for i in range(1, lenSolution):
        depthFirstModel.modelBatteries[solution[i] - 1].houses.append(
            env.houses[i - 1])
        depthFirstModel.modelBatteries[solution[i] -
                                       1].curCapacity += env.houses[i - 1].cap

    depthFirstModel.calculateCosts(env.distanceTable)
    return depthFirstModel