Код: Выделить всё
Traceback (most recent call last):
File "C:\Users\Administrator\TradEvolver\main.py", line 188, in
run("./config.txt")
File "C:\Users\Administrator\TradEvolver\main.py", line 186, in run
winner = p.run(main, 1000)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\site-packages\neat\population.py", line 94, in run
if best is None or g.fitness > best.fitness:
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\generic.py", line 1577, in __nonzero__
raise ValueError(
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Код: Выделить всё
import pandas as pd
import random
import time
import matplotlib.pyplot as plt
import threading
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import tkinter as tk
from tkinter import messagebox
import sys
import neat
def calculate_profit(buy_price, sell_price, balance):
c = 0.001
profit = (sell_price - buy_price) / buy_price * balance - (balance * c)
new_balance = balance + profit
return new_balance
class Trader:
def __init__(self):
self.balance = 10000
self.buy_price = 0
self.since_last_transaction = 0
self.consq = []
self.consq2 = []
def buy(self, buy_price):
self.buy_price = buy_price
def sell(self, sell_price):
self.balance = calculate_profit(self.buy_price, sell_price, self.balance)
self.buy_price = 0
file = "./data/BTCUSDT_2015-01-01T00_00_00+00_00__2024-10-10T23_59_59+00_00.csv"
df = pd.read_csv(file)
root = tk.Tk()
root.title("TradEvolver - Evolution Tracker")
def on_close():
if messagebox.askyesno("Exit Confirmation", "Are you sure that you want to close the program?"):
root.quit()
sys.exit()
root.protocol("WM_DELETE_WINDOW", on_close)
traders = []
for i in range(10):
traders.append(Trader())
close_data = []
root.rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)
fig, ax = plt.subplots()
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.get_tk_widget().grid(row=0, column=0, sticky="nsew")
balance_frame = tk.Frame(root)
balance_frame.grid(row=1, column=0, sticky="nsew")
balance_labels = []
def update_plot():
for close in df['close']:
close_data.append(close)
ax.clear()
ax.plot(close_data, color='blue')
ax.set_title('TradEvolver - Evolution Tracker')
ax.set_xlabel('Time')
ax.set_ylabel('Close Price')
for i, trader in enumerate(traders):
action = random.choice(['buy', 'sell', 'hold'])
if action == 'buy' and trader.buy_price == 0 :
trader.buy(close)
elif action == 'sell' and trader.buy_price != 0:
trader.sell(close)
balance_labels[i].config(text=f"Balance: {trader.balance:.2f}")
canvas.draw()
#time.sleep(0.01)
def start_plotting():
plot_thread = threading.Thread(target=update_plot)
plot_thread.start()
root.mainloop()
def main(genomes, config):
nets = []
ge = []
traders = []
for _, g in genomes:
net = neat.nn.FeedForwardNetwork.create(g, config)
nets.append(net)
traders.append(Trader())
g.fitness = 0
ge.append(g)
for trader in traders:
label = tk.Label(balance_frame, text=f"Balance: {trader.balance:.2f}")
label.pack()
balance_labels.append(label)
for x, close in enumerate(df['close']):
close_data.append(close)
ax.clear()
ax.plot(close_data, color='blue')
ax.set_title('TradEvolver - Evolution Tracker')
ax.set_xlabel('Time')
ax.set_ylabel('Close Price')
canvas.draw()
if x > 7 and len(traders) > 0:
for i, trader in enumerate(traders):
istooconseq = False
ehe = 1 if close > df["close"][x - 7] else 0
output = nets[i].activate((close, trader.buy_price, df["close"][x] - df["open"][x], df["volume"][x], ehe))
action = output.index(max(output))
if action == 0 and trader.buy_price == 0: # Buy
trader.buy(close)
trader.since_last_transaction = 0
trader.consq2.append(0)
elif action == 1: #Hold
trader.since_last_transaction += 1
trader.consq2.append(0)
elif action == 2 and trader.buy_price != 0: # Sell
trader.since_last_transaction = 0
trader.sell(close)
trader.consq2.append(1)
else:
trader.consq2.append(0)
trader.since_last_transaction += 1
if trader.buy_price != 0:
ge[i].fitness += ((df["close"] - df["open"])/df["open"])*100
max_index, max_trader = max(enumerate(traders), key=lambda x: x[1].balance)
#print(max_trader.balance, max_trader.since_last_transaction, max_trader.buy_price)
if x > 150:
trader.consq = trader.consq2[x-150:x]
istooconseq = trader.consq.count(1) > 60
#print(trader.consq.count(1))
if trader.since_last_transaction > 250 or trader.balance < 5500 or istooconseq:
traders.pop(i)
ge.pop(i)
nets.pop(i)
if trader.balance < 5500:
print("too low balance", i, trader.balance)
elif trader.since_last_transaction > 250:
print("not making trades", i, trader.since_last_transaction)
elif istooconseq:
print("too consequtive", i, trader.consq.count(1))
istooconseq = False
# balance_labels[i].config(text=f"Balance: {trader.balance:.2f}")
canvas.draw()
if len(traders) == 0:
break
def run(config_path):
config = neat.config.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, config_path)
p = neat.Population(config)
p.add_reporter(neat.StdOutReporter(True))
stats = neat.StatisticsReporter()
p.add_reporter(stats)
winner = p.run(main, 1000)
run("./config.txt")
Код: Выделить всё
[NEAT]
fitness_criterion = max
fitness_threshold = 2000
pop_size = 200
reset_on_extinction = True
[DefaultGenome]
# Diverse activation functions and higher mutation rates
activation_default = tanh
activation_mutate_rate = 0.3
activation_options = tanh, relu, sigmoid, swish
# Aggregation to allow more advanced behaviors
aggregation_default = sum
aggregation_mutate_rate = 0.1
aggregation_options = sum, avg, max
# Expanded bias ranges with increased mutation flexibility
bias_init_mean = 0.0
bias_init_stdev = 2.0
bias_max_value = 20.0
bias_min_value = -20.0
bias_mutate_power = 1.0
bias_mutate_rate = 0.8
bias_replace_rate = 0.3
# Stronger speciation configuration for improved species differentiation
compatibility_disjoint_coefficient = 1.2
compatibility_weight_coefficient = 0.7
# Add/remove connections for deeper exploration of connectivity
conn_add_prob = 0.5
conn_delete_prob = 0.3
# Network depth increased with initial dense connections
enabled_default = True
enabled_mutate_rate = 0.05
feed_forward = True
initial_connection = full_direct
# Higher node mutation probability and additional hidden layers
node_add_prob = 0.3
node_delete_prob = 0.1
# Input-output configuration with additional hidden units for more complex trading actions
num_hidden = 5
num_inputs = 5
num_outputs = 3
# Expanded range for node responses to adapt well to diverse scenarios
response_init_mean = 1.0
response_init_stdev = 0.5
response_max_value = 10.0
response_min_value = -10.0
response_mutate_power = 0.7
response_mutate_rate = 0.5
response_replace_rate = 0.2
# Wider range of connection weights and higher mutation rates for flexibility
weight_init_mean = 0.0
weight_init_stdev = 1.5
weight_max_value = 15.0
weight_min_value = -15.0
weight_mutate_power = 1.0
weight_mutate_rate = 0.8
weight_replace_rate = 0.3
[DefaultSpeciesSet]
compatibility_threshold = 3.5
[DefaultStagnation]
species_fitness_func = max
max_stagnation = 30
species_elitism = 3
[DefaultReproduction]
elitism = 2
survival_threshold = 0.3
Код: Выделить всё
import pygame
import random
import neat
import os
import time
pygame.font.init()
WIN_WIDTH = 500
WIN_HEIGHT = 800
BIRD_IMGS = [pygame.transform.scale2x(pygame.image.load(os.path.join("imgs", "bird1.png"))), pygame.transform.scale2x(pygame.image.load(os.path.join("imgs", "bird2.png"))), pygame.transform.scale2x(pygame.image.load(os.path.join("imgs", "bird3.png")))]
PIPE_IMG = pygame.transform.scale2x(pygame.image.load(os.path.join("imgs", "pipe.png")))
BASE_IMG = pygame.transform.scale2x(pygame.image.load(os.path.join("imgs", "base.png")))
BG_IMG = pygame.transform.scale2x(pygame.image.load(os.path.join("imgs", "bg.png")))
STAT_FONT = pygame.font.SysFont("comicsans", 50)
class Bird:
IMGS = BIRD_IMGS
MAX_ROTATION = 25
ROT_VEL = 20
ANIMATION_TIME = 5
def __init__(self, x, y):
self.x = x
self.y = y
self.tilt = 0
self.tick_count = 0
self.vel = 0
self.height = self.y
self.img_count = 0
self.img = self.IMGS[0]
def jump(self):
self.vel = -10.5
self.tick_count = 0
self.height = self.y
def move(self):
self.tick_count += 1
d = self.vel*self.tick_count + 1.5*self.tick_count**2
if d >= 16:
d = 16
if d < 0:
d -= 2
self.y = self.y + d
if d < 0 or self.y < self.height + 50:
if self.tilt < self.MAX_ROTATION:
self.tilt = self.MAX_ROTATION
else:
if self.tilt > -90:
self.tilt -= self.ROT_VEL
def draw(self, win):
self.img_count += 1
if self.img_count < self.ANIMATION_TIME:
self.img = self.IMGS[0]
elif self.img_count < self.ANIMATION_TIME*2:
self.img = self.IMGS[1]
elif self.img_count < self.ANIMATION_TIME*3:
self.img = self.IMGS[2]
elif self.img_count < self.ANIMATION_TIME*4:
self.img = self.IMGS[1]
elif self.img_count == self.ANIMATION_TIME*4 + 1:
self.img = self.IMGS[0]
self.img_count = 0
if self.tilt 0:
if len(pipes) > 1 and birds[0].x > pipes[0].x + pipes[0].PIPE_TOP.get_width():
pipe_ind = 1
else:
run = False
break
for x, bird in enumerate(birds):
bird.move()
ge[x].fitness += 0.1
output = nets[x].activate((bird.y, abs(bird.y - pipes[pipe_ind].height), abs(bird.y - pipes[pipe_ind].bottom)))
if output[0] > 0.5:
bird.jump()
#bird.move()
rem = []
add_pipe = False
for pipe in pipes:
for x, bird in enumerate(birds):
if pipe.collide(bird):
ge[x].fitness -= 1
birds.pop(x)
nets.pop(x)
ge.pop(x)
if not pipe.passed and pipe.x < bird.x:
pipe.passed = True
add_pipe = True
if pipe.x + pipe.PIPE_TOP.get_width() < 0:
rem.append(pipe)
pipe.move()
if add_pipe:
score += 1
for g in ge:
g.fitness += 5
pipes.append(Pipe(600))
for r in rem:
pipes.remove(r)
for x, bird in enumerate(birds):
if bird.y + bird.img.get_height() >= 730 or bird.y < 0:
birds.pop(x)
nets.pop(x)
ge.pop(x)
base.move()
draw_window(win, birds, pipes, base, score)
def run(config_path):
config = neat.config.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, config_path)
p = neat.Population(config)
p.add_reporter(neat.StdOutReporter(True))
stats = neat.StatisticsReporter()
p.add_reporter(stats)
winner = p.run(main, 50)
if __name__ == "__main__":
local_dir = os.path.dirname(__file__)
config_path = os.path.join(local_dir, "config.txt")
run(config_path)
Подробнее здесь: https://stackoverflow.com/questions/791 ... population