Skip to content

Instantly share code, notes, and snippets.

cartpole = CartpoleFitness(200)
timeTotal = 0
iterationTotal = 0
for i in range(1, 11): #Solve Cartpole 10 times and average results.
start = time.time()
#weightSpaceBounds numberOfParticles, inertia, initialVelocityBounds, localWeight, globalWeight
solver = ParticleSwarmOptimisation(cartpole.evaluate, (-8,8), 15, 0.5, 0.2, 2, 2)
iterationTotal += solver.maxamise(25, 195)
end = time.time() - start
print('Trail ' + str(i) + ' solved in ' + str(k) + ' iterations and ' + "{:.1f}".format(end) + ' seconds.')
class CartpoleFitness(object):
def __init__(self, terminationStep):
self.env = gym.make('CartPole-v1')
self.terminationStep = terminationStep
def probabilities(self, state, weights):
z = state.dot(weights)
exp = np.exp(z)
return exp/np.sum(exp)
class ParticleSwarmOptimisation(object):
def __init__(self, fitnessFunction, bounds, numParticles, omega, v, phiL, phiG):
self.bestGlobalFitness = -np.inf; self.bestGlobalPos = []; self.fitnessFunction = fitnessFunction; self.bounds = bounds
self.swarm = [] #Create swarm
for i in range(numParticles):
self.swarm.append(Particle(bounds, omega, v, phiL, phiG))
def maxamise(self, maxIterations, target):
bestGlobalFitness = -np.inf; bestGlobalPos = []
for i in resample:
avg = 0
for k in range(100):
state = env.reset()
policy = createPolicy(i)
step = 0
while True:
step += 1
state = observationSpace.Discretise(state[2:])
action = policy[state]
env = gym.make('CartPole-v1')
thetaHigh = 10 * 2 * np.pi / 360
high = np.array([thetaHigh, np.radians(15)])
observationSpace = DiscreteBox(-high, high, (3,3))
resample = []
for i in range(512):
state = env.reset()
policy = createPolicy(i)
step = 0
while True:
def createPolicy(id):
binary = unpackbits(np.array([id]), 9) #generate 9 long binary string
return np.reshape(binary, (3,3)) #reshape to form policy
#Credit for this function https://stackoverflow.com/a/51509307
def unpackbits(x, num_bits):
xshape = list(x.shape)
x = x.reshape([-1,1])
to_and = 2**np.arange(num_bits).reshape([1,num_bits])
return (x & to_and).astype(bool).astype(int).reshape(xshape + [num_bits])
class DiscreteBox(object):
def __init__(self, low, high, shape):
self.low, self.high, self.shape = low, high, shape
def Discretise(self, state):
discreteState = [int(np.floor((state[i] - self.low[i])/(self.high[i]-self.low[i])*(self.shape[i]-1))) for i in range(len(state))]
return tuple([np.min([self.shape[i]-1, np.max([discreteState[i], 0])]) for i in range(len(state))])
@adamprice97
adamprice97 / ShapelyBetweenness.py
Created April 22, 2020 15:27
Shapely betweenness centrality in python with networkX
import networkx as nx
import matplotlib.pyplot as plt
import colorsys
import numpy as np
import queue
#Input - G: networkX graph
#Output - cSh: array of ShapelyBetweeness of each node
def ShapelyBetweeness(self, G):
#Distance between nodes
@adamprice97
adamprice97 / Pong.py
Last active March 20, 2020 14:17
Pong
import pygame, sys
from pygame.locals import *
from Paddle import *
from SimpleBall import *
from Scorer import *
from random import randint
import numpy as np
### Credit to Will Baumbach for the code that ###
### formed the starting point of this game ###