Beispiel #1
0
func TestWriteNand(t *testing.T) {
	want := `package gepModel

func gepModel(d []bool) bool {
	y := false

	y = (((!(d[0] && d[1])) && (d[0] || d[1])) || (!(d[1] && d[1])))

	return y
}
`

	g1 := gene.New("Or.And.Not.Not.Or.And.And.d0.d1.d1.d1.d0.d1.d1.d0")
	gn := New([]*gene.Gene{g1}, "Or")
	grammar, err := grammars.LoadGoBooleanAllGatesGrammar()
	if err != nil {
		t.Fatalf("unable to LoadGoBooleanAllGatesGrammar(): %v", err)
	}

	b := new(bytes.Buffer)
	gn.Write(b, grammar)
	if b.String() != want {
		t.Errorf("gen.Write() got %v, want %v", b.String(), want)
	}
}
Beispiel #2
0
func main() {
	funcs := []gene.FuncWeight{
		{"Not", 1},
		{"And", 5},
		{"Or", 5},
	}
	numIn := len(nandTests[0].in)
	e := model.New(funcs, bn.BoolAllGates, 30, 7, 1, numIn, 0, "Or", validateNand)
	s := e.Evolve(1000)

	// Write out the Go source code for the solution.
	gr, err := grammars.LoadGoBooleanAllGatesGrammar()
	if err != nil {
		log.Printf("unable to load Boolean grammar: %v", err)
	}
	fmt.Printf("\n// gepModel is auto-generated Go source code for the\n")
	fmt.Printf("// nand solution karva expression:\n// %q, score=%v\n", s, validateNand(s))
	s.Write(os.Stdout, gr)
}
Beispiel #3
0
func TestExpression(t *testing.T) {
	want := "(((!((d[0] && d[1]))) && (d[0] || d[1])) || (!((d[1] && d[1]))))"
	g := New("Or.And.Not.Not.Or.And.And.d0.d1.d1.d1.d0.d1.d1.d0")
	grammar, err := grammars.LoadGoBooleanAllGatesGrammar()
	if err != nil {
		t.Fatalf("unable to LoadGoBooleanAllGatesGrammar(): %v", err)
	}

	helpers := make(grammars.HelperMap)
	got, err := g.Expression(grammar, helpers)
	if err != nil {
		t.Fatalf("g.Expression error: %v", err)
	}
	if got != want {
		t.Errorf("g.Expression got %q, want %q", got, want)
	}
	if len(helpers) != 0 {
		t.Errorf("helpers got length %v, want 0", len(helpers))
	}
}
Beispiel #4
0
func TestWrite6Multiplier(t *testing.T) {
	want := `package gepModel

func gepModel(d []bool) bool {
	y := false

	y = (d[3] || ((gepNor(d[1], d[3]) || (d[2] && d[0])) || gepNand((d[1] || d[0]), d[2])))
	y = y && gepNand((gepNor((d[5] || d[5]), gepNand(d[1], d[0])) || gepNor((d[2] || d[0]), d[3])), d[0])
	y = y && gepNor((((d[1] && d[2]) && d[4]) && gepNor(d[0], d[4])), gepNor((d[1] || d[4]), gepNor(d[4], d[0])))
	y = y && (gepNor(gepNor(gepNand(d[2], d[4]), d[1]), (!(d[2]))) || gepNand(gepNand(d[1], d[3]), gepNor(d[0], d[2])))

	return y
}

func gepNand(x, y bool) bool {
	return (!(x && y))
}

func gepNor(x, y bool) bool {
	return (!(x || y))
}
`

	g1 := gene.New("Or.d3.Or.Or.Nand.Nor.And.Or.d2.d1.d3.d2.d0.d1.d0.d0.d0")
	g2 := gene.New("Nand.Or.d0.Nor.Nor.Or.Nand.Or.d3.d5.d5.d1.d0.d2.d0.d1.d3")
	g3 := gene.New("Nor.And.Nor.And.Nor.Or.Nor.And.d4.d0.d4.d1.d4.d4.d0.d1.d2")
	g4 := gene.New("Or.Nor.Nand.Nor.Not.Nand.Nor.Nand.d1.d2.d1.d3.d0.d2.d2.d4.d4")
	gn := New([]*gene.Gene{g1, g2, g3, g4}, "And")
	grammar, err := grammars.LoadGoBooleanAllGatesGrammar()
	if err != nil {
		t.Fatalf("unable to LoadGoBooleanAllGatesGrammar(): %v", err)
	}

	b := new(bytes.Buffer)
	gn.Write(b, grammar)
	if b.String() != want {
		t.Errorf("gen.Write() got %v, want %v", b.String(), want)
	}
}