Exemplo n.º 1
0
func InjectEqn_Vanilla(p1 Expr, egp *prob.TreeParams, rng *rand.Rand) Expr {
	eqn := p1.Clone()
	eqn.CalcExprStats(0)

	s1 := rng.Intn(eqn.Size())
	s2 := s1
	e2 := eqn.GetExpr(&s2)

	egp.CurrSize = eqn.Size() - e2.Size()
	egp.CurrDepth = e2.Depth()
	egp.ResetTemp()

	// not correct (should be size based)
	new_eqn := exprGrow(-1, ExprGenDepth, egp, rng)
	//   eqn.SetExpr(&s1, new_eqn )
	SwapExpr(eqn, new_eqn, s1)

	eqn.CalcExprStats(0)
	return eqn
}
Exemplo n.º 2
0
func ExprGen(egp *prob.TreeParams, srules SimpRules, rng *rand.Rand) Expr {
	var ret Expr
	good := false
	cnt := 0

	for !good {
		egp.ResetCurr()
		egp.ResetTemp()

		//     eqn := ExprGenStats( &egp, rng )
		eqn := exprGrow(-1, ExprGenDepth, egp, rng)
		eqn.CalcExprStats(0)

		//     fmt.Printf( "%v\n", eqn)

		ret = eqn.Simplify(srules)
		ret.CalcExprStats(0)

		//     fmt.Printf( "%v\n\n", ret)

		// check eqn after simp
		good = egp.CheckExpr(ret)
		cnt++
	}
	return ret
}
Exemplo n.º 3
0
func InjectEqn_50_150(p1 Expr, egp *prob.TreeParams, rng *rand.Rand) Expr {
	eqn := p1.Clone()
	eqn.CalcExprStats(0)

	// begin loop
	s1 := rng.Intn(eqn.Size())
	s1_tmp := s1
	e1 := eqn.GetExpr(&s1_tmp)

	egp.ResetCurr()
	egp.ResetTemp()
	egp.TmpMinSize = e1.Size() / 2
	egp.TmpMaxSize = (e1.Size() * 3) / 2
	// loop if min/max out of bounds
	// and select new subtree

	// not correct (should be size based)
	new_eqn := exprGrow(-1, ExprGenDepth, egp, rng)
	//   eqn.SetExpr(&s1, new_eqn )
	SwapExpr(eqn, new_eqn, s1)
	eqn.CalcExprStats(0)
	return eqn
}
Exemplo n.º 4
0
func exprGrow(e ExprType, egfunc ExprGenFunc, egp *prob.TreeParams, rng *rand.Rand) Expr {

	if e == -1 {
		return egfunc(egp, rng)
	}

	switch {

	case e == TIME:
		return NewTime()
	case e == VAR:
		p := egp.UsableVars[rng.Intn(len(egp.UsableVars))]
		return NewVar(p)
	case e == CONSTANT:
		i := egp.CoeffCount
		egp.CoeffCount++
		return NewConstant(i)
	case e == CONSTANTF:
		return NewConstantF(rng.NormFloat64() * 2.0)
	case e == SYSTEM:
		return NewSystem(rng.Intn(egp.NumSys))

	case e == NEG:
		egp.CurrDepth++
		return NewNeg(egfunc(egp, rng))
	case e == ABS:
		egp.CurrDepth++
		return NewAbs(egfunc(egp, rng))
	case e == SQRT:
		egp.CurrDepth++
		return NewSqrt(egfunc(egp, rng))
	case e == SIN:
		egp.CurrDepth++
		egp.InTrig = true
		tmp := NewSin(egfunc(egp, rng))
		egp.InTrig = false
		return tmp
	case e == COS:
		egp.CurrDepth++
		egp.InTrig = true
		tmp := NewCos(egfunc(egp, rng))
		egp.InTrig = false
		return tmp
	case e == TAN:
		egp.CurrDepth++
		egp.InTrig = true
		tmp := NewTan(egfunc(egp, rng))
		egp.InTrig = false
		return tmp
	case e == EXP:
		egp.CurrDepth++
		return NewExp(egfunc(egp, rng))
	case e == LOG:
		egp.CurrDepth++
		return NewLog(egfunc(egp, rng))

	case e == POWI:
		egp.CurrDepth++
		return NewPowI(egfunc(egp, rng), (rng.Int()%7)-3)
	case e == POWF:
		egp.CurrDepth++
		return NewPowF(egfunc(egp, rng), rng.Float64()*3.0)
	case e == POWE:
		egp.CurrDepth++
		return NewPowE(egfunc(egp, rng), egfunc(egp, rng))

	case e == ADD:
		egp.CurrDepth++
		add := NewAdd()
		add.Insert(egfunc(egp, rng))
		add.Insert(egfunc(egp, rng))
		return add
	case e == MUL:
		egp.CurrDepth++
		mul := NewMul()
		mul.Insert(egfunc(egp, rng))
		mul.Insert(egfunc(egp, rng))
		return mul
	case e == DIV:
		egp.CurrDepth++
		return NewDiv(egfunc(egp, rng), egfunc(egp, rng))
	}
	return NewNull()
}