当前位置: 动力学知识库 > 问答 > 编程问答 >

python - Genetic Algorithm chromedriver snake

问题描述:

So I'm almost done completing a genetic algorithm for the snake game in python for this javascript snake game: http://patorjk.com/games/snake/

I've been getting an error for my code:

import selenium, os, urllib

from selenium import webdriver

import win32com.client as comctl

from random import randint

from operator import add

import time

import random

import lxml

from lxml import html

import requests

page=requests.get("http://patorjk.com/games/snake/")

tree=lxml.html.fromstring(page.content)

chromedriver="C:/Python34/Scripts/chromedriver"

os.environ["webdriver.chrome.driver"] = chromedriver

wsh = comctl.Dispatch("WScript.Shell")

wsh.AppActivate("patorjk.com/games/snake/")

driver = webdriver.Chrome(executable_path='C:\Program Files (x86)\Google\Chrome\Application\chrome.exe')

driver.get('http://patorjk.com/games/snake/')

hidden_element = driver.find_element_by_id('sbTryAgain0')

def createIndividual(length, mintim, maxtim):

return [[random.uniform(mintim,maxtim) for x in range(length)],[randint(0,3) for x in range(length)],[]]

def changeInt(int1):

if(int1==0):

return '{UP}'

if(int1==1):

return '{RIGHT}'

if(int1==2):

return '{DOWN}'

if(int1==3):

return '{LEFT}'

def run(ind):

for x in range(len(ind[0])):

if hidden_element.is_displayed():

break

wsh.SendKeys(" ")

time.sleep(ind[0][x])

wsh.SendKeys(changeInt(ind[1][x]))

print(changeInt(ind[1][x]))

def indfit(time, pts):

tree=lxml.html.fromstring(page.content)

points=tree.xpath('//div[@class="snake-panel-component"]/text()')

for i in points:

if(points[i-1]==" " and points[i-2]==":"):

pts=int(points[i:len(points)-1])

#return (pts/time)

return pts

def createPopulation(size, length, mintim, maxtim):

population=[]

for x in range(size):

population.append(createIndividual(length, mintim, maxtim))

return population

def cyclePop(pop):

index1=0

for x in pop:

start_time=time.time()

pop1=pop[index1]

run(pop1)

pop[index1][2]=indfit(time.time()-start_time,1)

print("Fitness of individual ",(index1+1),": ",pop[index1][2])

wsh.SendKeys(" ")

index1+=1

print("Population Fitness: ",popFitScore(pop))

def popFitScore(pop):

sum1=0

for x in pop:

sum1+=x[2]

return sum1/len(pop)

def bubble_sort(items):

""" Implementation of bubble sort """

for i in range(len(items)):

for j in range(len(items)-1-i):

if items[j][2] > items[j+1][2]:

items[j], items[j+1] = items[j+1], items[j] # Swap!

def mixtraitscross(ind1,ind2):

index1=0

for i in ind1:

if index1%2==1:

ind1[index1]=ind2[index1]

index1+=1

def mixtraitsavg(ind1,ind2):

index1=0

for i in ind1:

if index1%2==1:

ind1[index1]=(ind1[index1]+ind2[index1])/2

index1+=1

def evolve(pop,mutrate,killpercent,sammin,sammax):

bubble_sort(pop)

startmutlen=(len(pop)-(killpercent*len(pop)))

mutrateind=mutrate*len(pop[0][0])

for ig in pop:

index1=pop.index(ig)

if index1>startmutlen:

for g in range(int(mutrateind)):

ig[0][randint(0,len(pop[0][0])-1)]=randint(0,3)

ig[1][randint(0,len(pop[0][0])-1)]=random.uniform(sammin,sammax)

else:

for iff in range(int(startmutlen-1)):

mixtraitscross(pop[index1][0],pop[index1+1][0])

mixtraitsavg(pop[index1][1],pop[index1+1][1])

#createPopulation(10, 20, 0, 5)

index8=0

while(index8<=50):

x=createPopulation(5,10,0,2)

cyclePop(x)

evolve(x,.30,.5,0,2)

index8+=1

so this is my code and i'm trying to make it so that if the element that says "You Died :(", it will stop running the individual and it will apply the time to the individual's fitness. I'm getting really confused with all of this chromedriver stuff with selenium and if I were able to get it to work, It would be perfect. the comment that says

#return (pts/time)

is what I want to be there, but until I am able to get the selenium working, the time is the same. I made it so that the time would be the same, but I want it so that it cuts off after it dies. Right now, I'm just having errors working with chromedriver, and I'm not exactly sure it's what I even want to be using. This is my error:

Traceback (most recent call last):

File "H:\Senior Year\CSAP2\Snakegenalg\SnakeJSGenAlg.py", line 27, in <module>

driver = webdriver.Chrome(executable_path='C:\Program Files (x86)\Google\Chrome\Application\chrome.exe')

File "C:\Users\lab301-user28\AppData\Local\Continuum\Anaconda3\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 62, in __init__

self.service.start()

File "C:\Users\lab301-user28\AppData\Local\Continuum\Anaconda3\lib\site- packages\selenium\webdriver\common\service.py", line 86, in start

self.assert_process_still_running()

File "C:\Users\lab301-user28\AppData\Local\Continuum\Anaconda3\lib\site-packages\selenium\webdriver\common\service.py", line 99, in assert_process_still_running

% (self.path, return_code)

selenium.common.exceptions.WebDriverException: Message: Service C:\Program Files (x86)\Google\Chrome\Application\chrome.exe unexpectedly exited. Status code was: 0

Sorry if I'm really bad at coding and I'm getting bad at this simple stuff, I'm just in high school and learned python this month. I'm not using the shorthand for stuff right now because it confuses me, but I know my code is easy to follow

分享给朋友:
您可能感兴趣的文章:
随机阅读: