Module writepop
[hide private]
[frames] | no frames]

Source Code for Module writepop

  1  """ 
  2  writepop 
  3  ======== 
  4  Contains all classes used to write and extract individuals and populations  
  5  on the SQLite database. 
  6   
  7  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
  8  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
  9  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 10  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 11  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 12  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
 13  THE SOFTWARE. 
 14   
 15  @author: by Mehdi Khoury 
 16  @version: 1.20 
 17  @copyright: (c) 2009 Mehdi Khoury under the mit license 
 18  http://www.opensource.org/licenses/mit-license.html 
 19  @contact: mehdi.khoury at gmail.com 
 20   
 21   
 22   
 23   
 24   
 25  """ 
 26   
 27  import settings 
 28  import buildtree 
 29  import cPickle 
 30  import marshal 
 31  import pprint 
 32  import psyco 
 33  import timeit 
 34  import evalfitness 
 35  from pysqlite2 import dbapi2 as sqlite 
 36  import crossutil 
 37   
 38   
 39  psyco.profile() 
 40   
 41   
 42   
43 -def ClearDBTable(dbname,table):
44 """ 45 Function: ClearDBTable 46 ======================= 47 clear the table of the database 48 49 @param dbname: path to database e.g. r'D:\3d_work\pythongp\pySTGP_0.51\src\pop_db' 50 @param table: name of the database table 51 52 """ 53 54 con = sqlite.connect(dbname) 55 con.execute("drop table %s"%table) 56 con.commit() 57 con.close()
58 59 60
61 -def WriteInitialPopulation2DB(popsize,root_node,mintreesize,maxtreesize,buildmethod,dbname,tablename):
62 """ 63 Function: WriteInitialPopulation2DB 64 ==================================== 65 create a new population of randomly generated trees and write them to a database 66 67 @param popsize: size of the population 68 @param root_node: specify the root node and its arity (nb of children). e.g. (0,2,'root') 69 @param mintreesize: min tree depth (at the moment only 2 working) 70 @param maxtreesize: max tree depth (At least 2) 71 @param buildmethod: which Koza method is used to build the trees (either 72 'AddHalfNode' or 'AddFullNode' or 'AddGrowNodeMin' respectively for 73 Half, Full, or Ramped Half-n-Half) 74 @param dbname: path to database e.g. r'D:\3d_work\pythongp\pySTGP_0.51\src\pop_db' 75 @param tablename: name of the database table 76 77 """ 78 79 con = sqlite.connect(dbname) 80 con.execute("create table %s(o_id INTEGER PRIMARY KEY,tree TEXT,\ 81 tree_mapping TEXT, treedepth INTEGER, evaluated INTEGER, fitness FLOAT)"%tablename) 82 trees=[] 83 i=0 84 while i <popsize: 85 # create a tree individual 86 method = getattr(buildtree.buildTree(),buildmethod) 87 my_tree = method(root_node,0,mintreesize,maxtreesize) 88 if my_tree not in trees: 89 # my_tree = method(root_node,0,mintreesize,maxtreesize) 90 trees.append(my_tree) 91 # create its tree mapping 92 my_tree_indices=crossutil.GetIndicesMappingFromTree(my_tree) 93 # get depth of the tree 94 depth=crossutil.GetDepthFromIndicesMapping(my_tree_indices) 95 # get fitness of the tree 96 resultfitness=settings.FitnessFunction(my_tree) 97 #myfitness=evalfitness.EvalTreeForAllInputSets(my_tree,xrange(settings.nb_eval)) 98 #myfitness=evalfitness.EvalTreeForOneListInputSet(my_tree) 99 #print myfitness 100 #resultfitness=evalfitness.FinalFitness(myfitness) 101 #resultfitness=evalfitness.FinalFitness2(myfitness) 102 #resultfitness=evalfitness.FinalFitness4(myfitness) 103 #print resultfitness 104 con.execute("insert into %s(o_id,tree,tree_mapping,treedepth,evaluated,fitness) values (NULL,?,?,?,?,?)"%tablename,(buffer(marshal.dumps(my_tree,-1)),buffer(marshal.dumps(my_tree_indices,-1)),depth,1,resultfitness)) 105 i=i+1 106 con.commit() 107 con.close()
108 109 110
111 -def PrintPopFromDB(dbname,tablename,filename):
112 """ 113 Function: PrintPopFromDB 114 ========================= 115 print the population of trees with id references, tree depth and fitness scores 116 117 @param dbname: path to database e.g. r'D:\3d_work\pythongp\pySTGP_0.51\src\pop_db' 118 @param tablename: name of the database table 119 120 """ 121 122 con = sqlite.connect(dbname) 123 SELECT = "select o_id,tree,treedepth,fitness from %s order by fitness" % tablename 124 cur = con.cursor() 125 cur.execute(SELECT) 126 con.commit() 127 myresult= cur.fetchall() 128 con.close() 129 130 output = open(filename,'w') 131 for elem in myresult: 132 output.write(''.join([`elem[0]`,`marshal.loads(elem[1])`,`elem[2]`,`elem[3]`,'\n'])) 133 output.close()
134 135 136
137 -def GetPopStatFromDB(dbname,tablename):
138 """ 139 Function: GetPopStatFromDB 140 =========================== 141 get statistical data about the population 142 143 @param dbname: path to database e.g. r'D:\3d_work\pythongp\pySTGP_0.51\src\pop_db' 144 @param tablename: name of the database table 145 146 """ 147 148 con = sqlite.connect(dbname) 149 SELECT = "select o_id,tree,treedepth,fitness from %s order by fitness" % tablename 150 cur = con.cursor() 151 cur.execute(SELECT) 152 con.commit() 153 myresult= cur.fetchall() 154 con.close() 155 156 157 depths=[] 158 fit=[] 159 trees=[] 160 for elem in myresult: 161 ntree=marshal.loads(elem[1]) 162 if ntree not in trees: 163 trees.append(ntree) 164 depths.append(elem[2]) 165 #fit.append(elem[3]) 166 lengt=len(depths) 167 uniques_trees=len(trees) 168 av_depth=sum(depths)/len(myresult) 169 av_fit=sum(fit)/len(myresult) 170 print ''.join(['average depth: ',`av_depth`,' nb of unique trees: ',`uniques_trees`, ' over ', `lengt`])
171 172 173 174 175 176 if __name__ == '__main__': 177 #dbname=r'D:\3d_work\pythongp\pySTGP_0.51\src\pop_db' 178 dbname=r'D:\mehdi\python projects\pySTGP_0.52\src\pop_db' 179 tablename='tab1' 180 #ClearDBTable(dbname,tablename) 181 #WriteInitialPopulation2DB(100,(0,2,'root'),2,10,'AddHalfNode',dbname,tablename) 182 #WriteInitialPopulation2File(100,2,2,10,'AddHalfNode','pop') 183 #WriteInitialPopulation2DB2(100,(0,2,'root'),2,10,'AddHalfNode',dbname,tablename) 184 185 #t1 = timeit.Timer("writepop.WriteInitialPopulation2DB2(100,(0,2,'root'),2,10,'AddHalfNode',dbname,tablename)", 'from __main__ import dbname, tablename ;import writepop') 186 t2 = timeit.Timer("writepop.WriteInitialPopulation2DB(10000,(0,2,'root'),2,10,'AddHalfNode',dbname,tablename)", 'from __main__ import dbname, tablename ;import writepop') 187 print t2.repeat(1,1) 188