Ejemplo n.º 1
0
func (isle *EqnIsland) initEqns() {
	// initialize empty parents & pareto
	isle.parents = make(probs.ExprReportArray, isle.numEqns)
	isle.pareto = make(probs.ExprReportArray, isle.numEqns, isle.numEqns*2)

	// initialize new Exprs into brood
	srules := isle.treecfg.SRules
	fmt.Printf("srules: %v\n", srules)
	isle.brood = make([]probs.ExprReportArray, isle.numEqns)
	for i := 0; i < isle.numEqns; i++ {
		isle.brood[i] = make(probs.ExprReportArray, isle.eqnBroodSz)
		isle.eqnsLog.Println("EqnIsle Init Brood ", i)
		for j := 0; j < isle.eqnBroodSz; j++ {
			var e expr.Expr
			for {
				new_eqn := ExprGen(isle.treecfg, srules, isle.rng)

				// simplify equation
				eqnSimp := new_eqn.Simplify(isle.treecfg.SRules)
				if eqnSimp == nil || !(eqnSimp.HasVar()) {
					continue
				}
				eqnSimp.CalcExprStats(0)

				isle.treecfg.ResetCurr()
				isle.treecfg.ResetTemp()
				if isle.treecfg.CheckExpr(eqnSimp) {
					e = eqnSimp
					break
				}
			}

			isle.neqns++
			serial := make([]int, 0, 64)
			serial = e.Serial(serial)
			isle.trie.InsertSerial(serial)

			isle.brood[i][j] = new(probs.ExprReport)
			isle.brood[i][j].SetExpr(e)
			isle.eqnsLog.Println(isle.brood[i][j].Expr())
		}
		isle.eqnsLog.Println()
	}
	isle.eqnsLog.Println()
	isle.eqnsLog.Println()
}
Ejemplo n.º 2
0
func (isle *EqnIsland) breed() {
	// isle.mainLog.Println("Breeding EqnIsland ", isle.id, isle.gen)

	NE := isle.numEqns
	for e := 0; e < NE; e++ {

		// select parents for brood production
		rnum1, rnum2, rnum3, rnum4 := isle.rng.Intn(NE), isle.rng.Intn(NE), isle.rng.Intn(NE), isle.rng.Intn(NE)
		if rnum3 < rnum1 {
			rnum1 = rnum3
		}
		if rnum4 < rnum2 {
			rnum2 = rnum4
		}
		if isle.parents[rnum1] == nil || isle.parents[rnum2] == nil {
			e--
			continue
		}
		p1 := isle.parents[rnum1].Expr()
		p2 := isle.parents[rnum2].Expr()
		if p1 == nil || p2 == nil {
			e--
			continue
		}

		for b := 0; b < isle.eqnBroodSz; b++ {

			// production of one child from two parents
			var eqnSimp expr.Expr
			for {
				var new_eqn expr.Expr

				// cross equations
				if isle.rng.Float64() < isle.crossRate {
					new_eqn = CrossEqns_Vanilla(p1, p2, isle.treecfg, isle.rng)
				} else {
					new_eqn = InjectEqn_SubtreeFair(p1, isle.treecfg, isle.rng)
				}

				// mutate equation
				if isle.rng.Float64() < isle.mutateRate {
					MutateEqn_Vanilla(new_eqn, isle.treecfg, isle.rng, nil)
				}

				// simplify equation
				eqnSimp = new_eqn.Simplify(isle.treecfg.SRules)
				if eqnSimp == nil || !(eqnSimp.HasVar()) {
					continue
				}
				eqnSimp.CalcExprStats(0)

				isle.treecfg.ResetCurr()
				isle.treecfg.ResetTemp()
				if isle.treecfg.CheckExpr(eqnSimp) {
					break
				}
			}

			isle.neqns++
			serial := make([]int, 0, 64)
			serial = eqnSimp.Serial(serial)
			isle.trie.InsertSerial(serial)

			isle.brood[e][b] = new(probs.ExprReport)
			isle.brood[e][b].SetExpr(eqnSimp)
			isle.brood[e][b].SetProcID(isle.id)
			isle.brood[e][b].SetIterID(isle.gen)
			uid := isle.gen*(isle.numEqns*isle.eqnBroodSz) + e*(isle.eqnBroodSz) + b
			isle.brood[e][b].SetUnitID(uid)

		}

	}

}