genetic algorithm #35
116
main.py
116
main.py
@ -1,6 +1,5 @@
|
||||
import math
|
||||
import random
|
||||
import time
|
||||
import pygame
|
||||
import torch
|
||||
import agent
|
||||
@ -11,6 +10,7 @@ import neural_network
|
||||
import settings
|
||||
import tree
|
||||
import shutup
|
||||
import genetical_algorithm
|
||||
|
||||
possibleFields = {
|
||||
'dirt': field.Dirt(),
|
||||
@ -63,7 +63,6 @@ def randomize_map():
|
||||
height = settings.Field.vertical_count()
|
||||
for i in range(width):
|
||||
for j in range(height):
|
||||
# k = random.choice(list(possibleFields.keys()))
|
||||
x = random.uniform(0, 100)
|
||||
if x <= 80:
|
||||
plant = random.choice(possibleFieldsWithPlants)
|
||||
@ -120,14 +119,14 @@ def get_plants_array(fields):
|
||||
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')
|
||||
model = neural_network.Net(num_classes=3)
|
||||
model.load_state_dict(checkpoint)
|
||||
model.eval()
|
||||
img = ''
|
||||
b=0
|
||||
j=0
|
||||
b = 0
|
||||
j = 0
|
||||
field_array_small = []
|
||||
field_array_big = []
|
||||
for i in range(11):
|
||||
@ -149,12 +148,12 @@ def recognize_plants(plants_array, fields_for_astar, fields_for_movement, agent)
|
||||
else:
|
||||
pred = 'none'
|
||||
field_array_small.append(pred)
|
||||
print(i,',', j,'-',pred)
|
||||
print(i, ',', j, '-', pred)
|
||||
agent_movement(['f'], agent, fields_for_movement, fields_for_astar)
|
||||
agent_movement(['r','f','r'], agent, fields_for_movement, fields_for_astar)
|
||||
agent_movement(['r', 'f', 'r'], agent, fields_for_movement, fields_for_astar)
|
||||
field_array_big.append(field_array_small)
|
||||
else:
|
||||
for j in range(10,-1,-1):
|
||||
for j in range(10, -1, -1):
|
||||
if plants_array[j][i] == 'carrot':
|
||||
img = 'assets/learning/test/carrot/' + str(random.randint(1, 200)) + '.jpg'
|
||||
pred = neural_network.prediction(img, model)
|
||||
@ -170,28 +169,54 @@ def recognize_plants(plants_array, fields_for_astar, fields_for_movement, agent)
|
||||
else:
|
||||
pred = 'none'
|
||||
field_array_small.append(pred)
|
||||
print(i,',', j,'-',pred)
|
||||
print(i, ',', j, '-', pred)
|
||||
agent_movement(['f'], agent, fields_for_movement, fields_for_astar)
|
||||
field_array_small = field_array_small[::-1]
|
||||
field_array_big.append(field_array_small)
|
||||
agent_movement(['l','f','l'], agent, fields_for_movement, fields_for_astar)
|
||||
if b==0:
|
||||
b=1
|
||||
agent_movement(['l', 'f', 'l'], agent, fields_for_movement, fields_for_astar)
|
||||
if b == 0:
|
||||
b = 1
|
||||
else:
|
||||
b=0
|
||||
b = 0
|
||||
correct = 0
|
||||
incorrect = 0
|
||||
for i in range(11):
|
||||
for j in range(11):
|
||||
if plants_array[i][j]=='none':
|
||||
if plants_array[i][j] == 'none':
|
||||
continue
|
||||
else:
|
||||
if plants_array[i][j]==field_array_big[j][i]:
|
||||
correct+=1
|
||||
if plants_array[i][j] == field_array_big[j][i]:
|
||||
correct += 1
|
||||
else:
|
||||
incorrect+=1
|
||||
print("Accuracy: ",correct/(correct+incorrect)*100,'%')
|
||||
|
||||
incorrect += 1
|
||||
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):
|
||||
@ -212,13 +237,15 @@ def read_img(agent, fields):
|
||||
pygame.time.delay(2000)
|
||||
common.set('state_imgShown', False)
|
||||
|
||||
|
||||
def show_plant_img(img):
|
||||
window = common.get('window')
|
||||
image = pygame.image.load(img)
|
||||
image = pygame.transform.scale(image, (11*settings.Field.size(), 11*settings.Field.size()))
|
||||
window.blit(image,(0,0))
|
||||
image = pygame.transform.scale(image, (11 * settings.Field.size(), 11 * settings.Field.size()))
|
||||
window.blit(image, (0, 0))
|
||||
pygame.display.update()
|
||||
pygame.time.delay(2000)
|
||||
pygame.time.delay(1000)
|
||||
|
||||
|
||||
def agent_action(action: str):
|
||||
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()
|
||||
height = settings.Field.vertical_count()
|
||||
k = 0
|
||||
visited = 0
|
||||
for i in range(width):
|
||||
for j in range(height):
|
||||
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.succ))
|
||||
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
|
||||
visited+=1
|
||||
fields_to_sow.append(field_to_visit)
|
||||
fields_with_plants[x1][y1] = 'wheat_empty'
|
||||
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.succ))
|
||||
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
|
||||
visited += 1
|
||||
fields_to_sow.append(field_to_visit)
|
||||
fields_with_plants[x1][y1] = 'carrot_empty'
|
||||
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.succ))
|
||||
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
|
||||
visited += 1
|
||||
fields_to_sow.append(field_to_visit)
|
||||
fields_with_plants[x1][y1] = 'potato_empty'
|
||||
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.succ))
|
||||
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
|
||||
visited += 1
|
||||
fields_to_feritize.append(field_to_visit)
|
||||
fields_with_plants[x1][y1] = 'wheat_watered'
|
||||
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.succ))
|
||||
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
|
||||
visited += 1
|
||||
fields_to_feritize.append(field_to_visit)
|
||||
fields_with_plants[x1][y1] = 'carrot_watered'
|
||||
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.succ))
|
||||
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
|
||||
visited += 1
|
||||
fields_to_feritize.append(field_to_visit)
|
||||
fields_with_plants[x1][y1] = 'potato_watered'
|
||||
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.succ))
|
||||
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
|
||||
visited += 1
|
||||
fields_to_harvest.append(field_to_visit)
|
||||
fields_with_plants[x1][y1] = 'wheat_feritized'
|
||||
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.succ))
|
||||
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
|
||||
visited += 1
|
||||
fields_to_harvest.append(field_to_visit)
|
||||
fields_with_plants[x1][y1] = 'carrot_feritized'
|
||||
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.succ))
|
||||
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
|
||||
visited += 1
|
||||
fields_to_harvest.append(field_to_visit)
|
||||
fields_with_plants[x1][y1] = 'potato_feritized'
|
||||
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.succ))
|
||||
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
|
||||
visited += 1
|
||||
fields_to_water.append(field_to_visit)
|
||||
fields_with_plants[x1][y1] = 'wheat_sow'
|
||||
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.succ))
|
||||
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
|
||||
visited += 1
|
||||
fields_to_water.append(field_to_visit)
|
||||
fields_with_plants[x1][y1] = 'carrot_sow'
|
||||
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.succ))
|
||||
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
|
||||
visited += 1
|
||||
fields_to_water.append(field_to_visit)
|
||||
fields_with_plants[x1][y1] = 'potato_sow'
|
||||
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()
|
||||
print("fuel:", fuel, "water:", water, "feritizer:", feritizer, "seeds:", seeds, "carrots:", carrots,
|
||||
"potatoes:", potatoes, "wheat:", wheat)
|
||||
agent.set_tank_capacity(1500)
|
||||
agent.set_water(50)
|
||||
agent.set_feritizer(50)
|
||||
agent.set_seeds(50)
|
||||
print("Visited fields: "+str(visited))
|
||||
visited=0
|
||||
if k < 4:
|
||||
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_potatoes(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):
|
||||
@ -525,12 +570,21 @@ def agent_movement(move_list, agent, fields, fields_2):
|
||||
fuel -= 10
|
||||
agent.set_tank_capacity(fuel)
|
||||
draw_window(agent, fields)
|
||||
time.sleep(0.5)
|
||||
pygame.time.delay(300)
|
||||
|
||||
|
||||
common = common.Instance()
|
||||
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():
|
||||
common.set('game_running', True)
|
||||
@ -545,7 +599,8 @@ def main():
|
||||
fields, fields_2, fields_3 = randomize_map()
|
||||
plants_array = get_plants_array(fields_3)
|
||||
# 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
|
||||
t = tree.treelearn()
|
||||
while common.get('game_running'):
|
||||
@ -560,7 +615,6 @@ def main():
|
||||
if x:
|
||||
generate_movement(fields_2, fields_3, fields, agent, t)
|
||||
x = False
|
||||
|
||||
agent_action(agent.rotate(None))
|
||||
agent_action(agent.move(None))
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user