Пример #1
0
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)
}
Пример #2
0
// the converge of KS equalibrium.
func main() {
	ksarray := []float64{} // store ks
	vdarray := []float64{} // store VarD
	ngarray := []float64{} // store generation number
	// do evolution
	for i := 0; i < ngen; i++ {
		pop.Evolve()
		// select 10 samples and averge
		mean := desc.NewMean()
		vmean := desc.NewMean()
		ch := make(chan dResult)
		num := 10
		for j := 0; j < num; j++ {
			sample := fwd1.Sample(pop.Genomes, sampleSize)
			go calculateD(sample, pop.Length, ch)
		}

		for j := 0; j < num; j++ {
			dr := <-ch
			mean.Increment(dr.ks)
			vmean.Increment(dr.vard)
		}

		ksarray = append(ksarray, mean.GetResult())
		vdarray = append(vdarray, vmean.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()
}
Пример #3
0
func main() {
	// ks file
	ksfilestr := dir + "/" + prex + "_ks.txt"
	ksfile, err := os.Create(ksfilestr)
	if err != nil {
		log.Fatalf("Can not create file: %s, %v", ksfilestr, err)
	}
	defer ksfile.Close()

	ksMean := desc.NewMean()
	ksVar := desc.NewVarianceWithBiasCorrection()

	vardMean := desc.NewMean()
	vardVar := desc.NewVarianceWithBiasCorrection()

	scovsMeanArray := make([]*desc.Mean, maxL)
	scovsVarArray := make([]*desc.Variance, maxL)

	rcovsMeanArray := make([]*desc.Mean, maxL)
	rcovsVarArray := make([]*desc.Variance, maxL)

	xyPLMeanArray := make([]*desc.Mean, maxL)
	xyPLVarArray := make([]*desc.Variance, maxL)

	xsysPLMeanArray := make([]*desc.Mean, maxL)
	xsysPLVarArray := make([]*desc.Variance, maxL)

	smXYPLMeanArray := make([]*desc.Mean, maxL)
	smXYPLVarArray := make([]*desc.Variance, maxL)

	for i := 0; i < maxL; i++ {
		scovsMeanArray[i] = desc.NewMean()
		scovsVarArray[i] = desc.NewVarianceWithBiasCorrection()

		rcovsMeanArray[i] = desc.NewMean()
		rcovsVarArray[i] = desc.NewVarianceWithBiasCorrection()

		xyPLMeanArray[i] = desc.NewMean()
		xyPLVarArray[i] = desc.NewVarianceWithBiasCorrection()

		xsysPLMeanArray[i] = desc.NewMean()
		xsysPLVarArray[i] = desc.NewVarianceWithBiasCorrection()

		smXYPLMeanArray[i] = desc.NewMean()
		smXYPLVarArray[i] = desc.NewVarianceWithBiasCorrection()

	}

	stepNum := etime / stepSize
	for i := 0; i < stepNum; i++ {
		// simulate
		pop.Evolve(stepSize)

		// create distance matrix
		dmatrix := pop.DistanceMatrix(sampleSize)
		c := covs.NewCMatrix(dmatrix, sampleSize, pop.Length)

		// calculate ks
		ks := c.CalculateKS()
		vard := c.CalculateVarD()
		ksfile.WriteString(fmt.Sprintf("%d\t%g\t%g\n", pop.Time, ks, vard))
		ksMean.Increment(ks)
		ksVar.Increment(ks)
		vardMean.Increment(vard)
		vardVar.Increment(vard)
		log.Printf("KsMean: %g, KsVar: %g, VardMean: %g, VardVar: %g\n", ksMean.GetResult(), ksVar.GetResult(), vardMean.GetResult(), vardVar.GetResult())

		// calculate covs
		scovs, rcovs, xyPL, xsysPL, smXYPL := c.CalculateCovs(maxL)
		for j := 0; j < maxL; j++ {
			scovsMeanArray[j].Increment(scovs[j])
			scovsVarArray[j].Increment(scovs[j])

			rcovsMeanArray[j].Increment(rcovs[j])
			rcovsVarArray[j].Increment(rcovs[j])

			xyPLMeanArray[j].Increment(xyPL[j])
			xyPLVarArray[j].Increment(xyPL[j])

			xsysPLMeanArray[j].Increment(xsysPL[j])
			xsysPLVarArray[j].Increment(xsysPL[j])

			smXYPLMeanArray[j].Increment(smXYPL[j])
			smXYPLVarArray[j].Increment(smXYPL[j])
		}

		svger := render.NewSVG(dir+"/"+prex+"_covs", 1, 5, 800, 200)
		scovMeans := make([]float64, maxL-1)
		rcovMeans := make([]float64, maxL-1)
		xyPLMeans := make([]float64, maxL-1)
		xsysPLMeans := make([]float64, maxL-1)
		smXYPLMeans := make([]float64, maxL-1)
		xs := make([]float64, maxL-1)
		for j := 1; j < maxL; j++ {
			xs[j-1] = float64(j)
			scovMeans[j-1] = scovsMeanArray[j].GetResult()
			rcovMeans[j-1] = rcovsMeanArray[j].GetResult()
			xyPLMeans[j-1] = xyPLMeanArray[j].GetResult()
			xsysPLMeans[j-1] = xsysPLMeanArray[j].GetResult()
			smXYPLMeans[j-1] = smXYPLMeanArray[j].GetResult()
		}
		pl := chart.ScatterChart{Title: "scovs"}
		pl.AddDataPair("struc covs", xs, scovMeans, chart.PlotStyleLines, chart.Style{Symbol: '+', SymbolColor: "#0000ff", LineStyle: chart.SolidLine})
		svger.Plot(&pl)

		pl = chart.ScatterChart{Title: "rcovs"}
		pl.AddDataPair("rate covs", xs, rcovMeans, chart.PlotStyleLines, chart.Style{Symbol: '+', SymbolColor: "#0000ff", LineStyle: chart.SolidLine})
		svger.Plot(&pl)

		pl = chart.ScatterChart{Title: "xyPL"}
		pl.AddDataPair("xyPL", xs, xyPLMeans, chart.PlotStyleLines, chart.Style{Symbol: '+', SymbolColor: "#0000ff", LineStyle: chart.SolidLine})
		svger.Plot(&pl)

		pl = chart.ScatterChart{Title: "xsysPL"}
		pl.AddDataPair("xsysPL", xs, xsysPLMeans, chart.PlotStyleLines, chart.Style{Symbol: '+', SymbolColor: "#0000ff", LineStyle: chart.SolidLine})
		svger.Plot(&pl)

		pl = chart.ScatterChart{Title: "smXYPL"}
		pl.AddDataPair("smXYPL", xs, smXYPLMeans, chart.PlotStyleLines, chart.Style{Symbol: '+', SymbolColor: "#0000ff", LineStyle: chart.SolidLine})
		svger.Plot(&pl)

		svger.Close()

		// write to file
		covsfilestr := dir + "/" + prex + "_covs.txt"
		covsfile, err := os.Create(covsfilestr)
		if err != nil {
			log.Fatalf("Can not create file: %s, %v", covsfilestr, err)
		}
		for j := 0; j < maxL; j++ {
			covsfile.WriteString(
				fmt.Sprintf("%d\t%g\t%g\t%g\t%g\t%g\t%g\t%g\t%g\t%g\t%g\n",
					j,
					scovsMeanArray[j].GetResult(), scovsVarArray[j].GetResult(),
					rcovsMeanArray[j].GetResult(), rcovsVarArray[j].GetResult(),
					xyPLMeanArray[j].GetResult(), xyPLVarArray[j].GetResult(),
					xsysPLMeanArray[j].GetResult(), xsysPLVarArray[j].GetResult(),
					smXYPLMeanArray[j].GetResult(), smXYPLVarArray[j].GetResult(),
				),
			)
		}
		covsfile.Close()
	}

	tnow = time.Now()
	log.Printf("Done at %v\n", tnow)
}