Commit 21d198eb authored by Ahmad Reza's avatar Ahmad Reza
Browse files

New plot generator based on matplotlib. Only works for normal simulation

has to extended for multiple
parent c9294848
......@@ -40,14 +40,14 @@ class CsvAgentFile:
agent.csv_agent_writer.location_created, agent.csv_agent_writer.location_deleted,
agent.csv_agent_writer.location_read, agent.csv_agent_writer.location_write,
agent.csv_agent_writer.memory_read, agent.csv_agent_writer.memory_write,
agent.csv_agent_writer.agents_created, agent.csv_agent_writer.agentss_dropped,
agent.csv_agent_writer.agents_created, agent.csv_agent_writer.agents_dropped,
agent.csv_agent_writer.agents_deleted, agent.csv_agent_writer.agent_read,
agent.csv_agent_writer.steps, agent.csv_agent_writer.agents_taken,
agent.csv_agent_writer.agent_write,
agent.csv_agent_writer.items_created, agent.csv_agent_writer.itemss_deleted,
agent.csv_agent_writer.items_created, agent.csv_agent_writer.items_deleted,
agent.csv_agent_writer.items_dropped,
agent.csv_agent_writer.items_read, agent.csv_agent_writer.itemss_taken,
agent.csv_agent_writer.items_write, agent.csv_agent_writer.success]
agent.csv_agent_writer.item_read, agent.csv_agent_writer.items_taken,
agent.csv_agent_writer.item_write, agent.csv_agent_writer.success]
self.writer.writerow(csv_iterator)
......
import subprocess
import pandas as pd
import matplotlib.pyplot as plt
import csv
import numpy as np
import pandas as pn
import os as os
# def plot_generator(file,directory, start, x_index, name, plot_type="line"):
# with open(directory+"/"+file, 'r') as data:
# plotter(data, directory, start, x_index, name, plot_type)
def plot_generator(directory, plot_dir, multiple=0):
with open(directory+"/"+"rounds.csv", 'r') as data:
plotter(data, "rounds", 4, 5, plot_dir+"/rounds")
with open(directory + "/" + "agent.csv", 'r') as data:
plotter(data, "agents", 1, 2, plot_dir+"/agents")
def plot_generator(directory):
data = pd.read_csv(directory+"/rounds.csv")
i = 1
plot = subprocess.Popen(['gnuplot', '--persist'], shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
universal_newlines=True)
plot.stdin.write("set datafile separator ',' \n")
plot.stdin.write('set xlabel "Rounds" \n')
for bla in data.columns:
if i >= 5:
plot.stdin.write('set ylabel "%s" \n' % (bla))
plot.stdin.write("set outputs '" + directory + "/rounds_%s.png' \n" % (bla))
plot.stdin.write("set term png giant size 800,600 font 'Helvetica,20' \n")
plot.stdin.write(
"plot '" + directory + "/rounds.csv' using 2:" + str(i) + " title '" + bla + "' with lines axis x1y1 smooth unique \n")
plot.stdin.write("set terminal pdf monochrome font 'Helvetica,10' \n")
plot.stdin.write("set outputs '" + directory + "/rounds_%s.pdf' \n" % (bla))
plot.stdin.write("replot \n")
i += 1
data = pd.read_csv(directory+"/agent.csv")
i = 1
plot = subprocess.Popen(['gnuplot', '--persist'], shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
universal_newlines=True)
plot.stdin.write("set datafile separator ',' \n")
plot.stdin.write('set xlabel "Agent" \n')
for bla in data.columns:
if i >= 3 :
plot.stdin.write('set ylabel "%s" \n' % (bla))
plot.stdin.write("set term png giant size 800,600 font 'Helvetica,20' \n")
plot.stdin.write("set outputs '" + directory + "/agent_%s.png' \n" % (bla))
plot.stdin.write(
"plot '" + directory + "/agent.csv' using 2:" + str(
i) + " title '" + bla + "' with lines axis x1y1 smooth unique \n")
plot.stdin.write("set terminal pdf monochrome font 'Helvetica,10' \n")
plot.stdin.write("set outputs '" + directory + "/agent_%s.pdf' \n" % (bla))
plot.stdin.write("replot \n")
i += 1
plot.stdin.write('quit\n')
def plotter(data, name, x_index, y_start, plot_dir):
if not os.path.exists(plot_dir):
os.makedirs(plot_dir)
csv_object = csv.reader(data, delimiter=',')
a = next(csv_object)
plot_type = "line"
x = []
y = []
plt.figure(figsize=(20, 12))
for col in range(y_start, len(a)):
x.clear()
y.clear()
for row in csv_object:
if plot_type == "line":
x.append(int(row[x_index]))
elif plot_type == "bar":
x.append(str(row[x_index]))
if row[col] != "nan":
y.append(int(float(row[col])))
else:
y.append(np.nan)
if plot_type == "line":
plt.plot(x, y)
elif plot_type == "bar":
plt.bar(x, y, align='edge', width=0.5)
plt.xlabel(a[x_index])
plt.xticks(rotation=45)
plt.ylabel(a[col])
plt.savefig(plot_dir + '/' + name + '_' + a[col] + '.png')
plt.clf()
data.seek(0)
plot = csv.reader(data, delimiter=',')
next(plot)
#plot_generator("all_aggregates.csv", "../outputs/multiple/working_multi_layer_2020-02-29_14:34:1_leader_coating", 4,0, "Multi_Layer: 60 Particles", "bar")
\ No newline at end of file
"""
A world is created that has two particles, two markers, and two tiles.
"""
from lib.swarm_sim_header import get_multiple_steps_in_direction, get_coordinates_in_direction
def scenario(world):
world.add_agent((0.0,0.0,0.0))
world.add_item((1.0,0.0,0.0))
world.add_location((2.0,0.0,0.0))
[Simulator]
## Seedvalue for the same random sequence
## Different number creates a different random sequence
seed_value = 12
seed_value = 5
## Maximum round number in swarm-world, 0 = infinite
max_round = 1000
max_round = 10
## 1/True = Call of agents in random order
## 0/False = Call of agents in added order in scenario
......
......@@ -65,7 +65,7 @@ class World:
self.csv_round = self.csv_generator_module.CsvRoundData(scenario=config_data.scenario,
solution=config_data.solution,
seed=config_data.seed_value,
directory=config_data.directory_name)
directory=config_data.directory_csv)
if config_data.visualization:
self.vis = vis3d.Visualization(self)
......@@ -185,7 +185,7 @@ class World:
def csv_aggregator(self):
self.csv_round.aggregate_metrics()
agent_csv = self.csv_generator_module.CsvAgentFile(self.config_data.directory_name)
agent_csv = self.csv_generator_module.CsvAgentFile(self.config_data.directory_csv)
for a in self.agents:
agent_csv.write_agent(a)
agent_csv.csv_file.close()
......
......@@ -102,14 +102,18 @@ def create_directory_for_data(config_data, unique_descriptor):
config_data.directory_name = "%s/%s" % (unique_descriptor, str(config_data.seed_value))
config_data.directory_name = "./outputs/csv/mulitple/" + config_data.directory_name
config_data.directory_plot = "./outputs/plot/mulitple/" + config_data.directory_name
else:
config_data.directory_name = "%s_%s" % (unique_descriptor, str(config_data.seed_value))
config_data.directory_name = "./outputs/csv/" + config_data.directory_name
if not os.path.exists(config_data.directory_name):
os.makedirs(config_data.directory_name)
config_data.directory_csv = "./outputs/csv/" + config_data.directory_name
config_data.directory_plot = "./outputs/plot/" + config_data.directory_name
if not os.path.exists(config_data.directory_csv):
os.makedirs(config_data.directory_csv)
if not os.path.exists(config_data.directory_plot):
os.makedirs(config_data.directory_plot)
def run_solution(swarm_sim_world):
if swarm_sim_world.config_data.agent_random_order_always:
......@@ -130,7 +134,7 @@ def get_scenario(config_data):
def generate_data(config_data, swarm_sim_world):
swarm_sim_world.csv_aggregator()
plt_gnrtr = importlib.import_module('components.generators.plot.%s' % config_data.plot_generator)
plt_gnrtr.plot_generator(config_data.directory_name)
plt_gnrtr.plot_generator(config_data.directory_csv, config_data.directory_plot )
if __name__ == "__main__":
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment