Skip to content

Instantly share code, notes, and snippets.

@bricef
Created May 8, 2010 12:35
Show Gist options
  • Save bricef/394543 to your computer and use it in GitHub Desktop.
Save bricef/394543 to your computer and use it in GitHub Desktop.
#! /usr/bin/env python
import xml.etree.ElementTree as ET
import Image
import ImageDraw
import numpy as np
import sys, re, os
files = os.listdir(sys.argv[1])
files.sort()
regex = re.compile("[0-9]+\.xml")
for afile in files:
if regex.match(afile): #IE: we have xml, and numeric name
im = Image.new("RGB", (560,560), (0,0,0))
pix=im.load()
draw=ImageDraw.Draw(im)
print("== Processing "+afile+" -> "+afile[:-4]+".png")
fullpath=os.path.join(sys.argv[1], afile)
tree=ET.parse(fullpath)
agents=tree.findall("xagent")
#reinit the arrays
ANTx = [] #ants
ANTy = []
PHEx = [] #pheromones
PHEy = []
PHEl = []
NESx = [] #nest
NESy = []
FOOx = [] #food
FOOy = []
for agent in agents:
type = agent.find("name").text
if type == "Pheromone":
X = agent.find("pheromoneX")
Y = agent.find("pheromoneY")
L = agent.find("life")
PHEx.append(int(float(X.text)))
PHEy.append(int(float(Y.text)))
PHEl.append(float(L.text))
elif type == "Nest":
X = agent.find("nestX")
Y = agent.find("nestY")
NESx.append(int(float(X.text)))
NESy.append(int(float(Y.text)))
elif type == "Food":
X = agent.find("foodX")
Y = agent.find("foodY")
FOOx.append(int(float(X.text)))
FOOy.append(int(float(Y.text)))
elif type == "Ant":
X = agent.find("antX")
Y = agent.find("antY")
ANTx.append(int(float(X.text)))
ANTy.append(int(float(Y.text)))
#PHEROMONE
if PHEl:
max_life = np.max(PHEl)
PHEa=[]
for life in PHEl:
if life > 0:
PHEa.append(life/max_life)
else:
PHEa.append(0)
#NES
for x,y in zip(NESx,NESy):
s=5
draw.ellipse([((x*2)-s,(y*2)-s),((x*2)+s,(y*2)+s)] ,(255,255,255))
#PHE
for x,y,alpha in zip(PHEx,PHEy,PHEa):
s=1
draw.ellipse([((x*2)-s,(y*2)-s),((x*2)+s,(y*2)+s)] ,(int(alpha*255),0,0,))
#ANT
for x,y in zip(ANTx,ANTy):
s=2
draw.ellipse([((x*2)-s,(y*2)-s),((x*2)+s,(y*2)+s)] ,(0,255,0))
#FOO
for x,y in zip(FOOx,FOOy):
s=4
draw.ellipse([((x*2)-s,(y*2)-s),((x*2)+s,(y*2)+s)] ,(0,0,255))
im.save(os.path.join(sys.argv[2],afile[:-4]+".png"))
#! /bin/bash
##
# Warning: this script isn't safe. it doesn't check before deleting/writing.
# Make sure you understand it before running it, or you may lose work.
#
##
option=$1
arg=$2
ITER=100
XPARSER_LOC=/path/to/xparser
MODEL_DIR=/path/to/antModel/
MODEL_XML=/path/to/antModel/antModel.xml
WORKING_DIR=/path/to/working/
FRAME_MAKER=/path/to/make_frames2.py
MBOARD_DIR=/path/to/libmboard
function usage {
echo "util.sh (option)
where option is one of:
--cleanbuild Cleans the Xparser generated files from the model
directory
--make-movie Makes a movie from the generated xml files.
--run [num] Runs the simulation. Must specify a number of
iterations to run.
--frames Creates the frames from the xml files without
making the movie
--movie Makes the movie without first generating the frames
--build Build an executable from the model.
--cleanrun Cleans generated .xml and .png files
--do-all [num] Does everything at once, cleans, builds, runs the
simulation and then makes a movie.
-h --help Shows this usage message
"
}
function movie-only {
echo "== Making Movie from existing frames"
#move to where they are
cd $WORKING_DIR
#make ordered list
ls -1 | grep -E "[0-9]+\.png"|sort -n > frames.txt
#encode into a movie
mencoder 'mf://@frames.txt' -mf type=png:fps=5 -ovc lavc -lavcopts vcodec=wmv2 -oac copy -o movie.avi
}
function frames-only {
echo "== Making frames"
#make the frames
$FRAME_MAKER $WORKING_DIR $WORKING_DIR
}
function make-movie {
echo "== Making frames and encodeing movie"
frames-only
movie-only
}
function cleanrun {
echo "== Cleaning Run data"
cd $WORKING_DIR
find ./ -name "[0-9]*.xml" ! -name "0.xml" -exec rm {} \;
find ./ -name "[0-9]*.png" -exec rm {} \;
rm frames.txt
}
function build {
echo "== Building executable"
$XPARSER_LOC $MODEL_XML
cd $MODEL_DIR
pwd
make LIBMBOARD_DIR=$MBOARD_DIR
}
function cleanbuild {
echo "== Cleaning Build Files"
cd $MODEL_DIR
ls -1 > ALLFILES
sort MANIFEST MANIFEST ALLFILES| uniq -u | xargs rm
}
function runsim {
echo "== Running simulation"
cd $MODEL_DIR
cp 0.xml $WORKING_DIR
echo $arg
if [[ $arg ]]; then
./main $arg ${WORKING_DIR}0.xml
else
usage
exit
fi
}
case "$1" in
"--cleanbuild")
cleanbuild
;;
"--frames")
frames-only
;;
"--make-movie")
make-movie
;;
"--movie")
movie-only
;;
"--run")
runsim
;;
"--build")
build
;;
"--cleanrun")
cleanrun
;;
"--do-all")
cleanrun
cleanbuild
build
runsim
make-movie
;;
"-h"|"--help")
usage
;;
*)
usage
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment