Add basic fitness for genetic algorithm #32

Merged
s459307 merged 1 commits from add_basic_fitness into master 2022-06-08 21:07:14 +02:00

63
genetical_algorithm.py Normal file
View File

@ -0,0 +1,63 @@
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
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
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
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
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