func calculateD(sample []fwd1.Sequence, length int, ch chan dResult) { dmatrix := fwd1.GenerateDistanceMatrix(sample) cmatrix := covs.NewCMatrix(sampleSize, length, dmatrix) ks, vard := cmatrix.D() dr := dResult{ks: ks, vard: vard} ch <- dr }
func main() { // create file storing ks and vard f, err := os.Create(fname + ".csv") if err != nil { panic(err) } defer f.Close() ksarray := []float64{} // store ks vdarray := []float64{} // store VarD ngarray := []float64{} // store generation number // do evolution for i := 0; i < ngen; i++ { pop.Evolve() // we make 10 samples and average ksmean := desc.NewMean() vdmean := desc.NewMean() for j := 0; j < sampleTime; j++ { sample := fwd.Sample(pop.Genomes, sampleSize) dmatrix := fwd.GenerateDistanceMatrix(sample) cmatrix := covs.NewCMatrix(sampleSize, pop.Length, dmatrix) ks, vard := cmatrix.D() ksmean.Increment(ks) vdmean.Increment(vard) } // write to csv f.WriteString(fmt.Sprintf("%d,%g,%g\n", pop.NumOfGen, ksmean.GetResult(), vdmean.GetResult())) if (i+1)%1000 == 0 { fmt.Println("Generation: ", pop.NumOfGen, ksmean.GetResult(), vdmean.GetResult()) } // store array for draw ksarray = append(ksarray, ksmean.GetResult()) vdarray = append(vdarray, vdmean.GetResult()) ngarray = append(ngarray, float64(pop.NumOfGen)) } // draw svger := render.NewSVG(fname, 1, 2, 800, 200) pl := chart.ScatterChart{Title: "KS"} pl.AddDataPair("KS", ngarray, ksarray, chart.PlotStyleLines, chart.Style{Symbol: '+', SymbolColor: "#0000ff", LineStyle: chart.SolidLine}) svger.Plot(&pl) pl = chart.ScatterChart{Title: "VarD"} pl.AddDataPair("VarD", ngarray, vdarray, chart.PlotStyleLines, chart.Style{Symbol: '+', SymbolColor: "#0000ff", LineStyle: chart.SolidLine}) svger.Plot(&pl) svger.Close() // save population jf, err := os.Create(fname + ".json") if err != nil { panic(err) } defer jf.Close() b, err := json.Marshal(pop) if err != nil { panic(err) } jf.Write(b) }
func main() { // use all cpus runtime.GOMAXPROCS(runtime.NumCPU()) // population paramters length := 1000 // genome length = 1000 mutation := 0.00001 // mutation rate = 1e-5 transfer := 0.0 // transfer rate = 0 fragment := 0 // transferred length = 0 // simulation parameters samplesize := 100 numofgen := 100000 // population size array sizearray := []int{100, 1000, 10000} for _, size := range sizearray { // population pop := fwd.NewSeqPop(size, length, mutation, transfer, fragment) // result files ksname := fmt.Sprintf("ks_size_%d.csv", size) ksfile, err := os.Create(ksname) if err != nil { panic(err) } vdname := fmt.Sprintf("vd_size_%d.csv", size) vdfile, err := os.Create(vdname) if err != nil { panic(err) } // info file infoname := fmt.Sprintf("ks_size_%d.txt", size) infofile, err := os.Create(infoname) if err != nil { panic(err) } infofile.WriteString(fmt.Sprintf("Size = %d\n", pop.Size)) infofile.WriteString(fmt.Sprintf("Length = %d\n", pop.Length)) infofile.WriteString(fmt.Sprintf("Mutation = %g\n", pop.Mutation)) // population evolve for i := 0; i < numofgen; i++ { pop.Evolve() sample := fwd.Sample(pop.Genomes, samplesize) dmatrix := fwd.GenerateDistanceMatrix(sample) cmatrix := covs.NewCMatrix(samplesize, length, dmatrix) ks, vd := cmatrix.D() ksfile.WriteString(fmt.Sprintf("%g\n", ks)) vdfile.WriteString(fmt.Sprintf("%g\n", vd)) } // close files ksfile.Close() vdfile.Close() infofile.Close() } }