Exemplo n.º 1
0
func (PS *PgeSearch) GenInitExprAddMethod1() []expr.Expr {

	exprs := make([]expr.Expr, 0)

	// WARNING adding a single coefficient results in the same traversal but with the added extra coefficient
	cf := new(expr.Constant)
	cf.P = 1
	af := new(expr.Add)
	af.Insert(cf)
	exprs = append(exprs, af)

	for _, i := range PS.cnfg.treecfg.UsableVars {
		c := new(expr.Constant)
		c.P = -1
		v := new(expr.Var)
		v.P = i

		m := expr.NewMul()
		m.Insert(c)
		m.Insert(v)

		a := expr.NewAdd()
		a.Insert(m)
		exprs = append(exprs, a)
	}

	fmt.Println("Initial Add:  ", exprs)
	return exprs
}
Exemplo n.º 2
0
// add complexity to a single multiplication term
func (PS *PgeSearch) WidenTermInExprMethod1(O, E expr.Expr, pos int) (ret []expr.Expr) {
	ret = make([]expr.Expr, 0)

	// insert leafs  f()*L
	for _, L := range PS.GenLeafs {
		l := L.Clone()
		C := O.Clone()
		P := pos
		e := C.GetExpr(&P)
		// fmt.Printf("pos(%d): %v\n", pos, e)
		M := e.(*expr.Mul)
		M.Insert(l)
		sort.Sort(M)
		C.CalcExprStats()
		good := PS.cnfg.treecfg.CheckExpr(C)
		if good {
			ret = append(ret, C)
		}
	}

	// insert node(L)  :  f() * c*node(L)
	for _, N := range PS.GenNodes {
		for _, L := range PS.GenLeafs {
			c := new(expr.Constant)
			c.P = -1

			l := L.Clone()
			n := N.Clone()
			p := 1
			n.SetExpr(&p, l)

			var E expr.Expr
			if N.ExprType() == expr.DIV {
				E = expr.NewDiv(c, l)
			} else {
				// mul it
				M := expr.NewMul()
				M.Insert(c)
				M.Insert(n)
				E = M
			}

			C := O.Clone()
			P := pos
			e := C.GetExpr(&P)
			// fmt.Printf("pos(%d): %v\n", pos, e)
			M := e.(*expr.Mul)

			M.Insert(E)
			sort.Sort(M)
			C.CalcExprStats()
			good := PS.cnfg.treecfg.CheckExpr(C)
			if good {
				ret = append(ret, C)
			}
		}
	}
	return ret
}
Exemplo n.º 3
0
// change any term to something more complex...
func (PS *PgeSearch) DeepenTermInExprMethod1(O, E expr.Expr, pos int) []expr.Expr {
	exprs := make([]expr.Expr, 0)

	// make into add
	A := expr.NewAdd()
	A.Insert(E.Clone())
	OA := A.Clone()
	exprs = append(exprs, PS.AddTermToExprMethod1(OA, A, 0)[:]...)

	// make into mul
	M := expr.NewMul()
	M.Insert(E.Clone())
	OM := M.Clone()
	exprs = append(exprs, PS.WidenTermInExprMethod1(OM, M, 0)[:]...)

	// // make into div
	// if E.ExprType() != expr.DIV {
	// 	D := new(expr.Div)
	// 	c := new(expr.Constant)
	// 	c.P = -1
	// 	D.Numer = c
	// 	D.Denom = E.Clone()
	// 	exprs = append(exprs, D)
	// }

	// make inside of nodes
	for _, N := range PS.GenNodes {
		if N.ExprType() == expr.DIV {
			continue
		}
		T := N.Clone()
		P := 1
		T.SetExpr(&P, E.Clone())
		exprs = append(exprs, T)
	}

	ret := make([]expr.Expr, 0)
	for _, e := range exprs {
		C := O.Clone()
		P := pos
		C.SetExpr(&P, e)
		ret = append(ret, C)
	}
	return ret
}
Exemplo n.º 4
0
func (PS *PgeSearch) GenInitExprMulMethod1() []expr.Expr {
	exprs := make([]expr.Expr, 0)

	for _, i := range PS.cnfg.treecfg.UsableVars {
		c := new(expr.Constant)
		c.P = -1
		v := new(expr.Var)
		v.P = i

		m := expr.NewMul()
		m.Insert(c)
		m.Insert(v)

		exprs = append(exprs, m)
	}

	fmt.Println("Initial Mul:  ", exprs)
	return exprs
}
Exemplo n.º 5
0
// This is the FFXish style init function
func (PS *PgeSearch) GenInitExprMethod3() *probs.ReportQueue {
	fmt.Printf("generating initial expressions\n")

	GP := PS.cnfg.treecfg
	fmt.Printf("%v\n", GP)

	bases := make([]expr.Expr, 0)

	// single constant
	bases = append(bases, expr.NewConstant(-1))

	// c*x_i
	for _, v := range PS.prob.UsableVars {
		mul := expr.NewMul()
		mul.Insert(expr.NewConstant(-1))
		mul.Insert(expr.NewVar(v))
		bases = append(bases, mul)
	}

	// c*x_i^p & c*x_i^-p
	for p := 2; p <= 4; p++ {
		for _, v := range PS.prob.UsableVars {
			// positive powers
			mul := expr.NewMul()
			mul.Insert(expr.NewConstant(-1))
			mul.Insert(expr.NewPowI(expr.NewVar(v), p))
			bases = append(bases, mul)

			// negative powers
			nul := expr.NewMul()
			nul.Insert(expr.NewConstant(-1))
			nul.Insert(expr.NewPowI(expr.NewVar(v), -p))
			bases = append(bases, nul)
		}
	}

	// c*N(d*x_i)
	for _, N := range PS.GenNodes {
		if N.ExprType() == expr.DIV || N.ExprType() == expr.ADD || N.ExprType() == expr.MUL {
			continue
		}
		for _, v := range PS.prob.UsableVars {
			// positive powers
			mul := expr.NewMul()
			mul.Insert(expr.NewConstant(-1))

			nul := expr.NewMul()
			nul.Insert(expr.NewConstant(-1))
			nul.Insert(expr.NewVar(v))

			n := N.Clone()
			p := 1
			n.SetExpr(&p, nul)
			mul.Insert(n)
			bases = append(bases, mul)
		}
	}

	// copy bases for use later in expand
	PS.ffxBases = make([]expr.Expr, len(bases))
	for i, b := range bases {
		PS.ffxBases[i] = b.Clone()
	}

	exprs := probs.NewReportQueue()
	// exprs.SetSort(PS.cnfg.sortType)
	exprs.SetSort(probs.PESORT_PARETO_TST_ERR)

	for i, e := range bases {
		fmt.Printf("%d:  %v\n", i, e)
		serial := make([]int, 0, 64)
		serial = e.Serial(serial)
		PS.Trie.InsertSerial(serial)
		// on train data
		re := RegressExpr(e, PS.prob)
		re.SetUnitID(i)
		exprs.Push(re)
	}
	exprs.Sort()

	return exprs
}
Exemplo n.º 6
0
func (PS *PgeSearch) ExpandMethod3(O expr.Expr) (ret []expr.Expr) {
	O.Sort()
	ret = make([]expr.Expr, 0)
	// fmt.Printf("Expanding expression:  %v\n", O)

	add := O.(*expr.Add)

	// adding term to addition
	for _, B := range PS.ffxBases {
		found := false
		for _, C := range add.CS {
			// fmt.Printf("checking %v in %v\n", B, add)
			if C.AmISame(B) {
				// fmt.Println("found\n\n")
				found = true
				break
			}
		}
		if !found {
			e := O.Clone()
			a := e.(*expr.Add)
			a.Insert(B.Clone())
			sort.Sort(a)
			a.CalcExprStats()
			good := PS.cnfg.treecfg.CheckExpr(a)
			if good {
				ret = append(ret, a)
			}
			// fmt.Printf("grew  %v\n\n", a)
			// ret = append(ret, a)
		}
	}

	// extending terms in addition
	for _, B := range PS.ffxBases {
		for i, C := range add.CS {
			if C.ExprType() == expr.MUL {
				m := C.(*expr.Mul)
				if len(m.CS) > 3 {
					continue
				}
			}
			e := O.Clone()
			a := e.(*expr.Add)
			mul := expr.NewMul()
			mul.Insert(a.CS[i])
			mul.Insert(B.Clone())
			a.CS[i] = mul
			sort.Sort(a)
			a.CalcExprStats()
			good := PS.cnfg.treecfg.CheckExpr(a)
			if good {
				ret = append(ret, a)
			}
			// fmt.Printf("grew  %v\n\n", a)
			ret = append(ret, a)
		}
	}

	// fmt.Println("Len of ret = ", len(ret))
	return ret
}
Exemplo n.º 7
0
func fillExprStuff(names []string) (types []expr.ExprType, exprs []expr.Expr) {
	types = make([]expr.ExprType, len(names))
	exprs = make([]expr.Expr, len(names))
	for i, n := range names {
		switch strings.ToLower(n) {
		case "constant":
			types[i] = expr.CONSTANT
			exprs[i] = new(expr.Constant)
		case "constantf":
			types[i] = expr.CONSTANTF
			exprs[i] = new(expr.ConstantF)
		case "time":
			types[i] = expr.TIME
			exprs[i] = new(expr.Time)
		case "system":
			types[i] = expr.SYSTEM
			exprs[i] = new(expr.System)
		case "var":
			types[i] = expr.VAR
			exprs[i] = new(expr.Var)

		case "neg":
			types[i] = expr.NEG
			exprs[i] = new(expr.Neg)
		case "abs":
			types[i] = expr.ABS
			exprs[i] = new(expr.Abs)
		case "sqrt":
			types[i] = expr.SQRT
			exprs[i] = new(expr.Sqrt)
		case "sin":
			types[i] = expr.SIN
			exprs[i] = new(expr.Sin)
		case "cos":
			types[i] = expr.COS
			exprs[i] = new(expr.Cos)
		case "tan":
			types[i] = expr.TAN
			exprs[i] = new(expr.Tan)
		case "exp":
			types[i] = expr.EXP
			exprs[i] = new(expr.Exp)
		case "log":
			types[i] = expr.LOG
			exprs[i] = new(expr.Log)

		case "powi":
			types[i] = expr.POWI
			exprs[i] = new(expr.PowI)
		case "powf":
			types[i] = expr.POWF
			exprs[i] = new(expr.PowF)
		case "powe":
			types[i] = expr.POWE
			exprs[i] = new(expr.PowE)

		case "add":
			types[i] = expr.ADD
			exprs[i] = expr.NewAdd()
		case "mul":
			types[i] = expr.MUL
			exprs[i] = expr.NewMul()
		case "div":
			types[i] = expr.DIV
			exprs[i] = new(expr.Div)
		default:
			log.Fatalf("Unknown ExprType:  %s\n", n)
		}
	}
	return
}
Exemplo n.º 8
0
func (PS *PgeSearch) ExpandMethod2(O expr.Expr) (ret []expr.Expr) {
	O.Sort()
	ret = make([]expr.Expr, 0)
	// fmt.Printf("Expanding expression:  %v\n", O)

	add := O.(*expr.Add)

	// adding term to addition
	for _, B := range PS.ffxBases {
		found := false
		for _, C := range add.CS {
			// fmt.Printf("checking %v in %v\n", B, add)
			if C.AmISame(B) {
				// fmt.Println("found\n\n")
				found = true
				break
			}
		}
		if !found {
			e := O.Clone()
			a := e.(*expr.Add)
			a.Insert(B.Clone())
			sort.Sort(a)
			a.CalcExprStats()
			good := PS.cnfg.treecfg.CheckExpr(a)
			if good {
				ret = append(ret, a)
			}
			// fmt.Printf("grew  %v\n\n", a)
			// ret = append(ret, a)
		}
	}

	// extending terms in addition
	for _, B := range PS.ffxBases {
		for i, C := range add.CS {
			if C.ExprType() == expr.MUL {
				m := C.(*expr.Mul)
				if len(m.CS) > 3 {
					continue
				}
			}
			e := O.Clone()
			a := e.(*expr.Add)
			mul := expr.NewMul()
			mul.Insert(a.CS[i])
			mul.Insert(B.Clone())
			a.CS[i] = mul
			sort.Sort(a)
			a.CalcExprStats()
			good := PS.cnfg.treecfg.CheckExpr(a)
			if good {
				ret = append(ret, a)
			}
			// fmt.Printf("grew  %v\n\n", a)
			ret = append(ret, a)
		}
	}

	// deepening terms
	// if len(add.CS) < 2 {
	// 	return ret
	// }
	for i, C := range add.CS {
		if C.ExprType() == expr.MUL {
			m := C.(*expr.Mul)
			if len(m.CS) != 2 {
				continue
			}
			if m.CS[1].ExprType() == expr.ADD {
				continue
			}
		} else {
			continue
		}

		for _, B := range PS.ffxBases {
			e := O.Clone()
			a := e.(*expr.Add)
			m := a.CS[i].(*expr.Mul)
			n := m.CS[1]

			switch n.ExprType() {
			case expr.SQRT:
				A := expr.NewAdd()
				A.Insert(n.(*expr.Sqrt).C)
				A.Insert(B.Clone())
				n.(*expr.Sqrt).C = A
			case expr.SIN:
				A := expr.NewAdd()
				A.Insert(n.(*expr.Sin).C)
				A.Insert(B.Clone())
				n.(*expr.Sin).C = A
			case expr.COS:
				A := expr.NewAdd()
				A.Insert(n.(*expr.Cos).C)
				A.Insert(B.Clone())
				n.(*expr.Cos).C = A
			case expr.TAN:
				A := expr.NewAdd()
				A.Insert(n.(*expr.Tan).C)
				A.Insert(B.Clone())
				n.(*expr.Tan).C = A
			case expr.EXP:
				A := expr.NewAdd()
				A.Insert(n.(*expr.Exp).C)
				A.Insert(B.Clone())
				n.(*expr.Exp).C = A
			case expr.LOG:
				A := expr.NewAdd()
				A.Insert(n.(*expr.Log).C)
				A.Insert(B.Clone())
				n.(*expr.Log).C = A

			default:
				A := expr.NewAdd()
				A.Insert(m.CS[1])
				A.Insert(B.Clone())
				m.CS[1] = A
			}
			sort.Sort(a)
			a.CalcExprStats()
			good := PS.cnfg.treecfg.CheckExpr(a)
			if good {
				ret = append(ret, a)
			}
			// fmt.Printf("grew  %v\n", a)
			ret = append(ret, a)
		}
	}
	for i, C := range add.CS {
		if C.ExprType() == expr.MUL {
			m := C.(*expr.Mul)
			if len(m.CS) != 2 {
				continue
			}
			if m.CS[1].ExprType() == expr.ADD {
				continue
			}
		} else {
			continue
		}

		for _, B := range PS.ffxBases {
			e := O.Clone()
			a := e.(*expr.Add)
			m := a.CS[i].(*expr.Mul)
			n := m.CS[1]

			switch n.ExprType() {
			case expr.SQRT:
				M := expr.NewMul()
				M.Insert(n.(*expr.Sqrt).C)
				M.Insert(B.Clone())
				n.(*expr.Sqrt).C = M
			case expr.SIN:
				M := expr.NewMul()
				M.Insert(n.(*expr.Sin).C)
				M.Insert(B.Clone())
				n.(*expr.Sin).C = M
			case expr.COS:
				M := expr.NewMul()
				M.Insert(n.(*expr.Cos).C)
				M.Insert(B.Clone())
				n.(*expr.Cos).C = M
			case expr.TAN:
				M := expr.NewMul()
				M.Insert(n.(*expr.Tan).C)
				M.Insert(B.Clone())
				n.(*expr.Tan).C = M
			case expr.EXP:
				M := expr.NewMul()
				M.Insert(n.(*expr.Exp).C)
				M.Insert(B.Clone())
				n.(*expr.Exp).C = M
			case expr.LOG:
				M := expr.NewMul()
				M.Insert(n.(*expr.Log).C)
				M.Insert(B.Clone())
				n.(*expr.Log).C = M
			}
			sort.Sort(a)
			a.CalcExprStats()
			good := PS.cnfg.treecfg.CheckExpr(a)
			if good {
				ret = append(ret, a)
			}
			// fmt.Printf("grew  %v\n", a)
			ret = append(ret, a)
		}
	}
	// fmt.Println("Len of ret = ", len(ret))
	return ret
}
Exemplo n.º 9
0
// add another term to an add expr
func (PS *PgeSearch) AddTermToExprMethod1(O, E expr.Expr, pos int) (ret []expr.Expr) {
	ret = make([]expr.Expr, 0)
	A := E.(*expr.Add)

	// f() + cL
	for _, L := range PS.GenLeafs {
		c := new(expr.Constant)
		c.P = -1
		l := L.Clone()

		// mul it
		M := expr.NewMul()
		M.Insert(c)
		M.Insert(l)

		// skip if the same term already exists in the add
		skip := false
		for _, e := range A.CS {
			if e == nil {
				continue
			}
			// fmt.Printf("ACMP  %v  %v\n", M, e)
			if e.AmIAlmostSame(M) || M.AmIAlmostSame(e) {
				skip = true
				break
			}
		}
		if skip {
			continue
		}

		C := O.Clone()
		P := pos
		AM := C.GetExpr(&P).(*expr.Add)
		AM.Insert(M)
		sort.Sort(AM)
		C.CalcExprStats()
		good := PS.cnfg.treecfg.CheckExpr(C)
		if good {
			ret = append(ret, C)
		}
	}

	// f() + c*node(L)
	for _, N := range PS.GenNodes {
		for _, L := range PS.GenLeafs {

			c := new(expr.Constant)
			c.P = -1

			l := L.Clone()
			n := N.Clone()
			p := 1
			n.SetExpr(&p, l)

			var E expr.Expr
			if N.ExprType() == expr.DIV {
				E = expr.NewDiv(c, l)
			} else {
				// mul it
				M := expr.NewMul()
				M.Insert(c)
				M.Insert(n)
				E = M
			}

			// skip if the same term already exists in the add
			skip := false
			for _, e := range A.CS {
				if e == nil {
					continue
				}
				// fmt.Printf("ACMP  %v  %v\n", M, e)
				if e.AmIAlmostSame(E) || E.AmIAlmostSame(e) {
					skip = true
					break
				}
			}
			if skip {
				continue
			}

			// fmt.Println(E.String())

			C := O.Clone()
			P := pos
			AM := C.GetExpr(&P).(*expr.Add)
			AM.Insert(E)
			sort.Sort(AM)
			C.CalcExprStats()
			good := PS.cnfg.treecfg.CheckExpr(C)
			if good {
				ret = append(ret, C)
			}
		}
	}

	return ret
}