Exemple #1
0
    def search(self):
        start = time.process_time()
        count = 1
        while True:
            neighbor_states = self.action(self.current_state)

            if len(neighbor_states) < 1:
                print('Restart len 0')
                self.current_state = self.env.random_start_state()
                neighbor_states = self.action(self.current_state)

            highest_state = get_neighbor_highest(neighbor_states)
            highest_state.h = cal_heuristic(highest_state)
            self.current_state.h = cal_heuristic(self.current_state)
            if highest_state.h > self.current_state.h or self.goal(
                    self.current_state):
                if self.current_state.h == 0:
                    self.current_state.show()
                    cal_heuristic(self.current_state)
                if self.goal(self.current_state):
                    duration = time.process_time() - start
                    print(duration)
                    return self.current_state
                else:
                    print('Restart stuck')
                    self.current_state = self.env.random_start_state()
            else:
                self.current_state = highest_state
                self.current_state.h = cal_heuristic(self.current_state)

            print('---------------------')
            print('Time %s' % count)
            print('H = %s' % self.current_state.h)
            count += 1
Exemple #2
0
def get_neighbor_highest(states):
    state = states.pop()
    min = cal_heuristic(state)

    for other in states:
        if cal_heuristic(other) < min:
            min = cal_heuristic(other)
            state = other

    return state
Exemple #3
0
def reproduce(father, mother):
    child_h = min([father.h, mother.h])
    child = State()
    for i in range(5):
        child = State()
        c = random.randint(0, size - 1)

        for column in range(c):
            for row in range(size):
                if father.map[row][column] == 1:
                    child.map[row][column] = 1
                    break

        for column in range(c, size):
            for row in range(size):
                if mother.map[row][column] == 1:
                    child.map[row][column] = 1
                    break

        child.h = cal_heuristic(child)

        if child.h < child_h:
            break

    return child
Exemple #4
0
def random_selection(population):
    fitnessList = []

    for state in population:
        fitnessList.append(nCk(size, 2) - cal_heuristic(state))

    probaList = []
    total = sum(fitnessList)
    first = 0
    second = 0

    for item in fitnessList:
        probaList.append(item / total)

    for i in range(len(probaList)):
        if i > 0:
            probaList[i] += probaList[i - 1]

    random_value = random.uniform(0, 1)

    for i in range(len(probaList)):
        if random_value < probaList[i]:
            first = i
            break

    random_value = random.uniform(0, 1)

    for i in range(len(probaList)):
        if random_value < probaList[i]:
            second = i
            break

    return population[first], population[second]
Exemple #5
0
    def search(self):
        count = 1
        print(nCk(size, 2))
        start = time.process_time()

        while not self.is_best_individual():
            fitness = ''
            new_states = []
            avg = 0
            print('Time %s' % count)
            for i in range(self.state_size):
                father, mother = random_selection(self.states)

                child = reproduce(father, mother)
                if random.uniform(0, 1) < 0.3:
                    child = mutate(child)
                new_states.append(child)
                child.h = cal_heuristic(child)
                avg += child.h
                fitness += str(child.h) + ' '

            avg = avg / self.state_size
            list_childs = []
            for item in new_states:
                if item.h <= avg:
                    list_childs.append(item)

            print(fitness)
            self.states = list_childs
            count += 1

        duration = time.process_time() - start
        print(duration)
        for state in self.states:
            if cal_heuristic(state) == 0:
                return state

        return None
Exemple #6
0
    def is_best_individual(self):
        for state in self.states:
            if cal_heuristic(state) == 0:
                return True

        return False
Exemple #7
0
 def __init__(self, env):
     self.current_state = env.current_state
     self.current_state.h = cal_heuristic(self.current_state)
     self.goal = env.goal
     self.action = env.action
     self.env = env