genetic algorithm #35
82
main.py
82
main.py
@ -1,6 +1,5 @@
|
|||||||
import math
|
import math
|
||||||
import random
|
import random
|
||||||
import time
|
|
||||||
import pygame
|
import pygame
|
||||||
import torch
|
import torch
|
||||||
import agent
|
import agent
|
||||||
@ -11,6 +10,7 @@ import neural_network
|
|||||||
import settings
|
import settings
|
||||||
import tree
|
import tree
|
||||||
import shutup
|
import shutup
|
||||||
|
import genetical_algorithm
|
||||||
|
|
||||||
possibleFields = {
|
possibleFields = {
|
||||||
'dirt': field.Dirt(),
|
'dirt': field.Dirt(),
|
||||||
@ -63,7 +63,6 @@ def randomize_map():
|
|||||||
height = settings.Field.vertical_count()
|
height = settings.Field.vertical_count()
|
||||||
for i in range(width):
|
for i in range(width):
|
||||||
for j in range(height):
|
for j in range(height):
|
||||||
# k = random.choice(list(possibleFields.keys()))
|
|
||||||
x = random.uniform(0, 100)
|
x = random.uniform(0, 100)
|
||||||
if x <= 80:
|
if x <= 80:
|
||||||
plant = random.choice(possibleFieldsWithPlants)
|
plant = random.choice(possibleFieldsWithPlants)
|
||||||
@ -120,7 +119,7 @@ def get_plants_array(fields):
|
|||||||
return field_array_big
|
return field_array_big
|
||||||
|
|
||||||
|
|
||||||
def recognize_plants(plants_array, fields_for_astar, fields_for_movement, agent):
|
def recognize_plants(plants_array, fields_for_astar, fields_for_movement, agent,fields_with_plants):
|
||||||
checkpoint = torch.load(f'plants.model')
|
checkpoint = torch.load(f'plants.model')
|
||||||
model = neural_network.Net(num_classes=3)
|
model = neural_network.Net(num_classes=3)
|
||||||
model.load_state_dict(checkpoint)
|
model.load_state_dict(checkpoint)
|
||||||
@ -191,7 +190,33 @@ def recognize_plants(plants_array, fields_for_astar, fields_for_movement, agent)
|
|||||||
else:
|
else:
|
||||||
incorrect += 1
|
incorrect += 1
|
||||||
print("Accuracy: ", correct / (correct + incorrect) * 100, '%')
|
print("Accuracy: ", correct / (correct + incorrect) * 100, '%')
|
||||||
|
cords = agent.coordinates()
|
||||||
|
x = cords['x']
|
||||||
|
y = cords['y']
|
||||||
|
dir = agent.get_direction()
|
||||||
|
state = astar.State(dir, x, y)
|
||||||
|
move_list = (
|
||||||
|
astar.graphsearch([], astar.f, [], [0, 0], state, fields_for_astar,
|
||||||
|
astar.succ))
|
||||||
|
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
|
||||||
|
draw_window(agent, fields_for_movement)
|
||||||
|
fuel = agent.get_tank_capacity()
|
||||||
|
water = agent.get_water()
|
||||||
|
feritizer = agent.get_feritizer()
|
||||||
|
seeds = agent.get_seeds()
|
||||||
|
carrots = agent.get_carrots()
|
||||||
|
potatoes = agent.get_potatoes()
|
||||||
|
wheat = agent.get_wheat()
|
||||||
|
print("fuel:", fuel, "water:", water, "feritizer:", feritizer, "seeds:", seeds, "carrots:", carrots,
|
||||||
|
"potatoes:", potatoes, "wheat:", wheat)
|
||||||
|
fuel, water, feritizer, seeds = genetical_algorithm.solution(fields_with_plants, 0)
|
||||||
|
agent.set_tank_capacity(fuel)
|
||||||
|
agent.set_water(water)
|
||||||
|
agent.set_feritizer(feritizer)
|
||||||
|
agent.set_seeds(seeds)
|
||||||
|
agent.set_carrots(0)
|
||||||
|
agent.set_potatoes(0)
|
||||||
|
agent.set_wheat(0)
|
||||||
|
|
||||||
|
|
||||||
def read_img(agent, fields):
|
def read_img(agent, fields):
|
||||||
@ -212,13 +237,15 @@ def read_img(agent, fields):
|
|||||||
pygame.time.delay(2000)
|
pygame.time.delay(2000)
|
||||||
common.set('state_imgShown', False)
|
common.set('state_imgShown', False)
|
||||||
|
|
||||||
|
|
||||||
def show_plant_img(img):
|
def show_plant_img(img):
|
||||||
window = common.get('window')
|
window = common.get('window')
|
||||||
image = pygame.image.load(img)
|
image = pygame.image.load(img)
|
||||||
image = pygame.transform.scale(image, (11 * settings.Field.size(), 11 * settings.Field.size()))
|
image = pygame.transform.scale(image, (11 * settings.Field.size(), 11 * settings.Field.size()))
|
||||||
window.blit(image, (0, 0))
|
window.blit(image, (0, 0))
|
||||||
pygame.display.update()
|
pygame.display.update()
|
||||||
pygame.time.delay(2000)
|
pygame.time.delay(1000)
|
||||||
|
|
||||||
|
|
||||||
def agent_action(action: str):
|
def agent_action(action: str):
|
||||||
if action == 'open_window':
|
if action == 'open_window':
|
||||||
@ -258,6 +285,7 @@ def generate_movement(fields_for_astar, fields_with_plants, fields_for_movement,
|
|||||||
width = settings.Field.horizontal_count()
|
width = settings.Field.horizontal_count()
|
||||||
height = settings.Field.vertical_count()
|
height = settings.Field.vertical_count()
|
||||||
k = 0
|
k = 0
|
||||||
|
visited = 0
|
||||||
for i in range(width):
|
for i in range(width):
|
||||||
for j in range(height):
|
for j in range(height):
|
||||||
if fields_with_plants[i][j] == 'potato_empty' or fields_with_plants[i][j] == 'carrot_empty' or \
|
if fields_with_plants[i][j] == 'potato_empty' or fields_with_plants[i][j] == 'carrot_empty' or \
|
||||||
@ -301,6 +329,7 @@ def generate_movement(fields_for_astar, fields_with_plants, fields_for_movement,
|
|||||||
astar.graphsearch([], astar.f, [], [field_to_visit[0], field_to_visit[1]], state, fields_for_astar,
|
astar.graphsearch([], astar.f, [], [field_to_visit[0], field_to_visit[1]], state, fields_for_astar,
|
||||||
astar.succ))
|
astar.succ))
|
||||||
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
|
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
|
||||||
|
visited+=1
|
||||||
fields_to_sow.append(field_to_visit)
|
fields_to_sow.append(field_to_visit)
|
||||||
fields_with_plants[x1][y1] = 'wheat_empty'
|
fields_with_plants[x1][y1] = 'wheat_empty'
|
||||||
fields_for_movement[x1][y1] = possibleFields['wheat_empty'].tile.object
|
fields_for_movement[x1][y1] = possibleFields['wheat_empty'].tile.object
|
||||||
@ -313,6 +342,7 @@ def generate_movement(fields_for_astar, fields_with_plants, fields_for_movement,
|
|||||||
astar.graphsearch([], astar.f, [], [field_to_visit[0], field_to_visit[1]], state, fields_for_astar,
|
astar.graphsearch([], astar.f, [], [field_to_visit[0], field_to_visit[1]], state, fields_for_astar,
|
||||||
astar.succ))
|
astar.succ))
|
||||||
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
|
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
|
||||||
|
visited += 1
|
||||||
fields_to_sow.append(field_to_visit)
|
fields_to_sow.append(field_to_visit)
|
||||||
fields_with_plants[x1][y1] = 'carrot_empty'
|
fields_with_plants[x1][y1] = 'carrot_empty'
|
||||||
fields_for_movement[x1][y1] = possibleFields['carrot_empty'].tile.object
|
fields_for_movement[x1][y1] = possibleFields['carrot_empty'].tile.object
|
||||||
@ -325,6 +355,7 @@ def generate_movement(fields_for_astar, fields_with_plants, fields_for_movement,
|
|||||||
astar.graphsearch([], astar.f, [], [field_to_visit[0], field_to_visit[1]], state, fields_for_astar,
|
astar.graphsearch([], astar.f, [], [field_to_visit[0], field_to_visit[1]], state, fields_for_astar,
|
||||||
astar.succ))
|
astar.succ))
|
||||||
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
|
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
|
||||||
|
visited += 1
|
||||||
fields_to_sow.append(field_to_visit)
|
fields_to_sow.append(field_to_visit)
|
||||||
fields_with_plants[x1][y1] = 'potato_empty'
|
fields_with_plants[x1][y1] = 'potato_empty'
|
||||||
fields_for_movement[x1][y1] = possibleFields['potato_empty'].tile.object
|
fields_for_movement[x1][y1] = possibleFields['potato_empty'].tile.object
|
||||||
@ -343,6 +374,7 @@ def generate_movement(fields_for_astar, fields_with_plants, fields_for_movement,
|
|||||||
astar.graphsearch([], astar.f, [], [field_to_visit[0], field_to_visit[1]], state, fields_for_astar,
|
astar.graphsearch([], astar.f, [], [field_to_visit[0], field_to_visit[1]], state, fields_for_astar,
|
||||||
astar.succ))
|
astar.succ))
|
||||||
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
|
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
|
||||||
|
visited += 1
|
||||||
fields_to_feritize.append(field_to_visit)
|
fields_to_feritize.append(field_to_visit)
|
||||||
fields_with_plants[x1][y1] = 'wheat_watered'
|
fields_with_plants[x1][y1] = 'wheat_watered'
|
||||||
fields_for_movement[x1][y1] = possibleFields['wheat_watered'].tile.object
|
fields_for_movement[x1][y1] = possibleFields['wheat_watered'].tile.object
|
||||||
@ -355,6 +387,7 @@ def generate_movement(fields_for_astar, fields_with_plants, fields_for_movement,
|
|||||||
astar.graphsearch([], astar.f, [], [field_to_visit[0], field_to_visit[1]], state, fields_for_astar,
|
astar.graphsearch([], astar.f, [], [field_to_visit[0], field_to_visit[1]], state, fields_for_astar,
|
||||||
astar.succ))
|
astar.succ))
|
||||||
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
|
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
|
||||||
|
visited += 1
|
||||||
fields_to_feritize.append(field_to_visit)
|
fields_to_feritize.append(field_to_visit)
|
||||||
fields_with_plants[x1][y1] = 'carrot_watered'
|
fields_with_plants[x1][y1] = 'carrot_watered'
|
||||||
fields_for_movement[x1][y1] = possibleFields['carrot_watered'].tile.object
|
fields_for_movement[x1][y1] = possibleFields['carrot_watered'].tile.object
|
||||||
@ -367,6 +400,7 @@ def generate_movement(fields_for_astar, fields_with_plants, fields_for_movement,
|
|||||||
astar.graphsearch([], astar.f, [], [field_to_visit[0], field_to_visit[1]], state, fields_for_astar,
|
astar.graphsearch([], astar.f, [], [field_to_visit[0], field_to_visit[1]], state, fields_for_astar,
|
||||||
astar.succ))
|
astar.succ))
|
||||||
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
|
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
|
||||||
|
visited += 1
|
||||||
fields_to_feritize.append(field_to_visit)
|
fields_to_feritize.append(field_to_visit)
|
||||||
fields_with_plants[x1][y1] = 'potato_watered'
|
fields_with_plants[x1][y1] = 'potato_watered'
|
||||||
fields_for_movement[x1][y1] = possibleFields['potato_watered'].tile.object
|
fields_for_movement[x1][y1] = possibleFields['potato_watered'].tile.object
|
||||||
@ -385,6 +419,7 @@ def generate_movement(fields_for_astar, fields_with_plants, fields_for_movement,
|
|||||||
astar.graphsearch([], astar.f, [], [field_to_visit[0], field_to_visit[1]], state, fields_for_astar,
|
astar.graphsearch([], astar.f, [], [field_to_visit[0], field_to_visit[1]], state, fields_for_astar,
|
||||||
astar.succ))
|
astar.succ))
|
||||||
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
|
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
|
||||||
|
visited += 1
|
||||||
fields_to_harvest.append(field_to_visit)
|
fields_to_harvest.append(field_to_visit)
|
||||||
fields_with_plants[x1][y1] = 'wheat_feritized'
|
fields_with_plants[x1][y1] = 'wheat_feritized'
|
||||||
fields_for_movement[x1][y1] = possibleFields['wheat_feritized'].tile.object
|
fields_for_movement[x1][y1] = possibleFields['wheat_feritized'].tile.object
|
||||||
@ -397,6 +432,7 @@ def generate_movement(fields_for_astar, fields_with_plants, fields_for_movement,
|
|||||||
astar.graphsearch([], astar.f, [], [field_to_visit[0], field_to_visit[1]], state, fields_for_astar,
|
astar.graphsearch([], astar.f, [], [field_to_visit[0], field_to_visit[1]], state, fields_for_astar,
|
||||||
astar.succ))
|
astar.succ))
|
||||||
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
|
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
|
||||||
|
visited += 1
|
||||||
fields_to_harvest.append(field_to_visit)
|
fields_to_harvest.append(field_to_visit)
|
||||||
fields_with_plants[x1][y1] = 'carrot_feritized'
|
fields_with_plants[x1][y1] = 'carrot_feritized'
|
||||||
fields_for_movement[x1][y1] = possibleFields['carrot_feritized'].tile.object
|
fields_for_movement[x1][y1] = possibleFields['carrot_feritized'].tile.object
|
||||||
@ -409,6 +445,7 @@ def generate_movement(fields_for_astar, fields_with_plants, fields_for_movement,
|
|||||||
astar.graphsearch([], astar.f, [], [field_to_visit[0], field_to_visit[1]], state, fields_for_astar,
|
astar.graphsearch([], astar.f, [], [field_to_visit[0], field_to_visit[1]], state, fields_for_astar,
|
||||||
astar.succ))
|
astar.succ))
|
||||||
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
|
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
|
||||||
|
visited += 1
|
||||||
fields_to_harvest.append(field_to_visit)
|
fields_to_harvest.append(field_to_visit)
|
||||||
fields_with_plants[x1][y1] = 'potato_feritized'
|
fields_with_plants[x1][y1] = 'potato_feritized'
|
||||||
fields_for_movement[x1][y1] = possibleFields['potato_feritized'].tile.object
|
fields_for_movement[x1][y1] = possibleFields['potato_feritized'].tile.object
|
||||||
@ -427,6 +464,7 @@ def generate_movement(fields_for_astar, fields_with_plants, fields_for_movement,
|
|||||||
astar.graphsearch([], astar.f, [], [field_to_visit[0], field_to_visit[1]], state, fields_for_astar,
|
astar.graphsearch([], astar.f, [], [field_to_visit[0], field_to_visit[1]], state, fields_for_astar,
|
||||||
astar.succ))
|
astar.succ))
|
||||||
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
|
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
|
||||||
|
visited += 1
|
||||||
fields_to_water.append(field_to_visit)
|
fields_to_water.append(field_to_visit)
|
||||||
fields_with_plants[x1][y1] = 'wheat_sow'
|
fields_with_plants[x1][y1] = 'wheat_sow'
|
||||||
fields_for_movement[x1][y1] = possibleFields['wheat_sow'].tile.object
|
fields_for_movement[x1][y1] = possibleFields['wheat_sow'].tile.object
|
||||||
@ -439,6 +477,7 @@ def generate_movement(fields_for_astar, fields_with_plants, fields_for_movement,
|
|||||||
astar.graphsearch([], astar.f, [], [field_to_visit[0], field_to_visit[1]], state, fields_for_astar,
|
astar.graphsearch([], astar.f, [], [field_to_visit[0], field_to_visit[1]], state, fields_for_astar,
|
||||||
astar.succ))
|
astar.succ))
|
||||||
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
|
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
|
||||||
|
visited += 1
|
||||||
fields_to_water.append(field_to_visit)
|
fields_to_water.append(field_to_visit)
|
||||||
fields_with_plants[x1][y1] = 'carrot_sow'
|
fields_with_plants[x1][y1] = 'carrot_sow'
|
||||||
fields_for_movement[x1][y1] = possibleFields['carrot_sow'].tile.object
|
fields_for_movement[x1][y1] = possibleFields['carrot_sow'].tile.object
|
||||||
@ -451,6 +490,7 @@ def generate_movement(fields_for_astar, fields_with_plants, fields_for_movement,
|
|||||||
astar.graphsearch([], astar.f, [], [field_to_visit[0], field_to_visit[1]], state, fields_for_astar,
|
astar.graphsearch([], astar.f, [], [field_to_visit[0], field_to_visit[1]], state, fields_for_astar,
|
||||||
astar.succ))
|
astar.succ))
|
||||||
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
|
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
|
||||||
|
visited += 1
|
||||||
fields_to_water.append(field_to_visit)
|
fields_to_water.append(field_to_visit)
|
||||||
fields_with_plants[x1][y1] = 'potato_sow'
|
fields_with_plants[x1][y1] = 'potato_sow'
|
||||||
fields_for_movement[x1][y1] = possibleFields['potato_sow'].tile.object
|
fields_for_movement[x1][y1] = possibleFields['potato_sow'].tile.object
|
||||||
@ -476,15 +516,20 @@ def generate_movement(fields_for_astar, fields_with_plants, fields_for_movement,
|
|||||||
wheat = agent.get_wheat()
|
wheat = agent.get_wheat()
|
||||||
print("fuel:", fuel, "water:", water, "feritizer:", feritizer, "seeds:", seeds, "carrots:", carrots,
|
print("fuel:", fuel, "water:", water, "feritizer:", feritizer, "seeds:", seeds, "carrots:", carrots,
|
||||||
"potatoes:", potatoes, "wheat:", wheat)
|
"potatoes:", potatoes, "wheat:", wheat)
|
||||||
agent.set_tank_capacity(1500)
|
print("Visited fields: "+str(visited))
|
||||||
agent.set_water(50)
|
visited=0
|
||||||
agent.set_feritizer(50)
|
if k < 4:
|
||||||
agent.set_seeds(50)
|
k+=1
|
||||||
|
else:
|
||||||
|
k=0
|
||||||
|
fuel,water,feritizer,seeds=genetical_algorithm.solution(fields_with_plants, k)
|
||||||
|
agent.set_tank_capacity(fuel)
|
||||||
|
agent.set_water(water)
|
||||||
|
agent.set_feritizer(feritizer)
|
||||||
|
agent.set_seeds(seeds)
|
||||||
agent.set_carrots(0)
|
agent.set_carrots(0)
|
||||||
agent.set_potatoes(0)
|
agent.set_potatoes(0)
|
||||||
agent.set_wheat(0)
|
agent.set_wheat(0)
|
||||||
print("I'm in base")
|
|
||||||
time.sleep(10)
|
|
||||||
|
|
||||||
|
|
||||||
def get_closest_field(fields_to_visit, x, y, dir, fields_for_astar):
|
def get_closest_field(fields_to_visit, x, y, dir, fields_for_astar):
|
||||||
@ -525,12 +570,21 @@ def agent_movement(move_list, agent, fields, fields_2):
|
|||||||
fuel -= 10
|
fuel -= 10
|
||||||
agent.set_tank_capacity(fuel)
|
agent.set_tank_capacity(fuel)
|
||||||
draw_window(agent, fields)
|
draw_window(agent, fields)
|
||||||
time.sleep(0.5)
|
pygame.time.delay(300)
|
||||||
|
|
||||||
|
|
||||||
common = common.Instance()
|
common = common.Instance()
|
||||||
agent = agent.Instance(1500, 2, 50, 50, 50, 0, 0, 0)
|
agent = agent.Instance(1500, 2, 50, 50, 50, 0, 0, 0)
|
||||||
|
|
||||||
|
def genetical_algorithm_test(fields_3,agent):
|
||||||
|
fuel, water, feritizer, seeds = genetical_algorithm.solution(fields_3, 0)
|
||||||
|
agent.set_tank_capacity(fuel)
|
||||||
|
agent.set_water(water)
|
||||||
|
agent.set_feritizer(feritizer)
|
||||||
|
agent.set_seeds(seeds)
|
||||||
|
agent.set_carrots(0)
|
||||||
|
agent.set_potatoes(0)
|
||||||
|
agent.set_wheat(0)
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
common.set('game_running', True)
|
common.set('game_running', True)
|
||||||
@ -545,7 +599,8 @@ def main():
|
|||||||
fields, fields_2, fields_3 = randomize_map()
|
fields, fields_2, fields_3 = randomize_map()
|
||||||
plants_array = get_plants_array(fields_3)
|
plants_array = get_plants_array(fields_3)
|
||||||
# neural_network.learn()
|
# neural_network.learn()
|
||||||
recognize_plants(plants_array, fields_2, fields,agent)
|
# recognize_plants(plants_array, fields_2, fields, agent,fields_3)
|
||||||
|
genetical_algorithm_test(fields_3,agent)
|
||||||
x = True
|
x = True
|
||||||
t = tree.treelearn()
|
t = tree.treelearn()
|
||||||
while common.get('game_running'):
|
while common.get('game_running'):
|
||||||
@ -560,7 +615,6 @@ def main():
|
|||||||
if x:
|
if x:
|
||||||
generate_movement(fields_2, fields_3, fields, agent, t)
|
generate_movement(fields_2, fields_3, fields, agent, t)
|
||||||
x = False
|
x = False
|
||||||
|
|
||||||
agent_action(agent.rotate(None))
|
agent_action(agent.rotate(None))
|
||||||
agent_action(agent.move(None))
|
agent_action(agent.move(None))
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user