This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ### |