From 1f2abc6e622968f7d629ef5945a3e37ff4e4dca4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Cwojdzi=C5=84ski?= Date: Wed, 8 Jun 2022 17:33:41 +0200 Subject: [PATCH] Add basic fitness for genetic algorithm --- genetical_algorithm.py | 63 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 genetical_algorithm.py diff --git a/genetical_algorithm.py b/genetical_algorithm.py new file mode 100644 index 0000000..05fa68c --- /dev/null +++ b/genetical_algorithm.py @@ -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 + \ No newline at end of file -- 2.20.1