Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
updates the automation scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
brenomfviana committed Oct 25, 2021
1 parent a7fbd1b commit 5e3793e
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 68 deletions.
52 changes: 9 additions & 43 deletions plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@
# List of indexes
difficulty = ['8-12', '12-16', '16-20', '20-24', '24-28']
# List of columns
weapons = ['Barehand', 'Sword', 'Bow', 'Bomb Thrower', 'Shield', 'Cure Spell']
weapon = ['Barehand', 'Sword', 'Bow', 'Bomb Thrower', 'Shield', 'Cure Spell']


# Convert the array into a map
def to_map(array, attribute):
shape = (len(difficulty), (len(weapons)))
shape = (len(difficulty), len(weapon))
map = np.zeros(shape)
i = 0
for b in range(len(difficulty)):
for w in range(len(weapons)):
for w in range(len(weapon)):
if array[i] is None:
map[b, w] = 0
map[b, w] = None
else:
map[b, w] = array[i][attribute]
i += 1
Expand All @@ -31,7 +31,7 @@ def to_map(array, attribute):

# Plot the heatmap
def plot_heatmap(map, folder, filename, pop, max):
df = DataFrame(map, index=difficulty, columns=weapons)
df = DataFrame(map, index=difficulty, columns=weapon)
color = sb.color_palette('viridis_r', as_cmap=True)
ax = sb.heatmap(df, vmin=0, vmax=max, annot=True, cmap=color)
ax.invert_yaxis()
Expand All @@ -46,7 +46,7 @@ def plot_charts(data, filename, folder):
# Parse file
obj = json.loads(data)

# Create the folder for the given parameters
# Create the folder for the entered parameters
filename = filename.replace('.json', '')
folder = folder + os.path.sep + filename
if os.path.isdir(folder):
Expand All @@ -64,44 +64,10 @@ def plot_charts(data, filename, folder):
plot_heatmap(f_intermediate, folder, filename, 'fitness_intermediate', max)
plot_heatmap(f_final, folder, filename, 'fitness_final', max)

# Print measurements
measure = {}

# Calculate mean fitness
measure['mean_f_initial'] = np.mean(f_initial)
measure['mean_f_intermediate'] = np.mean(f_intermediate)
measure['mean_f_final'] = np.mean(f_final)

# Calculate max fitness
measure['max_f_initial'] = np.max(f_initial)
measure['max_f_intermediate'] = np.max(f_intermediate)
measure['max_f_final'] = np.max(f_final)

# Calculate min fitness
measure['min_f_initial'] = np.min(f_initial)
measure['min_f_intermediate'] = np.min(f_intermediate)
measure['min_f_final'] = np.min(f_final)

# Calculate the standard deviation
measure['std_f_initial'] = np.std(f_initial)
measure['std_f_intermediate'] = np.std(f_intermediate)
measure['std_f_final'] = np.std(f_final)

# Serializing json
json_object = json.dumps(measure, indent = 4)

# Write JSON file
filename = folder + os.path.sep + filename + '-measure.json'

# Writing to sample.json
with open(filename, "w") as outfile:
outfile.write(json_object)


# Target folder to save the charts
# Create the folder for the charts
CHART_FOLDER = 'charts'

# Check if the folder for charts exists and create it if it does not exist
if not os.path.isdir(CHART_FOLDER):
os.mkdir(CHART_FOLDER)

Expand All @@ -115,7 +81,7 @@ def plot_charts(data, filename, folder):
param_folder += sys.argv[5] # Number of competitors


# Read all the JSON files for the given parameters
# Read all the JSON files for the entered parameters
files = []
filenames = []
for p in Path(folder + param_folder).glob('*.json'):
Expand All @@ -124,7 +90,7 @@ def plot_charts(data, filename, folder):
filenames.append(p.name)


# Create the folder for the given parameters
# Create the folder for the entered parameters
folder = CHART_FOLDER + os.path.sep + param_folder
if not os.path.isdir(folder):
os.mkdir(folder)
Expand Down
55 changes: 30 additions & 25 deletions run.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import sys
import platform
import random
import numpy as np
Expand All @@ -8,10 +9,13 @@
# --- Initialization

# Initialize the random seed
random.seed(0)
seed = random.randrange(sys.maxsize)
random.seed(seed)
print("Seed:", seed)
# random.seed(0)

# Define the number of executions of each set of parameters
executions = range(1)
executions = range(3)



Expand All @@ -24,10 +28,10 @@
populations = [25]

# Mutation rates
mutations = [30]
mutations = [10]

# Crossover rates
crossovers = [80]
# Mutation rates for genes
genemutations = [30]

# Competitors
competitors = [3]
Expand All @@ -40,7 +44,7 @@
if platform.system() == 'Linux':
executable = './bin/Debug/net5.0/publish/EnemyGenerator '
elif platform.system() == 'Windows':
executable = 'start bin\\Debug\\net5.0\\publish\\EnemyGenerator '
executable = 'bin\\Debug\\net5.0\\publish\\EnemyGenerator '
else:
print('This script is not able to run in this OS.')
exit()
Expand All @@ -50,32 +54,23 @@
os.system('dotnet publish')


def run(ge, po, mu, cr, co):
def run(ge, po, mu, gm, co):
# Generate a random seed
rs = random.randint(0, np.iinfo(np.int32).max - 1)
# Build the parameters
parameters = ""
for i in [rs, ge, po, mu, cr, co]:
for i in [rs, ge, po, mu, gm, co]:
parameters += str(i) + ' '
# Print parameters
print('Parameters=[', parameters, ']')
# Run algoritm for the current set of parameters
os.system(executable + parameters)


def plot(ge, po, mu, cr, co):
# Plot the charts for the current set of parameters
parameters = ''
for i in [ge, po, mu, cr, co]:
parameters += str(i) + ' '
os.system('python plot.py ' + parameters)


# Variables to control the experiment progress
total = len(generations) * \
len(populations) * \
len(mutations) * \
len(crossovers) * \
len(genemutations) * \
len(competitors) * \
len(executions)
i = 1
Expand All @@ -85,31 +80,41 @@ def plot(ge, po, mu, cr, co):
for ge in generations:
for po in populations:
for mu in mutations:
for cr in crossovers:
for gm in genemutations:
for co in competitors:
for e in executions:
# Run execuble
run(ge, po, mu, cr, co)
run(ge, po, mu, gm, co)
# Print progress
print("%.2f" % ((i / total) * 100))
i += 1


# Variables to control the experiment progress
# --- Plot charts of the experiment results

def plot(ge, po, mu, gm, co):
parameters = ''
for i in [ge, po, mu, gm, co]:
parameters += str(i) + ' '
os.system('python plot.py ' + parameters)

# Variables to control the plotting progress
total = len(generations) * \
len(populations) * \
len(mutations) * \
len(crossovers) * \
len(genemutations) * \
len(competitors)
i = 1

# Plot all the results
# Plot charts for all sets of parameters
print('Plotting')
for ge in generations:
for po in populations:
for mu in mutations:
for cr in crossovers:
for gm in genemutations:
for co in competitors:
# Plot charts
plot(ge, po, mu, gm, co)
# Print progress
print("%.2f" % ((i / total) * 100))
plot(ge, po, mu, cr, co)
i += 1
91 changes: 91 additions & 0 deletions stats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import os
from pathlib import Path
import json
import numpy as np
from pandas import DataFrame


# List of indexes
difficulty = ['8-12', '12-16', '16-20', '20-24', '24-28']
# List of columns
weapon = ['Barehand', 'Sword', 'Bow', 'Bomb Thrower', 'Shield', 'Cure Spell']


# Convert the list of files to a map
def to_map(array, attribute):
shape = (len(difficulty), len(weapon))
map = np.zeros(shape)
i = 0
for b in range(len(difficulty)):
for w in range(len(weapon)):
if array[i] is None:
map[b, w] = None
else:
map[b, w] = array[i][attribute]
i += 1
return map

shape = (len(difficulty), len(weapon))
mean_map = np.zeros(shape)
std_map = np.zeros(shape)

maps = []

path = 'results' + os.path.sep + '100-25-30-80-3' + os.path.sep
for p in Path(path).glob('*.json'):
with p.open() as f:
# Convert the read files into a map of fitness
# and add them to the list of maps
obj = json.loads(f.read())
maps.append(to_map(obj['final'], 'fitness'))

for map in maps:
for l in range(len(difficulty)):
for e in range(len(weapon)):
print('%3.2f' % mean_map[l, e], end=' ')
if not np.isnan(map[l, e]):
mean_map[l, e] += map[l, e]
print()
print()

mean_map = mean_map / 10

# Uncomment to debug the mean map
# for l in range(len(difficulty)):
# for e in range(len(weapon)):
# print('%3.2f' % mean_map[l, e], end=' ')
# print()

for map in maps:
for l in range(len(difficulty)):
for e in range(len(weapon)):
if not np.isnan(map[l, e]):
std_map[l, e] += pow(map[l, e] - mean_map[l, e], 2)
std_map = std_map / 10
std_map = np.sqrt(std_map)

# Uncomment to debug the std map
# for l in range(len(difficulty)):
# for e in range(len(weapon)):
# print('%3.2f' % std_map[l, e], end=' ')
# print()

# Merge the mean and std maps
fmap = [ [ '' for e in range(len(weapon)) ] for l in range(len(difficulty)) ]
for l in range(len(difficulty)):
for e in range(len(weapon)):
fmap[l][e] = '{:.2f}+-{:.2f}'.format(mean_map[l, e], std_map[l, e])

# Uncomment to debug the merged map
# for l in range(len(difficulty)):
# for e in range(len(weapon)):
# print(fmap[l][e], end=' ')
# print()

# Print the resulting table
df = DataFrame(fmap, index=difficulty, columns=weapon)
print(df)

# Uncomment to write a CSV file with the resulting table
# filename = 'std_atual.csv'
# df.to_csv(filename)

0 comments on commit 5e3793e

Please sign in to comment.