NEURON interface to GAUL (Neymotin and Lytton)

Accession:102464
This interface allows the use of genetic algorithms for optimization and search in high-dimensional spaces from within the NEURON environment. It includes converted .c,.h files from GAUL wrapped in proper MOD file syntax as well as MOD code interfacing to the library. It also comes with hoc utilitiy functions to make it easier to use the GA.
Tool Information (Click on a link to find other Tools with that property)
Tool Type: Control Simulations;
Simulation Environment: NEURON;
\
neuron_gaul_2
gaul
readme.txt
compatibility.mod
ga_bitstring.mod
ga_chromo.mod
ga_climbing.mod
ga_compare.mod
ga_core.mod
ga_crossover.mod
ga_de.mod
ga_deterministiccrowding.mod
ga_gradient.mod
ga_hoc.mod
ga_intrinsics.mod
ga_io.mod
ga_mutate.mod
ga_optim.mod
ga_qsort.mod
ga_randomsearch.mod
ga_rank.mod
ga_replace.mod
ga_sa.mod
ga_seed.mod
ga_select.mod
ga_similarity.mod
ga_simplex.mod
ga_stats.mod
ga_systematicsearch.mod
ga_tabu.mod
ga_utility.mod
linkedlist.mod
log_util.mod
memory_chunks.mod
memory_util.mod
nn_util.mod
random_util.mod
avltree.mod
table_util.mod
timer_util.mod
vecst.mod
mosinit.hoc
ga_utils.hoc
init.hoc
declist.hoc
setup.hoc
decvec.hoc
ga_test.hoc
gaul.h
xtmp
                            
// $Id: ga_utils.hoc,v 1.10 2007/04/06 21:40:01 samn Exp $ 

//consts for hoc/mod interface
install_methods_GA()

///////////////////////////////////////////
//from gaul.h:
GA_ELITISM_UNKNOWN = 0
GA_ELITISM_NULL = 0
GA_ELITISM_PARENTS_SURVIVE = 1
GA_ELITISM_ONE_PARENT_SURVIVES = 2
GA_ELITISM_PARENTS_DIE = 3
GA_ELITISM_RESCORE_PARENTS = 4
///////////////////////////////////////////

plot_fitness = 0 //plot fitness while ga runs
save_params  = 1 //save ga params for a run
plot_params  = 0 //plot params while ga runs

//////////////////////////////////////////////////////
//evolution parameters
NumIslands    = 20 //NumIslands for archipelago evolution
                   //if NumIslands==1, don't use
                   //archipelago
Mutate        = 0.95
Crossover     = 0.80
Migrate       = 0.10
PopSize       = 25
MaxGen        = 25

MaxNumIslands = GetMaxNumIslands_GA()
CurrentIsland = 0

MaxNumParams  = GetMaxNumAlleles_GA()
NumParams     = 3

Elitism       = GA_ELITISM_PARENTS_SURVIVE

MinFitness    = -9999999999
//////////////////////////////////////////////////////

//params that go back & forth between mod ga & hoc
objref vparams
vparams = new Vector(MaxNumParams)

objref vallele_mins,vallele_maxs
vallele_mins=new Vector(MaxNumParams)
vallele_maxs=new Vector(MaxNumParams)

strdef str // str to print to

objref vfitnessbest[MaxNumIslands]
objref vfitnessavg[MaxNumIslands]
objref vfitnessstddev[MaxNumIslands]

//make fitness vectors
proc MakeVF(){ local idx
  for(idx=0;idx<NumIslands;idx+=1){
    vfitnessbest[idx] = new Vector(500)
    vfitnessavg[idx] = new Vector(500)
    vfitnessstddev[idx] = new Vector(500)
    vfitnessbest[idx].resize(0)
    vfitnessavg[idx].resize(0)
    vfitnessstddev[idx].resize(0)
    if(NumIslands>1){
      sprint(str,"pop best fitness i%d",idx)
      vfitnessbest[idx].label(str)
      sprint(str,"pop avg fitness i%d",idx)
      vfitnessavg[idx].label(str)
      sprint(str,"pop stddev fitness i%d",idx)
      vfitnessstddev[idx].label(str)
    } else{
      vfitnessbest[idx].label("pop best fitness")
      vfitnessavg[idx].label("pop avg fitness")
      vfitnessstddev[idx].label("pop stddev fitness")
    }
  }
}

objref paramlist[MaxNumIslands] //list of best params for a generation
proc MakeLParams(){ local idx
  for(idx=0;idx<NumIslands;idx+=1){
    paramlist[idx]=new List()
  }
}

objref vgraphs[MaxNumIslands]    //graphs for fitness results
objref vpgraphs[MaxNumParams]    //graphs for parameter display
objref vhistparams[MaxNumParams]

proc MakeParamGraphs(){
  for(idx=0;idx<NumParams;idx+=1){
    sprint(str,"p%d",idx)
    vpgraphs[idx]=new Graph()
    vpgraphs[idx].label(str)
  }
}

proc MakeFitnessGraphs(){
  for(idx=0;idx<NumIslands;idx+=1){
    vgraphs[idx]=new Graph()
  }
}

proc MakeMyGraphs(){ local idx,jdx
  if(plot_fitness) MakeFitnessGraphs()
  if(plot_params) MakeParamGraphs()      
}

objref vcur_list[MaxNumIslands]
proc MakeVCurList(){ local idx
  for(idx=0;idx<NumIslands;idx+=1){
    vcur_list[idx] = new List()
  }
}

proc InitGenStats(){
  MakeVF()
  MakeLParams()
  MakeMyGraphs()
}

//calls mod evolution functions
func Evolution(){
  if(!SetElitism_GA(Elitism) ||\
     !SetMigrate_GA(Migrate) ||\
     !SetMutate_GA(Mutate) ||\
     !SetCrossover_GA(Crossover) ||\
     !SetNumIslands_GA(NumIslands) ||\
     !SetMaxGen_GA(MaxGen) ||\
     !SetPopSize_GA(PopSize) ||\
     !SetNumAlleles_GA(NumParams) ||\
     !vallele_mins.set_allele_mins() ||\
     !vallele_maxs.set_allele_maxs()){
    return 0
  }

  vallele_mins.set_allele_mins()
  vallele_maxs.set_allele_maxs()

  CurrentIsland = 0

  InitGenStats()

  return Evolution_GA()
}

proc ClearParamGraphs(){ local idx
  if(plot_params){
    for(idx=0;idx<NumParams;idx+=1){
      vpgraphs[idx].erase_all()
      sprint(str,"p%d",idx)
      vpgraphs[idx].label(str)
    }
  }
}

proc PlotParamGraph(){ local jdx,kdx,isle,mcolor,mbrush
  isle = $1
  mcolor=isle%10 + 1
  mbrush=isle%10 + 1
  sprint(str,"i%d",isle)
  for(jdx=0;jdx<NumParams;jdx+=1){
    vpgraphs[jdx].beginline(str,mcolor,mbrush)
    for(kdx=0;kdx<paramlist[isle].count;kdx+=1){
      vpgraphs[jdx].line(kdx,paramlist[isle].o(kdx).x(jdx))
      vpgraphs[jdx].mark(kdx,paramlist[isle].o(kdx).x(jdx),"+",12,mcolor,mbrush)
    }
    vpgraphs[jdx].flush()
  }
}

proc PlotAllParamGraphs(){ local idx
  for(idx=0;idx<NumIslands;idx+=1){
    PlotParamGraph(idx)
  }
}


flip_clear=0
//called by mod at end of each gen
proc UpdateGenStats(){ local idx,jdx,kdx,ldx localobj fparm
  vfitnessbest[CurrentIsland].append(GetBest_GA())
  vfitnessavg[CurrentIsland].append(GetAvg_GA())
  vfitnessstddev[CurrentIsland].append(GetStddev_GA())

  if(plot_fitness){
    vgraphs[CurrentIsland].erase_all()
    vfitnessbest[CurrentIsland].plot(vgraphs[CurrentIsland],2,1)
    vfitnessbest[CurrentIsland].mark(vgraphs[CurrentIsland],1,"o",2,2,1)
    vfitnessavg[CurrentIsland].plot(vgraphs[CurrentIsland],3,1)
    vfitnessavg[CurrentIsland].mark(vgraphs[CurrentIsland],1,"o",2,3,1)
    vgraphs[CurrentIsland].exec_menu("View = plot")
  }

  if(save_params){
    fparm = new Vector(32)
    fparm.get_alleles()
    fparm.resize(NumParams)
    savevec(paramlist[CurrentIsland],fparm) //makes a copy of vector,doesn't just store pointer
  }

  if(plot_params) PlotParamGraph(CurrentIsland)

  CurrentIsland += 1
  if(CurrentIsland >= NumIslands){

    CurrentIsland = 0

    if(plot_params){
       if(flip_clear){
         ClearParamGraphs()
         flip_clear = 0
       } else{
         flip_clear = 1
       }
    }
  }
}

//$s1 = path to output fitness data file
//$s2 = path to output param data file
// func SaveRunStats(){ local idx localobj best,XO,lvtmp
// //4 vectors per island in fitness file
// for(idx=0;idx<NumIslands;idx+=1){
//   veclist = new List()
//   savevec(vfitnessbest[idx])
//   savevec(vfitnessavg[idx])
//   savevec(vfitnessstddev[idx])
//   best = paramlist[idx].o(vfitnessbest[idx].max_ind)
//   savevec(best)
// }
// prveclist($s1)
// //need to know # of generations to properly
// //interpret param list file
// XO = new Union()
// lvtmp = new List()
// for(idx=0;idx<NumIslands;idx+=1){
//   for ltr(XO,paramlist[idx]) savevec(lvtmp,XO)
// }
// prveclist($s2,lvtmp)
// return 1
// }

proc PrintIsland(){ local isle,islemax,pidx
  isle = $1
  islemax = vfitnessbest[isle].max
  pidx = vfitnessbest[isle].max_ind
  printf("Island %d fitness=%f\nparams:",isle,islemax)
  paramlist[isle].o(pidx).printf
}

proc PrintIslands(){ local idx
  for(idx=0;idx<NumIslands;idx+=1){
    PrintIsland(idx)
  }
}

func BestIsland(){ local idx,mymax,curmax,maxidx,pidx
  pidx = vfitnessbest[0].max_ind
  maxidx = 0
  mymax = vfitnessbest[0].max
  for(idx=1;idx<NumIslands;idx+=1){
    curmax = vfitnessbest[idx].max
    if(curmax > mymax){
     mymax = curmax
     maxidx = idx
     pidx = vfitnessbest[idx].max_ind
    }
  }
  printf("BestIsland is %d with fitness of %f\n",maxidx,mymax)
  printf("Best params = ")
  paramlist[maxidx].o(pidx).printf
  return maxidx
}