Python Q-Learning人工智能是&x27;不容易识别的模式

Python Q-Learning人工智能是&x27;不容易识别的模式,python,machine-learning,artificial-intelligence,q-learning,Python,Machine Learning,Artificial Intelligence,Q Learning,我有一个Q-Learning程序,试图预测我的股票模拟市场,股票的价格会上涨1-2-3-1-2-3 我已经试着调试这几天了,但就是不能得到它。我甚至完全从零开始,问题依然存在。如果你有多余的时间,我只需要多看一眼 getStock()函数用于模拟股票价格 函数的作用是:获取股票并将其转换成一个put of[股票最后一次上涨还是下跌,连续下跌/上涨多少次,股票连续上涨/下跌多少次] readAI()函数只读取给定输入时应该发生的事情 函数的作用是:检查之前的猜测,并根据猜测是否正确来更改polic

我有一个Q-Learning程序,试图预测我的股票模拟市场,股票的价格会上涨1-2-3-1-2-3

我已经试着调试这几天了,但就是不能得到它。我甚至完全从零开始,问题依然存在。如果你有多余的时间,我只需要多看一眼

getStock()函数用于模拟股票价格

函数的作用是:获取股票并将其转换成一个put of[股票最后一次上涨还是下跌,连续下跌/上涨多少次,股票连续上涨/下跌多少次]

readAI()函数只读取给定输入时应该发生的事情

函数的作用是:检查之前的猜测,并根据猜测是否正确来更改policyGradient

非常感谢你

import requests
import sys
import time

# Constants
learningRate = 0.5
stocksToBuy = 250
discountFactor = 0.5

# Variables declared:

# getStock()
currentStockPrice = 0
pastStockPrice = 0

# reducePricesToBinary()
binaryVersionOfPrices = ""

# Ai()
AI = dict()

# convertBinaryToInputs()
inputsForAI = [0,0,0]

# Ai
guess = 0
oldGuess = 0
reward = 0
pastInputsForAI = ['0',0,0]
firstTurnOver = False

# Buying and Selling stocks
money = 1000000
shares = 0

#
countToSaveEveryFifteen = 0

# Saving anything to a file.
def save(name, data):
    with open(name, 'w') as f:
        f.write(str(data))

def saveEverything():
    save("AI", AI)
    save("binaryStockPrices", binaryVersionOfPrices)
    save("money", money)
    save("shares", shares)

# Runs after an error.
def onExit():
    saveEverything()
    sys.exit()

# Prints and saves an error log if a function crashes.
def crashProgram(errorMessage):
    print(errorMessage)
    with open("crashLogs", 'w') as f:
        f.write("{}\n\n".format(errorMessage))
    onExit()

# Runs a function with try catches to catch an error.
def doFunction(function):
    try:
        function()
    except Exception, e:
        crashProgram("Fatal error running {}().\n{}".format(function.__name__, e))

# Gets the current stock value.
#def getStock():
#    global currentStockPrice
#    res = requests.get("https://markets.businessinsider.com/stocks/aapl-stock")
#    stockCostString = ""
#    for x in range (9):
#        stockCostString += res.text[res.text.find('"price": "')+10 + x]
#    currentStockPrice = float(stockCostString)
#    print(currentStockPrice)

def getStock():
    global currentStockPrice
    currentStockPrice = 1 if currentStockPrice == 3 else (2 if currentStockPrice == 1 else 3)

# Turns the prices into 0's and 1's.
def reducePricesToBinary():
    global pastStockPrice
    global binaryVersionOfPrices
    binaryString = "1" if currentStockPrice > pastStockPrice else "0" if currentStockPrice < pastStockPrice else ""
    binaryVersionOfPrices += binaryString
    pastStockPrice = currentStockPrice

# Converts the binaryStockPrices to inputs for the AI.
def convertBinaryToInputs():
    global inputsForAI
    inputsForAI[0] = binaryVersionOfPrices[len(binaryVersionOfPrices)-1]
    counterOfFirstNumber = 1
    counterOfSecondNumber = 1
    while(binaryVersionOfPrices[len(binaryVersionOfPrices) - counterOfFirstNumber] == inputsForAI[0]):
        counterOfFirstNumber+=1
    counterOfFirstNumber-=1
    while(binaryVersionOfPrices[len(binaryVersionOfPrices) - counterOfFirstNumber - counterOfSecondNumber]!=inputsForAI[0]):
        counterOfSecondNumber += 1
    counterOfSecondNumber-=1
    inputsForAI[0] = binaryVersionOfPrices[len(binaryVersionOfPrices)-1]
    inputsForAI[1] = counterOfFirstNumber
    inputsForAI[2] = counterOfSecondNumber


# AI functions
def readAI():
    global guess
    try:
        AIGuess = AI[inputsForAI[0], inputsForAI[1], inputsForAI[2]]
    except:
        AI[inputsForAI[0], inputsForAI[1], inputsForAI[2]] = 0.5
        AIGuess = 0.5
    guess = AIGuess
    print("GUESS: {}".format(guess))
    print("INPUTS: {}".format(inputsForAI))
    return guess

def checkGuess():
    global firstTurnOver
    if(firstTurnOver):
        global oldGuess
        global reward
        global pastInputsForAI
        oldGuess = 0 if oldGuess == -1 else 1
        print("Old guess: " + str(oldGuess) + " Input: " + str(int(round(float(inputsForAI[0])))))
        reward = 1 if oldGuess == int(round(float(inputsForAI[0]))) else -1
        AI[pastInputsForAI[0], pastInputsForAI[1], pastInputsForAI[2]] = (1-learningRate) * AI[pastInputsForAI[0], pastInputsForAI[1], pastInputsForAI[2]] + learningRate * (reward + discountFactor * 1)
        oldGuess = int(round(float(guess)))
    pastInputsForAI = inputsForAI
    firstTurnOver = True

def buySellStocks():
    global money
    global shares
    oldStocks = shares
    if(guess > 0):
        while(money > currentStockPrice and (shares - oldStocks) < stocksToBuy * guess):
            money -= currentStockPrice
            shares += 1
    else:
        while(shares > 0 and (oldStocks - shares) > stocksToBuy * guess):
            money += currentStockPrice
            shares -= 1

# Loads the binaryVersionOfPrices from a file.
def loadBinaryPrices():
    global binaryVersionOfPrices
    with open("binaryStockPrices", 'r') as f:
        binaryVersionOfPrices = f.read()

def loadMoney():
    global money
    with open("money", 'r') as f:
        money = int(f.read())

def loadShares():
    global shares
    with open("shares", 'r') as f:
        shares = int(f.read())

# Loads the AI from a file.
def loadAI():
    global AI
    with open("AI", 'r') as f:
        AI = eval(f.read())

#Prints relative information
def printStuff():
    print("Stock price: {}\nCurrent balance: {}\nCurrent shares: {}\nTotal value: {}\nGuess: {}\n".format(currentStockPrice, money, shares, money + shares * currentStockPrice, guess))

# Loads all variables from files.
def onProgramStart():
    doFunction(loadAI)
    doFunction(loadBinaryPrices)
    doFunction(loadMoney)
    doFunction(loadShares)

# Saves every 15 checks
def saveEveryFifteen():
    global countToSaveEveryFifteen
    countToSaveEveryFifteen += 1
    if(countToSaveEveryFifteen == 15):
        saveEverything()
        countToSaveEveryFifteen = 0

# Runs all functions.
def doAllFunctions():
    doFunction(reducePricesToBinary)
    doFunction(convertBinaryToInputs)
    doFunction(readAI)
    doFunction(checkGuess)
    doFunction(buySellStocks)
    doFunction(saveEveryFifteen)
    doFunction(printStuff)
    doFunction(getStock)

# Loads variables from files.
onProgramStart()

# Repeats the process.
while(1):
    doAllFunctions()
    time.sleep(0.5)
导入请求
导入系统
导入时间
#常数
学习率=0.5
stocksToBuy=250
折扣系数=0.5
#声明的变量:
#getStock()
当前股票价格=0
股票价格=0
#reducePricesToBinary()
binaryVersionOfPrices=“”
#Ai()
AI=dict()
#convertBinaryToInputs()
InputsFrai=[0,0,0]
#哎
猜测=0
oldGuess=0
奖励=0
pastInputsForAI=['0',0,0]
第一营业额=假
#买卖股票
货币=1000000
股票=0
#
CountToSaveEveryFine=0
#将任何内容保存到文件中。
def保存(名称、数据):
将open(名称“w”)作为f:
f、 写入(str(数据))
def saveEverything():
保存(“AI”,AI)
保存(“二进制股票价格”,二进制版本定价)
节省(“钱”,金钱)
保存(“股份”,股份)
#在发生错误后运行。
def onExit():
保存一切
sys.exit()
#如果函数崩溃,打印并保存错误日志。
def crashProgram(错误消息):
打印(错误消息)
将open(“crashLogs”,“w”)作为f:
f、 写入(“{}\n\n.”格式(errorMessage))
onExit()
#使用try-catch运行函数以捕获错误。
def DOF功能(功能):
尝试:
函数()
除例外情况外,e:
crashProgram(“运行{}().\n{}时发生致命错误”。格式(函数名)
#获取当前股票价值。
#def getStock():
#全球当前股价
#res=requests.get(“https://markets.businessinsider.com/stocks/aapl-stock")
#stockCostString=“”
#对于范围(9)内的x:
#stockCostString+=res.text[res.text.find(“‘价格’:”)+10+x]
#currentStockPrice=浮动(stockCostString)
#打印(当前股票价格)
def getStock():
全球当前股价
如果currentStockPrice==3 else,则currentStockPrice=1(如果currentStockPrice==1 else 3,则为2)
#将价格转换为0和1。
def REDUCTEPRICESTOBINARY():
全球股票价格
全局二进制版本
binaryString=“1”如果currentStockPrice>pastStockPrice else“0”如果currentStockPrice0):
而(货币>当前股票价格和(股票-旧股票)0和(旧股票-股票)>股票买入*猜测):
货币+=当前股票价格
股份-=1
#从文件加载BinaryVersionOfPrice。
def loadBinaryPrices():
全局二进制版本
以开放(“二进制股票价格”和“r”)作为f:
binaryVersionOfPrices=f.read()
def loadMoney():
全球货币
以开放式(“货币”,“r”)作为f:
money=int(f.read())
def loadShares():
全球份额
以未平仓(“股份”和“r”)作为f:
shares=int(f.read())
#从文件加载AI。
def loadAI():
全球人工智能
以开放(“AI”,“r”)作为f:
AI=eval(f.read())
#打印相关信息
def printStuff():
打印(“股票价格:{}\n当前余额:{}\n当前股票:{}\n总价值:{}\n压力:{}\n”。格式(当前股票价格,货币,股票,货币+股票*当前股票价格,猜测))
#从文件中加载所有变量。
def onProgramStart():
doFunction(loadAI)
doFunction(loadBinaryPrices)
doFunction(loadMoney)