[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[oMetah-devel] ometah/experiment interface.py metahtest.py
From: |
Jean-Philippe Aumasson |
Subject: |
[oMetah-devel] ometah/experiment interface.py metahtest.py |
Date: |
Fri, 10 Jun 2005 09:03:37 -0400 |
CVSROOT: /cvsroot/ometah
Module name: ometah
Branch:
Changes by: Jean-Philippe Aumasson <address@hidden> 05/06/10 13:03:36
Modified files:
experiment : interface.py metahtest.py
Log message:
* added err attribute for Point object
* different functions for single or multi mode runs and plottings
CVSWeb URLs:
http://savannah.gnu.org/cgi-bin/viewcvs/ometah/ometah/experiment/interface.py.diff?tr1=1.3&tr2=1.4&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/ometah/ometah/experiment/metahtest.py.diff?tr1=1.14&tr2=1.15&r1=text&r2=text
Patches:
Index: ometah/experiment/interface.py
diff -u ometah/experiment/interface.py:1.3 ometah/experiment/interface.py:1.4
--- ometah/experiment/interface.py:1.3 Fri Jun 10 09:17:15 2005
+++ ometah/experiment/interface.py Fri Jun 10 13:03:35 2005
@@ -1,8 +1,9 @@
#!/usr/bin/python
# -*- coding: iso-8859-1 -*-
-
+#
+# Test module for Open Metaheuristic, interface for a test
# Author: Jean-Philippe Aumasson <address@hidden>
-
+#
# Open Metaheuristic is a Library aimed at the conception of metaheuristics
# for difficult optimization.
#
@@ -36,6 +37,7 @@
self.__temp = "."
self.__path = "."
self.__logfile = "%s.log" % (self.__defaultFileName)
+ self.__points = []
def getXmlFromExecOmetah(self, path):
""" execute ometah with given arguments,
@@ -55,16 +57,12 @@
except:
self.log('ERROR : wrong path to XML file
[Interface.getXmlFromFile]\n')
return fd
-
-
- def setAttributes():
- """ set class attributes from XML parsing """
- dimension = 1
def plotValuesIterationsDistribHist(self, plist, breaks):
""" plot distribution of points
plist = list of Point objects
breaks = number of breaks in the histogram """
+ # TODO : distrib pour une itération donnée...
vlist = []
vlist = [x.value for x in plist]
r.hist(vlist, breaks, col='green', main='Distribution', xlab='Values',
ylab='Frequency')
@@ -111,9 +109,8 @@
sdlist.append(r.sd(i))
r.plot(sdlist, col='blue', type='o', main='Standard deviations',
xlab='Iteration', ylab='Std')
-
def log(self, string):
- """ will write log of current job in a *.log file with date,
+ """ write log of current job in a *.log file with date,
pb name, output files... """
if self.LOG_ON:
logf = "%s/%s" % (self.__path, self.__logfile)
@@ -122,26 +119,22 @@
fd.close()
def setLog(self, boolean):
- self.LOG_ON = boolean
-
+ """ turn the log on or off """
+ self.LOG_ON = boolean
def setPostscriptOutput(self, filename="default"):
""" set a postscript output file """
if filename == "default":
- # s = "%s/%s" % (self.__path,
self.datedFileName(self.__defaultFileName, '.ps'))
s = "%s/%s.ps" % (self.__path, self.__defaultFileName)
else:
- #s = "%s/%s" % (self.__path, self.datedFileName(filename, '.ps'))
s = "%s/%s.ps" % (self.__path, filename)
r.postscript(s, paper='letter')
def setBitmapOutput(self, filename="default"):
""" set a bitmap (png) output file """
if filename == "default":
- #s = "%s/%s" % (self.__path,
self.datedFileName(self.__defaultFileName, '.png'))
s = "%s.png" % (self.__defaultFileName)
else:
- #s = "%s/%s" % (self.__path, self.datedFileName(filename, '.png'))
s = "%s.png" % (filename)
r.bitmap(s, res=150) # ??????? SEGFAULT ????????
@@ -152,9 +145,6 @@
s = datetime.date.today().isoformat()
return "%s%s%s" % (name, s, extension)
- def setDefaultFileName(self, name):
- self.defaultFileName = name
-
def setPath(self, path):
""" set the path where will be saved the output files (ps &| png) """
try:
@@ -166,6 +156,9 @@
self.log('ERROR : wrong path for output files, error creating
directory [Interface.setPath]\n')
self.__path = path
+ def getPath(self):
+ return self.__path
+
def copyToDisk(self, rfd, filename=""):
""" copy the file open with fd to a new file on disk
used to cp XML output on disk """
@@ -195,4 +188,12 @@
except:
self.log('ERROR : cannot move log file [Interface.moveLog]\n')
+ def setPoints(self, points):
+ """ set the list of Point object to be plotted """
+ self.__points = points
+
+ def getPoints(self):
+ return self.__points
+
+
Index: ometah/experiment/metahtest.py
diff -u ometah/experiment/metahtest.py:1.14 ometah/experiment/metahtest.py:1.15
--- ometah/experiment/metahtest.py:1.14 Fri Jun 10 10:41:29 2005
+++ ometah/experiment/metahtest.py Fri Jun 10 13:03:35 2005
@@ -1,8 +1,10 @@
#!/usr/bin/python
# -*- coding: iso-8859-1 -*-
-
+#
+# Test module for Open Metaheuristic, main file
# Author: Jean-Philippe Aumasson <address@hidden>
-
+# Started 06/2005
+#
# Open Metaheuristic is a Library aimed at the conception of metaheuristics
# for difficult optimization.
#
@@ -22,13 +24,6 @@
#
"""
---------------------------------------------------------
-Test module for ometah
-----------
-Jean-Philippe Aumasson <address@hidden>
-started 06/06/2005
---------------------------------------------------------
-
TODO
------
W = WIP
@@ -36,69 +31,33 @@
P = Problem
------
-TD histogramme 3D ?? transfo Xdim en 1 ou 2 dim...
-
-W Organisation fichiers (arborescence ? noms fichiers ? clé/ID unique ? ..?)
-
-D renommer fonctions de plot selon les conventions
-
-P [déjà bcp de methodes, redondances dans les noms..] interface plus simple
pour lancer un plottage
-
-D garder la sortie XML dans un fichier, et possibilité de parser à partir d'un
fichier et non une sortie de ometah
-(=> pour plot sans lancement de ometah)
-
-D setBitmap & co comme méthodes d'interface, et noms de fichiers par défaut
(nom pb, etc...)
-
-D récupérer infos sur le problème à partir du header XML, et instancier un
Header à remplir selon les vars parsées
-
-W modulariser le prog
-
-D création d'un répertoire pour placer les sortie, methode dans Interface pour
fixer le path, et pour le nom de fichier par défaut
+10/06/05
-D add function name in error logs
+D passer directements les coodronnées lues en float, au lieu de le faire au
plotting -> économies de float()
-D renommage fichiers de sortie, copiés dans un répertoire donné (ou pas)
+D fonction pour déplacer le fichier de log
-D attributs __path et __defaultfilename dans l'Interface
+D Header.toString : pour mettre dans le log
-D privater les méthodes non publiques ( add __ )
+W nettoyer le code, le lisibiliser
-10/06/05
+D print execution time in log
W Interface pour N runs, en plottant le nécessaire à chaque fois...
1000 < N < 100 000
-
plottage de distribution : distrib pour chaque iteration, et pas pour
l'ensemble...superposition des distributions ?
-> distrib pour optima
-D passer directements les coodronnées lues en float, au lieu de le faire au
plotting -> économies de float()
+'runs' multiples, sauvegarder chaque optimum obtenu
-D (inutile) renommer logfile et sortie XML APRES, because on ne connaît pas
encore le titre (nom pb + algo)
+plottages solutions si plusieurs dimensions ?
-W nommer fichier sortie image / ps : pb_algo_type_(date), trop long avec
date...
--> la mettre pour le nom du répertoire : pb_algo_date
-
-D fonction pour déplacer le fichier de log
-
-D Header.toString : pour mettre dans le log
-
-nettoyer le code, le lisibiliser
-
-D print execution time in log
+parse error when no description (ou no autre chose...)
/----
NOTES
-fonction math :
-import math
-import Numeric
-
-pour XML parsing :
-http://pyxml.sourceforge.net/
-http://communaute.openesub.org/softs/externe/cvs/chora/co.php/enki/openNewRapport.py?r=1.7
-
-sortie parsing : liste d'instances de Point
executer commandes R : r('cmd...')
@@ -122,15 +81,24 @@
Interface has NOT Header or Points...
H & P given by XMLParser to main, which use their data in Interface
-code performance tips :
-http://www.szgti.bmf.hu/harp/python/fastpython.html
-
+histogram 3D pas possible with R
"""
from rpy import *
+# path to ometah binary
+__OMETAH_PATH = "../ometah"
+# generic name of the ouput file
+__R_OUT = "results"
+# nb of breaks in the distribution histogram
+__HIST_BREAKS = 20
+# name of the xml output file
+__OMETAH_OUTPUT = 'ometah_output'
+# temp directory to save files before final directory created
+__TEMP_DIR = "."
+
class XMLParser:
""" to parse the XML output """
@@ -189,16 +157,13 @@
if self.__header__ != None:
return self.__header__
self.__header__ = Header()
-
- # Exceptions !
try:
- # 1 remplir problem, et l'instancier avant
probl = self.__getRootElement().getElementsByTagName('problem')[0]
-
self.__header__.problem.key =
self.__getText(probl.getElementsByTagName('key')[0])
self.__header__.problem.name =
self.__getText(probl.getElementsByTagName('name')[0])
self.__header__.problem.description =
self.__getText(probl.getElementsByTagName('description')[0])
self.__header__.problem.dimension =
int(self.__getText(probl.getElementsByTagName('dimension')[0]))
+ print "Dimension: ", dimension
self.__header__.problem.reference =
self.__getText(probl.getElementsByTagName('reference')[0])
optimums = probl.getElementsByTagName('optimums')[0]
@@ -226,7 +191,6 @@
self.__interface.log('ERROR : XML exploration error for <problem>
[XMLParser.getHeader]\n')
try:
- # 2 idem pour metaheur
metah =
self.__getRootElement().getElementsByTagName('metaheuristic')[0]
self.__header__.metah.key =
self.__getText(metah.getElementsByTagName('key')[0])
self.__header__.metah.name =
self.__getText(metah.getElementsByTagName('name')[0])
@@ -249,7 +213,7 @@
description = None
formula = None
dimension = None
- optimum = [] # list of Point objects
+ optimum = [] # list of Point objects
min_bound = [] # idem
max_bound = [] # idem
reference = None
@@ -290,7 +254,7 @@
(m.name, m.key, m.family, m.acronym)
s += ' description: %s\n' % \
(m.description)
- s += '\nProblem:\n name: %s\n key: %s\n description: %s\n formula:
%s\n' % \
+ s += '\nProblem:\n name: %s\n key: %s\n description: %s\n dimension:
%s\n' % \
(p.name, p.key, p.description, p.dimension)
return s
@@ -304,67 +268,36 @@
coords = None
value = None
+ error = None
def __init__(self):
pass
-def run(n):
- """ make n runs for the given pb, algo, etc...
- each output has the index of its run """
-
-
-def main():
- """ main() """
-
- import time
- t = time.time()
-
- import sys
+def init(argv):
+ """ initialize interface, parser, and return the Interface """
import interface
-
- # path to ometah binary
- __OMETAH_PATH = "../ometah"
-
- # generic name of the ouput file
- __R_OUT = "results"
-
- # nb of breaks in the distribution histogram
- __HIST_BREAKS = 20
-
- # name of the xml output file
- __OMETAH_OUTPUT = 'ometah_output'
-
- # temp directory to save files before final directory created
- __TEMP_DIR = "."
-
# create a new Interface instance
- intfc = interface.Interface(sys.argv)
-
+ intfc = interface.Interface(argv)
# active the logs
intfc.setLog(1)
-
# set path to save logs and output files
# temporarly current dir, because we don't know yet the name to give to
the dir
# files (xml and log) shall be moved later
intfc.setPath(__TEMP_DIR)
-
# to be used after __path updated
intfc.setTemp(__TEMP_DIR)
-
slog = "\n[ Starting at " + time.ctime() + " ] \n"
intfc.log(slog)
# fd = XML output
fd = intfc.getXmlFromExecOmetah(__OMETAH_PATH)
intfc.log('ometah execution ... OK\n')
-
"""
# to get xml output from a file
# fd = intfc.getXmlFromFile(pathToTheFile)
- """
-
- # plan B to output XML, cos seek => error (file objects and FDs)
+ """
+ # put XML in a file, then read from it
fileOut = intfc.copyToDisk(fd, filename=__OMETAH_OUTPUT)
fd.close()
fd = open(fileOut, 'r')
@@ -376,16 +309,17 @@
parser.readXml()
fd.close() # we don't need it anymore
intfc.log('parsing XML : readXML() ... OK\n')
-
- pointsList = parser.getPoints()
+
+ # get the list of Points
+ intfc.setPoints(parser.getPoints())
intfc.log('getting points list : getPoints() ... OK\n')
# get header informations
header = parser.getHeader()
+ intfc.log(header.toString())
title = header.getTitle()
# NOW CAN CREATE THE DIR AND MOVE LOGFILE AND XMLOUTPUT
-
newpath = intfc.datedFileName(title, '')
# 1: create the new directory, and update interface's path value
intfc.setPath(newpath)
@@ -398,47 +332,102 @@
except:
intfc.log('ERROR : cannot move XML file [main]\n')
- """
+ return intfc
+
+
+
+def plotSingleRun(intfc):
+ """ make plottings for the given interface """
+ points = intfc.getPoints()
+
intfc.setBitmapOutput(filename = 'valuesDistribHist')
- intfc.plotValuesIterationsDistribHist(pointsList, __HIST_BREAKS)
+ intfc.plotValuesIterationsDistribHist(points, __HIST_BREAKS)
r.dev_off()
intfc.log('outputing results : plotValuesIterationsDistribHist(...) ...
OK\n')
- """
- """ # plotValues for each point
- setBitmapOutput(filename = intfc.datedFileName('values', '.png'))
- intfc.plotValuesIterationsGraph(pointsList, 1)
+
+ # plotValues for each point
+ intfc.setBitmapOutput(filename = intfc.datedFileName('values', '.png'))
+ intfc.plotValuesIterationsGraph(points, 1)
r.dev_off()
- interface.log('outputing results : plotValues(...) ... OK\n')
- """
+ intfc.log('outputing results : plotValues(...) ... OK\n')
# set file name... strange segfault if use commented args (cf.
Interface.setBitmap..)
name = 'valueBoxes' # "%s_%s" % (title, 'valueBoxes')
intfc.setBitmapOutput(filename = name)
- intfc.plotValuesIterationsBoxes(pointsList, 10)
+ intfc.plotValuesIterationsBoxes(points, 10)
r.dev_off()
intfc.log('outputing results : plotValuesIterationsBoxes(...) ... OK\n')
name = 'valueStdGraph'
intfc.setBitmapOutput(filename = name)
- intfc.plotValuesIterationsStdGraph(pointsList, 10)
+ intfc.plotValuesIterationsStdGraph(points, 10)
r.dev_off()
intfc.log('outputing results : plotValuesIterationsStdGraph(...) ...
OK\n')
-
- # plan B for r.bitmap's segfault : move all png and ps files to our
working directory
- cmd = 'mv *.png *.ps %s &> /dev/null' % (newpath)
- os.system(cmd)
- intfc.log(header.toString())
+def plotMultiRun(intfc, errorsList, valuesList, solutionsList):
+ """ after N runs, make the plottings for the data collected,
+ valuesList and ErrorList are lists of sublists """
+
+
+def runSingle(argv):
+ """ run once and make plottings for a single calcul """
+ import time
+ t = time.time()
+ interf = init(argv)
+ plotSingleRun(interf)
+ # for r.bitmap's segfault : move all png and ps files to our working
directory
+ cmd = 'mv *.png *.ps %s &> /dev/null' % (interf.getPath())
+ os.system(cmd)
slog = "\nTotal time : %f\n" % ( time.time()-t )
- intfc.log(slog)
-
+ interf.log(slog)
slog = "\n[ End at " + time.ctime() + " ] \n"
- intfc.log(slog)
+ interf.log(slog)
+
+def runMulti(argvs, n):
+ """ make n runs for the given pb & algo,
+ keeping values and solutions for each run """
+ # TODO : give a list of argvs and loop over the list
+ import time
+
+ # each element is a sublist (for each run),
+ # these sublists contain values for each iteration of the run
+ lvalues = []
+
+ # idem for the solutions,
+ # the list contains sublists of Solution objects
+ lsolut = []
+
+ ## BOF...use Point object !! => add Err attribute in Point
+
+ for arg in argvs:
+ t = time.time()
+ interf = init(arg)
+ #plotMultiRun(interf)
+ # for r.bitmap's segfault : move all png and ps files to our working
directory
+ cmd = 'mv *.png *.ps %s &> /dev/null' % (interf.getPath())
+ os.system(cmd)
+ slog = "\nTotal time : %f\n" % ( time.time()-t )
+ interf.log(slog)
+ slog = "\n[ End at " + time.ctime() + " ] \n"
+ interf.log(slog)
+
+
+
+#######################################################
+# MAIN
+#######################################################
-if __name__ == '__main__':
+def main():
+ """ main() """
+
+ import sys
+ # runMulti([sys.argv], 1)
+ runSingle(sys.argv)
+
+if __name__ == '__main__':
main()