Przyrost "Drzewa decyzyjne" (Add decisionTrees.py)
This commit is contained in:
parent
414211131b
commit
148af1f97f
50
classes/decisionTrees.py
Normal file
50
classes/decisionTrees.py
Normal file
@ -0,0 +1,50 @@
|
||||
from chefboost import Chefboost as chef
|
||||
from multiprocessing import freeze_support
|
||||
import pandas as pd
|
||||
from numpy import random
|
||||
|
||||
|
||||
class DecisionTrees:
|
||||
|
||||
def return_predict(self):
|
||||
# header = ['Size(bigger_more_difficult)', 'Year(older_more_difficult)', 'Protection_from_defuse',
|
||||
# 'Meters_under_the_ground', 'Random_detonation_chance', 'Detonation_power_in_m',
|
||||
# 'Decision']
|
||||
|
||||
# read data
|
||||
|
||||
df = pd.read_csv("D:\\1 Python projects\Saper\data\db.txt")
|
||||
|
||||
# print data
|
||||
# print(df.head())
|
||||
|
||||
lines = []
|
||||
|
||||
with open('D:\\1 Python projects\Saper\data\db.txt') as f:
|
||||
line = f.readline()
|
||||
for i in range(0, 200):
|
||||
line = f.readline()
|
||||
line = line.rstrip()
|
||||
line = line.replace(",detonate", "")
|
||||
line = line.replace(",defuse", "")
|
||||
lines.append(line)
|
||||
|
||||
ss = []
|
||||
for line in lines:
|
||||
ss.append(line.split(","))
|
||||
|
||||
normalized_data_for_predict = []
|
||||
for i in ss:
|
||||
normalized_data_for_predict.append(list(map(int, i)))
|
||||
print(normalized_data_for_predict)
|
||||
|
||||
# ID3 config
|
||||
config = {'algorithm': 'ID3'}
|
||||
# create decision tree
|
||||
model = chef.fit(df, config)
|
||||
|
||||
# print predict
|
||||
# print(chef.predict(model, [1, 2022, 0, 0, 0, 10]))
|
||||
|
||||
predict = normalized_data_for_predict[random.randint(0, 199)]
|
||||
return chef.predict(model, predict)
|
@ -1,6 +1,7 @@
|
||||
import pygame
|
||||
from classes import system
|
||||
from random import randrange
|
||||
from classes import decisionTrees
|
||||
|
||||
pygame.mixer.init()
|
||||
|
||||
@ -390,6 +391,12 @@ class Minesweeper:
|
||||
return
|
||||
for mine in self.current_map.mines:
|
||||
if (self.position_x, self.position_y) == (mine.position_x, mine.position_y):
|
||||
|
||||
tree = decisionTrees.DecisionTrees()
|
||||
|
||||
decision = tree.return_predict()
|
||||
print(decision)
|
||||
|
||||
self.current_map.mines.remove(mine)
|
||||
pygame.mixer.Channel(3).set_volume(0.7)
|
||||
pygame.mixer.Channel(3).play(pygame.mixer.Sound("assets/sounds/pickup.wav"))
|
||||
|
201
data/db.txt
Normal file
201
data/db.txt
Normal file
@ -0,0 +1,201 @@
|
||||
Size(bigger_more_difficult),Year(older_more_difficult),Protection_from_defuse,Meters_under_the_ground,Random_detonation_chance,Detonation_power_in_m,Decision
|
||||
10,2022,1,10,100,1,detonate
|
||||
8,1993,0,4,37,0,defuse
|
||||
2,1978,0,7,70,0,defuse
|
||||
9,2003,0,9,35,0,detonate
|
||||
5,1954,0,9,70,0,detonate
|
||||
7,1949,0,6,96,0,detonate
|
||||
3,1971,0,2,51,0,defuse
|
||||
5,1954,0,7,86,0,detonate
|
||||
4,1968,0,3,25,6,defuse
|
||||
4,1961,0,4,46,5,defuse
|
||||
2,1961,0,2,40,6,defuse
|
||||
9,1947,0,5,18,0,defuse
|
||||
1,1967,0,6,21,0,defuse
|
||||
7,1960,0,4,36,0,defuse
|
||||
3,1979,0,2,46,2,defuse
|
||||
1,2007,1,7,92,0,detonate
|
||||
1,1970,0,3,98,5,detonate
|
||||
7,1981,0,9,43,0,detonate
|
||||
5,1986,0,3,87,6,detonate
|
||||
4,1944,0,3,80,3,detonate
|
||||
5,1970,0,5,20,0,defuse
|
||||
5,1995,0,3,53,0,defuse
|
||||
7,1996,0,8,27,0,detonate
|
||||
3,1986,0,1,61,6,defuse
|
||||
7,1956,0,2,87,3,detonate
|
||||
6,1997,0,7,70,0,defuse
|
||||
8,1944,0,6,10,0,defuse
|
||||
2,1996,0,6,68,0,defuse
|
||||
1,1979,0,3,48,2,defuse
|
||||
3,1964,0,3,24,3,defuse
|
||||
7,1974,0,0,96,9,detonate
|
||||
9,2021,1,3,55,4,detonate
|
||||
7,1945,0,7,60,0,defuse
|
||||
4,2001,1,8,77,0,detonate
|
||||
4,1979,0,6,76,0,detonate
|
||||
8,2010,1,4,93,2,detonate
|
||||
8,2004,1,4,89,0,detonate
|
||||
8,1998,0,0,44,1,defuse
|
||||
6,1952,0,6,23,0,defuse
|
||||
2,1942,0,1,12,2,defuse
|
||||
2,1974,0,8,9,0,detonate
|
||||
9,1969,0,7,33,0,defuse
|
||||
3,1985,0,1,37,0,defuse
|
||||
6,2003,1,5,86,4,detonate
|
||||
8,2011,0,3,67,0,defuse
|
||||
8,1966,0,4,35,4,defuse
|
||||
2,1985,0,2,31,6,defuse
|
||||
1,1975,0,1,46,7,defuse
|
||||
1,1962,0,1,96,1,detonate
|
||||
1,1975,0,7,6,0,defuse
|
||||
5,1995,0,3,17,0,defuse
|
||||
7,1979,0,6,84,0,detonate
|
||||
7,2002,1,5,80,0,detonate
|
||||
6,1950,0,0,98,2,detonate
|
||||
5,1977,0,5,99,0,detonate
|
||||
2,1969,0,1,52,0,defuse
|
||||
8,1970,0,7,20,0,defuse
|
||||
7,1994,0,1,93,1,detonate
|
||||
5,1979,0,9,99,0,detonate
|
||||
8,1990,0,5,39,0,defuse
|
||||
9,1942,0,8,20,0,detonate
|
||||
4,2008,1,6,42,0,detonate
|
||||
2,1996,0,1,68,4,defuse
|
||||
7,1968,0,7,48,0,defuse
|
||||
1,1994,0,0,26,1,defuse
|
||||
3,1956,0,9,43,0,detonate
|
||||
7,2017,0,1,32,0,defuse
|
||||
6,1946,0,2,90,7,detonate
|
||||
7,1976,0,6,63,0,defuse
|
||||
1,2014,0,5,22,0,defuse
|
||||
4,1966,0,7,80,0,detonate
|
||||
1,1952,0,9,49,0,detonate
|
||||
3,1968,0,4,16,0,defuse
|
||||
4,1958,0,8,44,0,detonate
|
||||
3,2002,1,7,93,0,detonate
|
||||
3,1961,0,6,51,0,defuse
|
||||
5,2016,1,1,83,5,detonate
|
||||
6,1954,0,2,70,1,defuse
|
||||
3,2005,1,6,93,0,detonate
|
||||
9,1982,0,0,65,0,defuse
|
||||
8,1946,0,3,78,0,detonate
|
||||
5,1968,0,2,21,3,defuse
|
||||
1,2002,1,3,16,5,detonate
|
||||
4,1970,0,9,70,0,detonate
|
||||
2,1941,0,1,84,6,detonate
|
||||
4,1996,0,3,48,3,defuse
|
||||
7,1977,0,4,87,1,detonate
|
||||
7,1960,0,0,59,8,defuse
|
||||
6,2001,1,5,4,0,detonate
|
||||
9,1986,0,2,37,0,defuse
|
||||
1,1975,0,1,8,7,defuse
|
||||
7,2001,0,5,73,3,defuse
|
||||
3,2005,0,7,9,0,defuse
|
||||
7,1999,0,7,72,0,defuse
|
||||
4,1952,0,0,96,3,detonate
|
||||
2,1973,0,1,72,0,defuse
|
||||
9,2020,1,3,38,5,detonate
|
||||
3,2020,0,1,12,5,defuse
|
||||
7,1995,0,0,39,7,defuse
|
||||
2,1987,0,2,72,0,defuse
|
||||
8,2009,1,3,35,4,detonate
|
||||
2,1953,0,5,21,1,defuse
|
||||
1,1944,0,4,84,5,detonate
|
||||
7,1992,0,5,69,0,defuse
|
||||
7,1965,0,6,0,0,defuse
|
||||
1,2012,1,0,35,8,detonate
|
||||
2,1955,0,1,62,0,defuse
|
||||
8,1996,0,6,53,1,defuse
|
||||
1,1996,0,8,80,1,detonate
|
||||
3,1941,0,3,29,2,defuse
|
||||
7,2007,0,3,70,6,defuse
|
||||
6,2019,1,9,28,0,detonate
|
||||
6,1994,0,5,93,0,detonate
|
||||
9,1973,0,7,33,0,defuse
|
||||
1,1998,0,0,58,0,defuse
|
||||
3,2000,1,8,34,0,detonate
|
||||
3,1990,0,0,75,6,detonate
|
||||
9,1973,0,4,17,3,defuse
|
||||
9,1995,0,3,33,5,defuse
|
||||
4,2014,1,5,9,0,detonate
|
||||
5,1969,0,7,32,0,defuse
|
||||
3,1964,0,1,45,2,defuse
|
||||
3,1975,0,0,88,1,detonate
|
||||
5,1965,0,1,83,6,detonate
|
||||
5,1983,0,3,80,6,detonate
|
||||
2,2012,0,2,56,2,defuse
|
||||
7,1988,0,6,13,2,defuse
|
||||
5,1961,0,9,95,0,detonate
|
||||
4,1945,0,6,71,0,defuse
|
||||
9,2009,1,7,3,1,detonate
|
||||
5,1970,0,1,62,0,defuse
|
||||
1,1988,0,1,53,7,defuse
|
||||
7,2003,1,6,75,0,detonate
|
||||
4,1949,0,1,58,3,defuse
|
||||
1,1998,0,0,16,3,defuse
|
||||
2,1995,0,6,75,3,detonate
|
||||
6,1982,0,8,82,0,detonate
|
||||
7,1973,0,0,84,9,detonate
|
||||
1,1997,0,8,35,0,detonate
|
||||
1,2020,1,3,62,1,detonate
|
||||
3,1989,0,5,25,2,defuse
|
||||
8,2012,0,2,6,0,defuse
|
||||
5,1949,0,7,9,0,defuse
|
||||
3,2000,1,5,29,0,detonate
|
||||
3,2005,0,4,44,2,defuse
|
||||
7,1969,0,2,44,0,defuse
|
||||
8,1965,0,5,57,0,defuse
|
||||
3,1990,0,9,5,0,detonate
|
||||
5,2015,1,6,80,0,detonate
|
||||
8,1992,0,1,94,6,detonate
|
||||
1,1966,0,9,51,0,detonate
|
||||
3,1971,0,3,83,5,detonate
|
||||
9,2002,1,2,74,0,detonate
|
||||
2,1975,0,0,9,7,defuse
|
||||
9,1974,0,2,69,2,defuse
|
||||
7,2010,1,8,58,1,detonate
|
||||
3,1941,0,8,55,1,detonate
|
||||
8,1977,0,7,68,1,defuse
|
||||
8,1965,0,9,57,0,detonate
|
||||
5,1984,0,6,11,0,defuse
|
||||
7,1952,0,3,79,3,detonate
|
||||
8,2012,1,5,1,1,detonate
|
||||
9,1970,0,7,93,0,detonate
|
||||
9,1948,0,4,88,4,detonate
|
||||
7,2006,0,8,34,0,detonate
|
||||
9,1981,0,0,88,9,detonate
|
||||
1,2013,1,7,43,0,detonate
|
||||
3,1946,0,3,69,6,defuse
|
||||
4,1953,0,3,72,0,defuse
|
||||
9,1955,0,5,1,0,defuse
|
||||
2,1987,0,4,46,5,defuse
|
||||
4,1997,0,8,8,0,detonate
|
||||
9,1963,0,6,26,0,defuse
|
||||
8,2008,1,7,97,0,detonate
|
||||
8,1950,0,8,71,0,detonate
|
||||
7,2016,0,0,70,8,defuse
|
||||
1,2009,0,8,99,0,detonate
|
||||
2,2002,1,7,26,0,detonate
|
||||
2,1987,0,5,48,0,defuse
|
||||
3,1988,0,5,49,2,defuse
|
||||
4,1975,0,1,14,1,defuse
|
||||
2,1975,0,2,25,4,defuse
|
||||
8,1993,0,2,47,5,defuse
|
||||
8,1963,0,4,79,0,detonate
|
||||
4,2021,1,8,5,0,detonate
|
||||
2,2012,0,3,56,0,defuse
|
||||
7,2007,0,5,50,3,defuse
|
||||
2,1988,0,4,92,0,detonate
|
||||
8,1981,0,6,14,0,defuse
|
||||
6,2003,1,3,68,0,detonate
|
||||
8,2013,0,7,57,0,defuse
|
||||
1,1968,0,6,39,2,defuse
|
||||
8,1966,0,2,71,7,defuse
|
||||
6,1997,0,0,59,4,defuse
|
||||
7,1943,0,4,23,5,defuse
|
||||
1,1999,0,5,63,4,defuse
|
||||
5,2015,1,0,40,0,detonate
|
||||
8,2020,1,5,40,0,detonate
|
||||
6,1956,0,7,60,0,defuse
|
||||
3,1989,0,1,1,5,defuse
|
5
main.py
5
main.py
@ -1,9 +1,11 @@
|
||||
# pygame - biblioteka do symulacji graficznych
|
||||
from multiprocessing import freeze_support
|
||||
|
||||
import pygame
|
||||
# system - klasy związane z pygame
|
||||
# minesweeper - klasy związane z samym saperem
|
||||
# ai - klasa wykonująca ruchy sapera
|
||||
from classes import system, minesweeper, ai
|
||||
from classes import system, minesweeper, ai, decisionTrees
|
||||
|
||||
# ustalenie wielkości pojedyńczych kawałków mapy, oraz wielkości mapy
|
||||
TILE_SIZE = 64
|
||||
@ -78,4 +80,5 @@ def main():
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
freeze_support()
|
||||
main()
|
||||
|
Loading…
Reference in New Issue
Block a user