Działająca wersja

This commit is contained in:
Miron 2021-05-23 22:05:49 +02:00
parent 351af34703
commit 9a74d214b5
8 changed files with 244 additions and 219 deletions

Binary file not shown.

View File

@ -1,31 +1,33 @@
import pygame import pygame
import random import random
import time import time
from src.a_star import a_star_search, WAREHOUSE_MAP, St from src.a_star import a_star_search, WAREHOUSE_MAP
from src.bfs import breadth_first_search from src.training_data.scripts.gen_data_id3 import generate_size, generate_weight, generate_price, generate_flammability, generate_explosiveness
from src.tree import predict_result, load_tree_from_structure
pygame.init() pygame.init()
WIDTH = 675 WIDTH = 675
HEIGHT = 675 HEIGHT = 675
size = 75 size = 75
QTY_OF_PACKAGES = 8 QTY_OF_PACKAGES = 8
RECT_SIZE = 9 RECT_SIZE = 9
data_tree = load_tree_from_structure('tree/tree_model')
IMAGE = pygame.image.load('img/wozek2.png') IMAGE = pygame.image.load('img/wozek2.png')
BACKGROUND = pygame.transform.scale(pygame.image.load('img/background.png'), (WIDTH, HEIGHT)) BACKGROUND = pygame.transform.scale(pygame.image.load('img/background.png'), (WIDTH, HEIGHT))
DOCK = pygame.transform.scale(pygame.image.load('img/dock_left.png'), (75, 75)) DOCK = pygame.transform.scale(pygame.image.load('img/dock_left.png'), (75, 75))
get_pix_from_position = {} get_pix_from_position = {}
def get_position_from_pix(pix_pos): def get_position_from_pix(pix_pos):
for key, value in get_pix_from_position.items(): for key, value in get_pix_from_position.items():
if pix_pos == value: if pix_pos == value:
return key return key
print("ERROR: THERE IS NO POSITION LIKE THIS (get_position_from_pix)") print("ERROR: THERE IS NO POSITION LIKE THIS (get_position_from_pix)")
def create_positions(): def create_positions():
size_agent = 40 size_agent = 40
x_position_pix = size_agent x_position_pix = size_agent
@ -36,27 +38,53 @@ def create_positions():
y_position_pix += size y_position_pix += size
y_position_pix = size_agent y_position_pix = size_agent
x_position_pix += size x_position_pix += size
def generate_package(a, b): def generate_package(a, b):
rand_y = random.randint(1, 4) name_list = ['Grocery', 'Builders', 'Electronic', 'Explosive']
rand_x = random.randint(10, 150) image_list = ['img/package_grocery.png', 'img/package_builders.png', 'img/package.png', 'img/package_explosive.png']
name_list = ['Grocery', 'Explosive', 'Electronic', 'Builders'] rand_type = random.randint(1, 4)
image_list = ['img/package_grocery.png', 'img/package_explosive.png', 'img/package.png', 'img/package_builders.png'] #rand_weight = generate_weight()
p1 = Package((a, b), rand_x, name_list[rand_y - 1], image_list[rand_y - 1]) rand_weight = random.randint(1, 10)
#rand_size = generate_size()
rand_size = random.randint(1, 10)
rand_flammability = generate_flammability(name_list[rand_type - 1])
rand_explosiveness = generate_explosiveness(name_list[rand_type - 1])
#rand_price = generate_price()
rand_price = random.randint(1, 10)
p1 = Package((a, b), rand_weight, rand_type, rand_size, rand_flammability, rand_explosiveness, rand_price,
image_list[rand_type - 1])
return p1 return p1
# class Package:
# def __init__(self, pos, content, content_size, pack_image):
# self.pos = pos
# self.content = content
# self.content_size = content_size
# self.image = pygame.transform.scale(pygame.image.load(pack_image), (50, 50))
# self.rect = self.image.get_rect(center=pos)
# self.is_package_up = False
class Package: class Package:
def __init__(self, pos, content, content_size, pack_image): def __init__(self, pos, package_weight, package_type, package_size, flammability, explosiveness, price, image):
self.pos = pos self.pos = pos
self.content = content self.package_weight = package_weight
self.content_size = content_size self.package_type = package_type
self.image = pygame.transform.scale(pygame.image.load(pack_image), (50, 50)) name_list = ['Grocery', 'Builders', 'Electronic', 'Explosive']
self.package_type_name = name_list[package_type - 1]
self.package_size = package_size
self.flammability = flammability
self.explosiveness = explosiveness
self.price = price
self.image = pygame.transform.scale(pygame.image.load(image), (50, 50))
self.is_accepted = predict_result(data_tree, self.package_weight, self.package_type, self.package_size, self.flammability, self.explosiveness, self.price)[0]
#self.is_accepted = 0
self.rect = self.image.get_rect(center=pos) self.rect = self.image.get_rect(center=pos)
self.is_package_up = False self.is_package_up = False
class Shelf: class Shelf:
def __init__(self, pos, content, content_size): def __init__(self, pos, content, content_size):
self.pos_pix = get_pix_from_position[pos] self.pos_pix = get_pix_from_position[pos]
@ -64,15 +92,15 @@ class Shelf:
self.content_size = content_size self.content_size = content_size
self.image = pygame.transform.scale(pygame.image.load('img/shelf.png'), (60, 60)) self.image = pygame.transform.scale(pygame.image.load('img/shelf.png'), (60, 60))
self.rect = self.image.get_rect(center=self.pos_pix) self.rect = self.image.get_rect(center=self.pos_pix)
class Stain: class Stain:
def __init__(self, pos): def __init__(self, pos):
self.pos_pix = get_pix_from_position[pos] self.pos_pix = get_pix_from_position[pos]
self.image = pygame.transform.scale(pygame.image.load("img/stain.png"), (60, 60)) self.image = pygame.transform.scale(pygame.image.load("img/stain.png"), (60, 60))
self.rect = self.image.get_rect(center=self.pos_pix) self.rect = self.image.get_rect(center=self.pos_pix)
class Agent: class Agent:
def __init__(self, pos, agent_direction="right"): def __init__(self, pos, agent_direction="right"):
self.pos = pos self.pos = pos
@ -83,101 +111,103 @@ class Agent:
self.path = None self.path = None
self.goal_achieved = False self.goal_achieved = False
self.agent_direction = agent_direction self.agent_direction = agent_direction
self.image_left = pygame.transform.flip(IMAGE, True, False) self.image_left = pygame.transform.flip(IMAGE, True, False)
self.image_right = IMAGE self.image_right = IMAGE
self.image_down = pygame.transform.rotate(self.image_right, -90) self.image_down = pygame.transform.rotate(self.image_right, -90)
self.image_up = pygame.transform.rotate(self.image_left, -90) self.image_up = pygame.transform.rotate(self.image_left, -90)
def move_bfs(self): def move_bfs(self):
if self.path is None or not self.path: if self.path is None or not self.path:
self.goal_achieved = True self.goal_achieved = True
else: else:
move = self.path.pop(0) move = self.path.pop(0)
if move == "rotate_left": if move == "rotate_left":
if self.agent_direction == "right": if self.agent_direction == "right":
self.agent_direction = "up" self.agent_direction = "up"
self.image = self.image_up self.image = self.image_up
elif self.agent_direction == "left": elif self.agent_direction == "left":
self.agent_direction = "down" self.agent_direction = "down"
self.image = self.image_down self.image = self.image_down
elif self.agent_direction == "up": elif self.agent_direction == "up":
self.agent_direction = "left" self.agent_direction = "left"
self.image = self.image_left self.image = self.image_left
elif self.agent_direction == "down": elif self.agent_direction == "down":
self.agent_direction = "right" self.agent_direction = "right"
self.image = self.image_right self.image = self.image_right
elif move == "rotate_right": elif move == "rotate_right":
if self.agent_direction == "right": if self.agent_direction == "right":
self.agent_direction = "down" self.agent_direction = "down"
self.image = self.image_down self.image = self.image_down
elif self.agent_direction == "left": elif self.agent_direction == "left":
self.agent_direction = "up" self.agent_direction = "up"
self.image = self.image_up self.image = self.image_up
elif self.agent_direction == "up": elif self.agent_direction == "up":
self.agent_direction = "right" self.agent_direction = "right"
self.image = self.image_right self.image = self.image_right
elif self.agent_direction == "down": elif self.agent_direction == "down":
self.agent_direction = "left" self.agent_direction = "left"
self.image = self.image_left self.image = self.image_left
elif move == "move": elif move == "move":
if self.agent_direction == "right": if self.agent_direction == "right":
self.rect.move_ip(size, 0) self.rect.move_ip(size, 0)
self.pos = (self.pos[0] + size, self.pos[1]) self.pos = (self.pos[0] + size, self.pos[1])
self.pos_coord = get_position_from_pix(self.pos) self.pos_coord = get_position_from_pix(self.pos)
elif self.agent_direction == "left": elif self.agent_direction == "left":
self.rect.move_ip(-size, 0) self.rect.move_ip(-size, 0)
self.pos = (self.pos[0] - size, self.pos[1]) self.pos = (self.pos[0] - size, self.pos[1])
self.pos_coord = get_position_from_pix(self.pos) self.pos_coord = get_position_from_pix(self.pos)
elif self.agent_direction == "up": elif self.agent_direction == "up":
self.rect.move_ip(0, -size) self.rect.move_ip(0, -size)
self.pos = (self.pos[0], self.pos[1] - size) self.pos = (self.pos[0], self.pos[1] - size)
self.pos_coord = get_position_from_pix(self.pos) self.pos_coord = get_position_from_pix(self.pos)
elif self.agent_direction == "down": elif self.agent_direction == "down":
self.rect.move_ip(0, size) self.rect.move_ip(0, size)
self.pos = (self.pos[0], self.pos[1] + size) self.pos = (self.pos[0], self.pos[1] + size)
self.pos_coord = get_position_from_pix(self.pos) self.pos_coord = get_position_from_pix(self.pos)
def lift_package_bfs(self): def lift_package_bfs(self):
for package in Package_list: for package in Package_list:
if package.is_package_up: if package.is_package_up:
package.is_package_up = False package.is_package_up = False
elif package.pos == agent.pos: elif package.pos == agent.pos:
package.is_package_up = True package.is_package_up = True
print("TYP: {}, WAGA: {}, ROZMIAR: {}, LATWOPALNOSC: {}, WYBUCHOWOSC: {}, CENA: {}, CZY PRZYJETA: {}"
.format(package.package_type_name, package.package_weight, package.package_size,
package.flammability,
package.explosiveness, package.price, package.is_accepted))
def goal_test(self, state): def goal_test(self, state):
if state == self.goal: if state == self.goal:
return True return True
else: else:
return False return False
board = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA) # transparently surface board = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA) # transparently surface
create_positions() create_positions()
# Rysowanie lini # Rysowanie lini
for x in range(9): for x in range(9):
for y in range(9): for y in range(9):
pygame.draw.rect(board, (0, 0, 0), (x * size, y * size, size, size), 3) pygame.draw.rect(board, (0, 0, 0), (x * size, y * size, size, size), 3)
agent = Agent((40, 640)) agent = Agent((40, 640))
Shelf_list = [ Shelf_list = [
Shelf((5, 2), 'Explosive', 5), Shelf((6, 2), 'Explosive', 5), Shelf((7, 2), 'Explosive', 5), Shelf((5, 2), 'Explosive', 5), Shelf((6, 2), 'Explosive', 5), Shelf((7, 2), 'Explosive', 5),
Shelf((8, 2), 'Explosive', 5), Shelf((8, 2), 'Explosive', 5),
@ -186,77 +216,88 @@ Shelf_list = [
Shelf((8, 6), 'Builders', 5), Shelf((8, 6), 'Builders', 5),
Shelf((5, 8), 'Electronic', 5), Shelf((6, 8), 'Electronic', 5), Shelf((7, 8), 'Electronic', 5), Shelf((5, 8), 'Electronic', 5), Shelf((6, 8), 'Electronic', 5), Shelf((7, 8), 'Electronic', 5),
Shelf((8, 8), 'Electronic', 5)] Shelf((8, 8), 'Electronic', 5)]
coord_goals = [(8, 0), (0, 0), (5, 8), (0, 0), (8, 0), (0, 0), (8, 8), (0, 0), coord_goals = [(0, 0)]
(5, 6), (0, 0), (6, 6), (0, 0), (7, 6), (0, 0), (8, 6), (0, 8)] free_shelf = [(5, 8), (6, 8), (7, 8), (8, 8), (5, 6), (6, 6), (7, 6), (8, 6)]
screen = pygame.display.set_mode([WIDTH, HEIGHT]) screen = pygame.display.set_mode([WIDTH, HEIGHT])
Package_list = [ Package_list = [
generate_package(40, 40) Package((40, 40), 3, 4, 6, 3, 3, 6, 'img/package_explosive.png')
] ]
agent.path = a_star_search((agent.pos_coord[1], agent.pos_coord[0]), coord_goals.pop(0), agent.agent_direction)
#print(a_star_search((6, 8), (0, 0), "down"))
agent.path = a_star_search((agent.pos_coord[1], agent.pos_coord[0]), (0, 0), agent.agent_direction)
# Pętla służąca do tworzenia plam oleju na podstawie mapy magazynu # Pętla służąca do tworzenia plam oleju na podstawie mapy magazynu
Stain_list = [] Stain_list = []
for index_x in range(9): for index_x in range(9):
for index_y in range(9): for index_y in range(9):
if WAREHOUSE_MAP[index_x][index_y] == St: if WAREHOUSE_MAP[index_x][index_y] == 10:
Stain_list.append(Stain((index_y, index_x))) Stain_list.append(Stain((index_y, index_x)))
running = True running = True
while running: while running:
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
running = False running = False
if event.type == pygame.KEYDOWN: if event.type == pygame.KEYDOWN:
pass pass
if len(Package_list) < QTY_OF_PACKAGES: if len(Package_list) < QTY_OF_PACKAGES:
is_dock_empty = True is_dock_empty = True
for package in Package_list: for package in Package_list:
if package.pos == (40, 40): if package.pos == (40, 40):
is_dock_empty = False is_dock_empty = False
elif package.pos == get_pix_from_position[(8, 0)] and not package.is_package_up: elif package.pos == get_pix_from_position[(8, 0)] and not package.is_package_up:
print("Paczka wyrzucona") print("Paczka wyrzucona")
Package_list.remove(package) Package_list.remove(package)
is_dock_empty = False
elif get_position_from_pix(package.pos) in free_shelf:
Package_list.remove(package)
is_dock_empty = False
if is_dock_empty: if is_dock_empty:
Package_list.append(generate_package(40, 40)) Package_list.append(generate_package(40, 40))
# PATHING # PATHING
if not agent.goal_achieved: if not agent.goal_achieved:
# print(agent.path) # print(agent.path)
agent.move_bfs() agent.move_bfs()
else: else:
agent.lift_package_bfs() agent.lift_package_bfs()
if coord_goals:
agent.goal = coord_goals.pop(0) if agent.pos_coord == (0, 0):
agent.goal_achieved = False
if Package_list[-1].is_accepted == 1:
#agent.path = breadth_first_search(agent.pos_coord, agent.goal, agent.agent_direction) coord_goals.append(free_shelf[random.randint(0, 7)])
agent.path = a_star_search((agent.pos_coord[1], agent.pos_coord[0]), (agent.goal[1], agent.goal[0]), agent.agent_direction) coord_goals.append((0, 0))
else:
coord_goals.append((8, 0))
coord_goals.append((0, 0))
agent.goal = coord_goals.pop(0)
agent.goal_achieved = False
agent.path = a_star_search((agent.pos_coord[1], agent.pos_coord[0]), (agent.goal[1], agent.goal[0]),
agent.agent_direction)
for package in Package_list: for package in Package_list:
if package.is_package_up: if package.is_package_up:
package.rect.move_ip(agent.pos[0] - package.pos[0], agent.pos[1] - package.pos[1]) package.rect.move_ip(agent.pos[0] - package.pos[0], agent.pos[1] - package.pos[1])
package.pos = agent.pos package.pos = agent.pos
# DRAWING # DRAWING
screen.blit(BACKGROUND, [0, 0]) screen.blit(BACKGROUND, [0, 0])
screen.blit(DOCK, [0, 0]) screen.blit(DOCK, [0, 0])
screen.blit(pygame.transform.flip(DOCK, True, False), [8*size, 0]) screen.blit(pygame.transform.flip(DOCK, True, False), [8 * size, 0])
screen.blit(board, board.get_rect()) screen.blit(board, board.get_rect())
for shelf in Shelf_list: for shelf in Shelf_list:
screen.blit(shelf.image, shelf.rect) screen.blit(shelf.image, shelf.rect)
@ -265,9 +306,9 @@ while running:
screen.blit(agent.image, agent.rect) screen.blit(agent.image, agent.rect)
for package in Package_list: for package in Package_list:
screen.blit(package.image, package.rect) screen.blit(package.image, package.rect)
time.sleep(0.15) time.sleep(0.05)
pygame.display.update() pygame.display.update()
pygame.quit() pygame.quit()

View File

@ -1,3 +1,5 @@
3,4,6,2,3,8,0
3,4,6,2,3,8,1
10,2,2,0,0,4,0 10,2,2,0,0,4,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0 5 0
158 7 7 1 8 8 1 0 0 0 1 0 0
159 2 2 3 1 9 9 1 0 0 0 3 0 0 1
160 6 6 9 3 1 1 3 4 0 0 9 1 4 0
161 4 4 4 3 5 5 3 0 0 0 4 1 0 1
162 5 5 5 4 6 6 4 3 3 3 5 1 3 1
163 1 1 3 2 10 10 2 0 0 3 0 2 0
193 3 3 2 2 2 2 3 2 2 2 1 3 1
194 2 2 5 1 7 7 1 0 0 0 5 1 0 1
195 1 1 4 2 3 3 2 0 2 2 4 1 0 1
196 9 9 2 5 5 2 0 0 0 2 0 0 1
197 4 4 5 3 8 8 3 5 0 0 5 0 5 0
198 2 2 1 2 6 6 2 4 0 0 1 1 4 1
199 3 3 3 4 4 3 0 2 2 3 1 0 1
200 1 1 4 3 5 5 3 0 0 0 4 1 0 1
201 2 2 9 4 4 4 4 3 3 3 9 0 3 0
202 1 1 2 6 6 2 0 0 0 2 1 0 1

View File

@ -4,8 +4,8 @@ import pandas
from sklearn import tree from sklearn import tree
from sklearn.tree import DecisionTreeClassifier from sklearn.tree import DecisionTreeClassifier
columns = ["weight", "type", "size", "inflammability", "explosiveness", "prize", "allowed_in"] columns = ["weight", "type", "size", "inflammability", "explosiveness", "price", "allowed_in"]
attributes = ["weight", "type", "size", "inflammability", "explosiveness", "prize"] attributes = ["weight", "type", "size", "inflammability", "explosiveness", "price"]
decisions = ["allowed_in"] decisions = ["allowed_in"]
@ -21,8 +21,8 @@ def learning_tree():
return decision_tree return decision_tree
def making_decision(decision_tree, weight, type, size, inflammability, explosiveness, prize): def predict_result(decision_tree, weight, type, size, inflammability, explosiveness, price):
decision = decision_tree.predict([[weight, type, size, inflammability, explosiveness, prize]]) decision = decision_tree.predict([[weight, type, size, inflammability, explosiveness, price]])
return decision return decision
@ -50,3 +50,9 @@ def save_tree_to_structure(decision_tree):
def load_tree_from_structure(file): def load_tree_from_structure(file):
return joblib.load(file) return joblib.load(file)
if __name__ == "__main__":
tree_data = learning_tree()
save_all(tree_data)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 337 KiB

After

Width:  |  Height:  |  Size: 302 KiB

View File

@ -1,6 +1,6 @@
|--- feature_3 <= 2.50 |--- feature_0 <= 8.50
| |--- feature_5 <= 9.50 | |--- feature_5 <= 8.50
| | |--- feature_0 <= 8.50 | | |--- feature_3 <= 1.50
| | | |--- feature_2 <= 7.50 | | | |--- feature_2 <= 7.50
| | | | |--- feature_5 <= 7.50 | | | | |--- feature_5 <= 7.50
| | | | | |--- feature_2 <= 2.50 | | | | | |--- feature_2 <= 2.50
@ -13,130 +13,106 @@
| | | | | | | | |--- feature_5 <= 3.00 | | | | | | | | |--- feature_5 <= 3.00
| | | | | | | | | |--- class: 1 | | | | | | | | | |--- class: 1
| | | | | | | | |--- feature_5 > 3.00 | | | | | | | | |--- feature_5 > 3.00
| | | | | | | | | |--- feature_3 <= 1.00 | | | | | | | | | |--- class: 0
| | | | | | | | | | |--- class: 0
| | | | | | | | | |--- feature_3 > 1.00
| | | | | | | | | | |--- class: 1
| | | | | |--- feature_2 > 2.50 | | | | | |--- feature_2 > 2.50
| | | | | | |--- class: 1 | | | | | | |--- class: 1
| | | | |--- feature_5 > 7.50 | | | | |--- feature_5 > 7.50
| | | | | |--- feature_3 <= 0.50 | | | | | |--- feature_1 <= 2.50
| | | | | | |--- feature_2 <= 2.50
| | | | | | | |--- class: 0
| | | | | | |--- feature_2 > 2.50
| | | | | | | |--- feature_0 <= 3.50
| | | | | | | | |--- feature_2 <= 5.50
| | | | | | | | | |--- class: 0
| | | | | | | | |--- feature_2 > 5.50
| | | | | | | | | |--- class: 1
| | | | | | | |--- feature_0 > 3.50
| | | | | | | | |--- class: 1
| | | | | |--- feature_3 > 0.50
| | | | | | |--- class: 1 | | | | | | |--- class: 1
| | | | | |--- feature_1 > 2.50
| | | | | | |--- feature_0 <= 4.50
| | | | | | | |--- class: 0
| | | | | | |--- feature_0 > 4.50
| | | | | | | |--- class: 1
| | | |--- feature_2 > 7.50 | | | |--- feature_2 > 7.50
| | | | |--- feature_1 <= 2.50 | | | | |--- feature_0 <= 5.00
| | | | | |--- feature_2 <= 9.50 | | | | | |--- feature_2 <= 8.50
| | | | | | |--- class: 0 | | | | | | |--- class: 1
| | | | | |--- feature_2 > 9.50 | | | | | |--- feature_2 > 8.50
| | | | | | |--- feature_5 <= 2.00 | | | | | | |--- feature_2 <= 9.50
| | | | | | | |--- feature_1 <= 1.50
| | | | | | | | |--- feature_5 <= 3.50
| | | | | | | | | |--- class: 1
| | | | | | | | |--- feature_5 > 3.50
| | | | | | | | | |--- class: 0
| | | | | | | |--- feature_1 > 1.50
| | | | | | | | |--- class: 0
| | | | | | |--- feature_2 > 9.50
| | | | | | | |--- class: 1 | | | | | | | |--- class: 1
| | | | | | |--- feature_5 > 2.00 | | | | |--- feature_0 > 5.00
| | | | | | | |--- class: 0
| | | | |--- feature_1 > 2.50
| | | | | |--- feature_2 <= 9.00
| | | | | | |--- class: 1
| | | | | |--- feature_2 > 9.00
| | | | | | |--- class: 0
| | |--- feature_0 > 8.50
| | | |--- feature_5 <= 1.50
| | | | |--- class: 1
| | | |--- feature_5 > 1.50
| | | | |--- feature_5 <= 6.50
| | | | | |--- feature_2 <= 4.50
| | | | | | |--- feature_1 <= 1.50
| | | | | | | |--- feature_0 <= 9.50
| | | | | | | | |--- class: 0
| | | | | | | |--- feature_0 > 9.50
| | | | | | | | |--- class: 1
| | | | | | |--- feature_1 > 1.50
| | | | | | | |--- class: 0
| | | | | |--- feature_2 > 4.50
| | | | | | |--- feature_1 <= 2.50
| | | | | | | |--- class: 0
| | | | | | |--- feature_1 > 2.50
| | | | | | | |--- feature_4 <= 1.00
| | | | | | | | |--- class: 1
| | | | | | | |--- feature_4 > 1.00
| | | | | | | | |--- class: 0
| | | | |--- feature_5 > 6.50
| | | | | |--- class: 1
| |--- feature_5 > 9.50
| | |--- feature_4 <= 0.50
| | | |--- class: 0
| | |--- feature_4 > 0.50
| | | |--- class: 1
|--- feature_3 > 2.50
| |--- feature_3 <= 4.50
| | |--- feature_2 <= 8.50
| | | |--- feature_0 <= 1.50
| | | | |--- feature_1 <= 3.50
| | | | | |--- feature_5 <= 3.00
| | | | | | |--- class: 1
| | | | | |--- feature_5 > 3.00
| | | | | | |--- class: 0
| | | | |--- feature_1 > 3.50
| | | | | |--- class: 0 | | | | | |--- class: 0
| | | |--- feature_0 > 1.50 | | |--- feature_3 > 1.50
| | | | |--- feature_0 <= 6.50 | | | |--- feature_4 <= 3.50
| | | | | |--- feature_0 <= 4.50 | | | | |--- feature_2 <= 9.00
| | | | | | |--- feature_5 <= 7.00 | | | | | |--- feature_3 <= 4.50
| | | | | | | |--- feature_0 <= 2.50 | | | | | | |--- feature_1 <= 3.50
| | | | | | | |--- feature_3 <= 3.50
| | | | | | | | |--- class: 1 | | | | | | | | |--- class: 1
| | | | | | | |--- feature_0 > 2.50 | | | | | | | |--- feature_3 > 3.50
| | | | | | | | |--- feature_2 <= 6.00 | | | | | | | | |--- feature_5 <= 4.00
| | | | | | | | | |--- feature_4 <= 2.50 | | | | | | | | | |--- class: 1
| | | | | | | | | | |--- feature_1 <= 3.00 | | | | | | | | |--- feature_5 > 4.00
| | | | | | | | | | | |--- class: 1 | | | | | | | | | |--- class: 0
| | | | | | | | | | |--- feature_1 > 3.00 | | | | | | |--- feature_1 > 3.50
| | | | | | | |--- feature_3 <= 3.50
| | | | | | | | |--- feature_2 <= 3.50
| | | | | | | | | |--- class: 0
| | | | | | | | |--- feature_2 > 3.50
| | | | | | | | | |--- feature_0 <= 4.50
| | | | | | | | | | |--- feature_5 <= 2.50
| | | | | | | | | | | |--- class: 0 | | | | | | | | | | | |--- class: 0
| | | | | | | | | |--- feature_4 > 2.50 | | | | | | | | | | |--- feature_5 > 2.50
| | | | | | | | | | | |--- truncated branch of depth 3
| | | | | | | | | |--- feature_0 > 4.50
| | | | | | | | | | |--- class: 1 | | | | | | | | | | |--- class: 1
| | | | | | | | |--- feature_2 > 6.00 | | | | | | | |--- feature_3 > 3.50
| | | | | | | | | |--- feature_3 <= 3.50 | | | | | | | | |--- feature_0 <= 2.00
| | | | | | | | | | |--- class: 0 | | | | | | | | | |--- class: 0
| | | | | | | | | |--- feature_3 > 3.50 | | | | | | | | |--- feature_0 > 2.00
| | | | | | | | | | |--- feature_0 <= 3.50 | | | | | | | | | |--- class: 1
| | | | | | | | | | | |--- class: 0 | | | | | |--- feature_3 > 4.50
| | | | | | | | | | |--- feature_0 > 3.50 | | | | | | |--- feature_2 <= 2.50
| | | | | | | | | | | |--- class: 1
| | | | | | |--- feature_5 > 7.00
| | | | | | | |--- class: 0
| | | | | |--- feature_0 > 4.50
| | | | | | |--- class: 1
| | | | |--- feature_0 > 6.50
| | | | | |--- feature_5 <= 2.50
| | | | | | |--- feature_4 <= 4.50
| | | | | | | |--- class: 1 | | | | | | | |--- class: 1
| | | | | | |--- feature_4 > 4.50 | | | | | | |--- feature_2 > 2.50
| | | | | | | |--- class: 0 | | | | | | | |--- feature_0 <= 5.50
| | | | | |--- feature_5 > 2.50 | | | | | | | | |--- class: 0
| | | | | | |--- class: 0 | | | | | | | |--- feature_0 > 5.50
| | |--- feature_2 > 8.50 | | | | | | | | |--- class: 1
| | | |--- class: 0 | | | | |--- feature_2 > 9.00
| |--- feature_3 > 4.50
| | |--- feature_0 <= 2.50
| | | |--- feature_2 <= 2.50
| | | | |--- class: 1
| | | |--- feature_2 > 2.50
| | | | |--- class: 0
| | |--- feature_0 > 2.50
| | | |--- feature_4 <= 0.50
| | | | |--- feature_0 <= 5.50
| | | | | |--- class: 0 | | | | | |--- class: 0
| | | | |--- feature_0 > 5.50 | | | |--- feature_4 > 3.50
| | | | | |--- feature_0 <= 8.00 | | | | |--- feature_3 <= 3.50
| | | | | |--- feature_4 <= 4.50
| | | | | | |--- class: 1 | | | | | | |--- class: 1
| | | | | |--- feature_0 > 8.00 | | | | | |--- feature_4 > 4.50
| | | | | | |--- class: 0 | | | | | | |--- feature_2 <= 3.50
| | | |--- feature_4 > 0.50 | | | | | | | |--- feature_2 <= 1.50
| | | | | | | | |--- class: 0
| | | | | | | |--- feature_2 > 1.50
| | | | | | | | |--- class: 1
| | | | | | |--- feature_2 > 3.50
| | | | | | | |--- class: 0
| | | | |--- feature_3 > 3.50
| | | | | |--- class: 0
| |--- feature_5 > 8.50
| | |--- feature_0 <= 1.50
| | | |--- class: 1
| | |--- feature_0 > 1.50
| | | |--- feature_2 <= 1.50
| | | | |--- feature_0 <= 3.00
| | | | | |--- class: 1
| | | | |--- feature_0 > 3.00
| | | | | |--- class: 0
| | | |--- feature_2 > 1.50
| | | | |--- class: 0 | | | | |--- class: 0
|--- feature_0 > 8.50
| |--- feature_5 <= 2.50
| | |--- feature_1 <= 2.50
| | | |--- class: 1
| | |--- feature_1 > 2.50
| | | |--- feature_0 <= 9.50
| | | | |--- class: 0
| | | |--- feature_0 > 9.50
| | | | |--- class: 1
| |--- feature_5 > 2.50
| | |--- class: 0

Binary file not shown.