Compare commits

..

No commits in common. "master" and "add_basic_fitness" have entirely different histories.

2 changed files with 33 additions and 220 deletions

View File

@ -1,8 +1,3 @@
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
@ -38,96 +33,21 @@ def evaluate_values(fuel, water, feritizer, seeds, fields_with_plants, k):
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):
@ -141,56 +61,3 @@ def fitness(solution, fields_plants, k):
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]

82
main.py
View File

@ -1,5 +1,6 @@
import math
import random
import time
import pygame
import torch
import agent
@ -10,7 +11,6 @@ import neural_network
import settings
import tree
import shutup
import genetical_algorithm
possibleFields = {
'dirt': field.Dirt(),
@ -63,6 +63,7 @@ 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)
@ -119,7 +120,7 @@ def get_plants_array(fields):
return field_array_big
def recognize_plants(plants_array, fields_for_astar, fields_for_movement, agent,fields_with_plants):
def recognize_plants(plants_array, fields_for_astar, fields_for_movement, agent):
checkpoint = torch.load(f'plants.model')
model = neural_network.Net(num_classes=3)
model.load_state_dict(checkpoint)
@ -190,33 +191,7 @@ def recognize_plants(plants_array, fields_for_astar, fields_for_movement, agent,
else:
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):
@ -237,15 +212,13 @@ 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))
pygame.display.update()
pygame.time.delay(1000)
pygame.time.delay(2000)
def agent_action(action: str):
if action == 'open_window':
@ -285,7 +258,6 @@ 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 \
@ -329,7 +301,6 @@ 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
@ -342,7 +313,6 @@ 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
@ -355,7 +325,6 @@ 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
@ -374,7 +343,6 @@ 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
@ -387,7 +355,6 @@ 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
@ -400,7 +367,6 @@ 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
@ -419,7 +385,6 @@ 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
@ -432,7 +397,6 @@ 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
@ -445,7 +409,6 @@ 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
@ -464,7 +427,6 @@ 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
@ -477,7 +439,6 @@ 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
@ -490,7 +451,6 @@ 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
@ -516,20 +476,15 @@ 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)
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_tank_capacity(1500)
agent.set_water(50)
agent.set_feritizer(50)
agent.set_seeds(50)
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):
@ -570,21 +525,12 @@ def agent_movement(move_list, agent, fields, fields_2):
fuel -= 10
agent.set_tank_capacity(fuel)
draw_window(agent, fields)
pygame.time.delay(300)
time.sleep(0.5)
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)
@ -599,8 +545,7 @@ 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,fields_3)
genetical_algorithm_test(fields_3,agent)
recognize_plants(plants_array, fields_2, fields,agent)
x = True
t = tree.treelearn()
while common.get('game_running'):
@ -615,6 +560,7 @@ 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))