Compare commits

..

10 Commits

2 changed files with 282 additions and 32 deletions

196
genetical_algorithm.py Normal file
View File

@ -0,0 +1,196 @@
import random
from copy import copy
import settings
import tree
def evaluate_values(fuel, water, feritizer, seeds, fields_with_plants, k):
fields_to_sow = 0
fields_to_water = 0
fields_to_feritize = 0
fields_to_harvest = 0
width = settings.Field.horizontal_count()
height = settings.Field.vertical_count()
ans = 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 \
fields_with_plants[i][j] == 'wheat_empty':
fields_to_sow += 1
elif fields_with_plants[i][j] == 'potato_sow' or fields_with_plants[i][j] == 'carrot_sow' or \
fields_with_plants[i][j] == 'wheat_sow':
fields_to_water += 1
elif fields_with_plants[i][j] == 'potato_watered' or fields_with_plants[i][j] == 'carrot_watered' or \
fields_with_plants[i][j] == 'wheat_watered':
fields_to_feritize += 1
elif fields_with_plants[i][j] == 'potato_feritized' or fields_with_plants[i][j] == 'carrot_feritized' or \
fields_with_plants[i][j] == 'wheat_feritized':
fields_to_harvest += 1
t = tree.treelearn()
if water < fields_to_water or feritizer < fields_to_feritize or seeds < fields_to_sow or fuel < 1000:
return 5
if tree.make_decision(t, fuel, water, feritizer, 0, 0, 0, 0, 0, seeds):
return 5
if fuel > 2000:
return 0
if k == 0:
ans += (fuel) / 30 - fields_to_harvest
ans += water - fields_to_water
ans += feritizer - fields_to_feritize - fields_to_water
ans += seeds - fields_to_harvest - fields_to_sow
fuel = fuel - 1500
water = water - fields_to_water
feritizer = feritizer - fields_to_feritize - fields_to_water
seeds = seeds - fields_to_harvest - fields_to_sow
if water < 0:
return 10
if feritizer < 0:
return 10
if seeds < 0:
return 10
if fuel < 0:
return 10
if k == 1:
ans += water - fields_to_water
ans += feritizer - fields_to_water - fields_to_feritize
ans += seeds - fields_to_sow
ans += (fuel - 1100) / 30 - fields_to_harvest
fuel = fuel - 1500
water = water - fields_to_water
feritizer = feritizer - fields_to_water - fields_to_feritize
seeds = seeds - fields_to_sow
if water < 0:
return 10
if feritizer < 0:
return 10
if seeds < 0:
return 10
if fuel < 0:
return 10
if k == 2:
ans += feritizer - fields_to_feritize
ans += seeds - fields_to_sow
ans += (fuel - 800) / 15 - fields_to_feritize - fields_to_harvest
ans += water - fields_to_water - fields_to_sow
feritizer = feritizer - fields_to_feritize
seeds = seeds - fields_to_sow
fuel -= 1500
water = water - fields_to_water - fields_to_sow
if water < 0:
return 10
if feritizer < 0:
return 10
if seeds < 0:
return 10
if fuel < 0:
return 10
if k == 3:
ans += seeds - fields_to_sow
ans += (fuel - 400) / 30 - fields_to_harvest
ans += water - fields_to_water - fields_to_sow
ans += feritizer - fields_to_feritize - fields_to_water - fields_to_sow
fuel -= 1500
seeds = seeds - fields_to_sow
water = water - fields_to_water - fields_to_sow
feritizer = feritizer - fields_to_feritize - fields_to_water - fields_to_sow
if water < 0:
return 10
if feritizer < 0:
return 10
if seeds < 0:
return 10
if fuel < 0:
return 10
if tree.make_decision(t, fuel, water, feritizer, 15, 15, 15, 10, 10, seeds):
return 15
if k == 0:
fuel = fuel-500
if tree.make_decision(t, fuel, water, feritizer, 15, 15, 15, 10, 10, seeds):
return ans
else:
ans += 50
water = water - fields_to_harvest - fields_to_sow-10
if k == 1:
water = water - fields_to_harvest - fields_to_sow-10
if tree.make_decision(t, fuel - 400, water, feritizer, 15, 15, 15, 10, 10, seeds):
return ans
else:
ans += 50
if k == 2:
feritizer = feritizer - fields_to_sow - fields_to_water-10
if tree.make_decision(t, fuel - 400, water, feritizer, 15, 15, 15, 10, 10, seeds):
return ans
else:
ans += 50
if k == 3:
seeds = seeds - fields_to_water - fields_to_feritize-10
if tree.make_decision(t, fuel - 400, water, feritizer, 15, 15, 15, 10, 10, seeds):
return ans
else:
ans += 50
return ans
def fitness(solution, fields_plants, k):
fuel = solution[0]
water = solution[1]
feritizer = solution[2]
seeds = solution[3]
if fuel / 30 + water + feritizer + seeds > 200:
ans = 0
else:
ans = evaluate_values(fuel, water, feritizer, seeds, fields_plants, k)
return ans
def solution(fields_with_plants, l):
solutions = []
ranked_solutions = []
for i in range(10000):
solution = []
fuel = random.randint(0, 2000)
water = random.randint(0, 100)
feritizer = random.randint(0, 100)
seeds = random.randint(0, 100)
solution.append(fuel)
solution.append(water)
solution.append(feritizer)
solution.append(seeds)
solutions.append(solution)
for s in solutions:
ranked_solutions.append((fitness(s, fields_with_plants, l), s))
ranked_solutions.sort()
ranked_solutions.reverse()
best_solutions = ranked_solutions[:100]
k = 1
print("Gen 1 best solution: " + str(ranked_solutions[0]))
for q in range(4):
k += 1
random_cross = random.randint(0, 3)
random_mutation_place = random.randint(0, 3)
random_mutation = random.randint(1, 5)
ranked_solutions = []
solutions = []
solutions.append(best_solutions[0][1])
for i in range(100):
for j in range(100):
if i == j:
continue
random_mutation_chance = random.randint(1, 100)
solution = copy(best_solutions[i][1])
solution[random_cross:4] = best_solutions[j][1][random_cross:4]
if random_mutation_chance <= 3:
if random_mutation_place == 0:
solution[random_mutation_place] = solution[random_mutation_place] + random_mutation * 30
else:
solution[random_mutation_place] = solution[random_mutation_place] + random_mutation
solutions.append(copy(solution))
for s in solutions:
ranked_solutions.append((fitness(s, fields_with_plants, l), s))
ranked_solutions.sort()
ranked_solutions.reverse()
best_solutions = ranked_solutions[:100]
print("Gen " + str(k) + " best solution: " + str(ranked_solutions[0]))
return ranked_solutions[0][1][0], ranked_solutions[0][1][1], ranked_solutions[0][1][2], ranked_solutions[0][1][3]

116
main.py
View File

@ -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,14 +119,14 @@ 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)
model.eval() model.eval()
img = '' img = ''
b=0 b = 0
j=0 j = 0
field_array_small = [] field_array_small = []
field_array_big = [] field_array_big = []
for i in range(11): for i in range(11):
@ -149,12 +148,12 @@ def recognize_plants(plants_array, fields_for_astar, fields_for_movement, agent)
else: else:
pred = 'none' pred = 'none'
field_array_small.append(pred) 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(['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) field_array_big.append(field_array_small)
else: else:
for j in range(10,-1,-1): for j in range(10, -1, -1):
if plants_array[j][i] == 'carrot': if plants_array[j][i] == 'carrot':
img = 'assets/learning/test/carrot/' + str(random.randint(1, 200)) + '.jpg' img = 'assets/learning/test/carrot/' + str(random.randint(1, 200)) + '.jpg'
pred = neural_network.prediction(img, model) pred = neural_network.prediction(img, model)
@ -170,28 +169,54 @@ def recognize_plants(plants_array, fields_for_astar, fields_for_movement, agent)
else: else:
pred = 'none' pred = 'none'
field_array_small.append(pred) 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(['f'], agent, fields_for_movement, fields_for_astar)
field_array_small = field_array_small[::-1] field_array_small = field_array_small[::-1]
field_array_big.append(field_array_small) field_array_big.append(field_array_small)
agent_movement(['l','f','l'], agent, fields_for_movement, fields_for_astar) agent_movement(['l', 'f', 'l'], agent, fields_for_movement, fields_for_astar)
if b==0: if b == 0:
b=1 b = 1
else: else:
b=0 b = 0
correct = 0 correct = 0
incorrect = 0 incorrect = 0
for i in range(11): for i in range(11):
for j in range(11): for j in range(11):
if plants_array[i][j]=='none': if plants_array[i][j] == 'none':
continue continue
else: else:
if plants_array[i][j]==field_array_big[j][i]: if plants_array[i][j] == field_array_big[j][i]:
correct+=1 correct += 1
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))