Пример #1
0
func TestWriteMath(t *testing.T) {
	want := `package gepModel

import (
	"math"
)

func gepModel(d []float64) float64 {
	y := 0.0

	y = (d[0] * d[0])
	y += d[0]
	y += (d[0] * (d[0] * d[0]))
	y += (((d[0] * d[0]) * d[0]) * d[0])

	return y
}
`

	g1 := gene.New("*.d0.d0.*.d0.*.*.d0.d0.d0.d0.d0.d0.d0.d0.d0.d0")
	g2 := gene.New("d0.*.d0.*.*.d0.d0.*.d0.d0.d0.d0.d0.d0.d0.d0.d0")
	g3 := gene.New("*.d0.*.d0.d0.*.d0.*.d0.d0.d0.d0.d0.d0.d0.d0.d0")
	g4 := gene.New("*.*.d0.*.d0.d0.d0.d0.d0.d0.d0.d0.d0.d0.d0.d0.d0")
	gn := New([]*gene.Gene{g1, g2, g3, g4}, "+")
	grammar, err := grammars.LoadGoMathGrammar()
	if err != nil {
		t.Fatalf("unable to LoadGoMathGrammar(): %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)
	}
}
Пример #2
0
func TestDup(t *testing.T) {
	mux := New([]*gene.Gene{
		gene.New("Nand.Or.And.Not.Nor.Not.Nor.And.d3.d2.d0.d2.d1.d4.d1.d4.d2"),
		gene.New("Nor.Nor.And.Or.Nor.And.Nor.Or.d0.d1.d0.d2.d3.d1.d2.d0.d3"),
		gene.New("Or.Or.Nand.d4.Not.Or.Nand.Nand.d0.d4.d3.d4.d1.d1.d3.d2.d0"),
		gene.New("Or.And.And.Nand.d5.Nand.Nand.Nor.d4.d3.d5.d0.d1.d0.d4.d0.d1"),
	},
		"And")
	validateSixMultiplexer(t, mux) // Force evaluation
	gn := mux.Dup()
	if err := checkEqual(gn, mux); err != nil {
		t.Errorf("TestDup after Dup failed: gn != mux: %v\n", err)
	}
	validateSixMultiplexer(t, gn) // Force evaluation
	validateSixMultiplexer(t, mux)

	gn = newFuelConsumption()
	validateFuelConsumption(t, gn) // Force evaluation
	mux = gn.Dup()
	if err := checkEqual(gn, mux); err != nil {
		t.Errorf("TestDup after Dup failed: gn != mux: %v\n", err)
	}
	validateFuelConsumption(t, gn) // Force evaluation
	validateFuelConsumption(t, mux)
}
Пример #3
0
func newFuelConsumption() *Genome {
	g1 := gene.New("+.d2.-.+./.Atan./.*.c4.d0.d5.d0.d0.d0.d5.d2.d0")
	g2 := gene.New("c7.+.+.+.Avg2.d4.d6.-.d4.c7.d1.d2.c2.d6.c8.d6.d2")
	g3 := gene.New("Min2.-.+.d6.*.*.Ln.Tanh.c4.c4.d0.c4.d6.d4.d4.c2.c4")
	g4 := gene.New("-.-.d2.+.+.Tanh.+.c1.c4.d0.d0.d6.d6.c9.d5.c9.c7")
	g1.Constants = []float64{
		-6.89063692129276,
		-5.68895535142064,
		74.5934430982391,
		-5.52293465987121,
		7.36762822046571,
		-4.90189548568468,
		-9.06796472060305,
		-4.60798974578082,
		2.21714835047456,
		-8.37993408001953,
	}
	g2.Constants = []float64{
		3.84215124973296,
		-8.56854411450545,
		-4.83944158531019,
		-7.34665429273354,
		1.81066316721091,
		-5.68092898342845,
		1.9168656633198,
		6.00730927940611,
		-7.87530137028108,
		-5.61497329630421,
	}
	g3.Constants = []float64{
		1.04586931974242,
		8.52891628772851,
		-4.40046388134404,
		-2.04504531998657,
		0.681604358043153,
		-2.69447920163579,
		-0.451368755149998,
		9.55381939146092,
		7.5619409561449,
		9.05453657643361,
	}
	g4.Constants = []float64{
		-1.2619803460799,
		3.61616965876798,
		-1.45847956785791,
		-5.12960448011719,
		0.023370006408887,
		7.84438612018189,
		-4.07391582995086,
		6.88815179906613,
		3.30928445387127,
		-8.11151463362529,
	}
	return New([]*gene.Gene{g1, g2, g3, g4}, "+")
}
Пример #4
0
func TestIrisPlants(t *testing.T) {
	g1 := gene.New("Sqrt.GOE2E.d2.d2.d2.c6.+.d3.d2.c3.c4.c9.d2.c7.d0.d3.d3")
	g2 := gene.New("d2.*.3Rt.Ln.*.Ln.OR2.GOE2A.c7.d2.c3.d3.d2.d3.d2.c1.c9")
	g3 := gene.New("AND2.Avg2.GOE2C.d3.AND1.GOE2A.3Rt.AND2.d0.c1.d3.d2.d0.c6.c3.c7.d0")
	g1.Constants = []float64{
		-7.36991485335856,
		3.02133243812372E-02,
		3.90911587878048,
		-3.37382122257149,
		6.02038636432997,
		-2.62184514908292,
		7.33332316049684,
		-9.19431134983367,
		-1.36875514999847,
		-6.14551225318155,
	}
	g2.Constants = []float64{
		-2.94106875820185,
		4.71480452894681,
		3.80718405713065,
		5.0022888882107,
		2.05542161320841,
		9.67284157841731,
		-0.159092989898373,
		3.67595446638386,
		4.72334971160009,
		7.20145268105106,
	}
	g3.Constants = []float64{
		6.54164250618,
		-5.31662953581347,
		7.16788232062746,
		2.4283577990051,
		-3.33404034546953,
		-4.19415875728629,
		-6.94814905240028,
		1.22348704489273,
		-2.74391918698691,
		1.35288552507096,
	}
	gn := New([]*gene.Gene{g1, g2, g3}, "+")
	validateIrisPlants(t, gn)
	w := map[string]int{
		"GOE2C": 1, "Avg2": 1, "c1": 1, "d0": 2, "+": 2, "d2": 4, "d3": 2, "AND1": 1, "c6": 1,
		"AND2": 2, "3Rt": 1, "GOE2A": 1, "Sqrt": 1, "GOE2E": 1,
	}
	if !reflect.DeepEqual(gn.SymbolMap, w) {
		t.Errorf("Genome %q SymbolMap=%#v, want %v", gn, gn.SymbolMap, w)
	}
}
Пример #5
0
func TestOdd3Parity(t *testing.T) {
	mux := New([]*gene.Gene{
		gene.New("Or.Or.d1.And.Or.d0.And.d2.d0.d2.d1.d1.d1.d0.d1"),
		gene.New("Not.And.And.Not.Or.And.And.d0.d1.d2.d2.d1.d0.d2.d2"),
		gene.New("Or.Or.Or.And.And.Not.Not.d1.d2.d0.d2.d1.d0.d0.d2"),
	},
		"And")
	validateOdd3Parity(t, mux)
	w := map[string]int{
		"d1": 6, "Or": 7, "Not": 4, "And": 10, "d2": 6, "d0": 6,
	}
	if !reflect.DeepEqual(mux.SymbolMap, w) {
		t.Errorf("Genome %q SymbolMap=%#v, want %v", mux, mux.SymbolMap, w)
	}
}
Пример #6
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)
	}
}
Пример #7
0
func TestSixMultiplexer(t *testing.T) {
	mux := New([]*gene.Gene{
		gene.New("Nand.Or.And.Not.Nor.Not.Nor.And.d3.d2.d0.d2.d1.d4.d1.d4.d2"),
		gene.New("Nor.Nor.And.Or.Nor.And.Nor.Or.d0.d1.d0.d2.d3.d1.d2.d0.d3"),
		gene.New("Or.Or.Nand.d4.Not.Or.Nand.Nand.d0.d4.d3.d4.d1.d1.d3.d2.d0"),
		gene.New("Or.And.And.Nand.d5.Nand.Nand.Nor.d4.d3.d5.d0.d1.d0.d4.d0.d1"),
	},
		"And")
	validateSixMultiplexer(t, mux)
	w := map[string]int{
		"Or": 7, "d1": 7, "d4": 6, "d3": 5, "d5": 2, "And": 9, "d0": 7, "d2": 4, "Nor": 7, "Not": 3, "Nand": 7,
	}
	if !reflect.DeepEqual(mux.SymbolMap, w) {
		t.Errorf("Genome %q SymbolMap=%#v, want %v", mux, mux.SymbolMap, w)
	}
}
Пример #8
0
func TestOdd7Parity(t *testing.T) {
	mux := New([]*gene.Gene{
		gene.New("Nor.Nand.d4.Or.Or.Xor.Or.Xor.d1.d4.d1.d0.d5.d2.d2.d1.d1"),
		gene.New("And.Or.Nor.Nand.Xor.Xor.Xor.Nor.d5.d0.d4.d3.d3.d6.d5.d5.d1"),
		gene.New("Xor.Xor.Xor.Xor.Nand.Or.Nand.Nor.d0.d2.d2.d4.d1.d1.d3.d1.d3"),
		gene.New("And.And.And.Xor.Nor.And.Xor.Not.d2.d5.d5.d5.d1.d6.d6.d1.d1"),
	},
		"Xor")
	validateOdd7Parity(t, mux)
	w := map[string]int{
		"Nor": 5, "Or": 5, "d2": 5, "d4": 4, "d0": 3, "d6": 3, "And": 5, "Xor": 14, "d5": 7, "Nand": 4, "d1": 8, "d3": 4, "Not": 1,
	}
	if !reflect.DeepEqual(mux.SymbolMap, w) {
		t.Errorf("Genome %q SymbolMap=%#v, want %v", mux, mux.SymbolMap, w)
	}
}
Пример #9
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)
	}
}
Пример #10
0
func TestEmotivEEG(t *testing.T) {
	g1 := gene.New("Inv./.c2.+.Avg2.+.-.+.d8.-.d0.d2.d3.d3.d8.d1.c2.c2.d3.c2.d9")
	g2 := gene.New("Avg2.c2.Logi.+.*.*.*.-.d4.d8.d9.c3.d9.d7.d9.c3.d7.d2.c2.d8.d10")
	g3 := gene.New("-.Avg2.d4.c7.+.-.-.+.d0./.d9.c7.d5.d9.c2.c4.c6.d11.c1.d12.d10")
	g4 := gene.New("d8.d5.Inv.-.*.-.-.-.d5.-.c2.d0.c4.d8.d2.c1.d0.d6.d7.d12.d9")
	g1.Constants = []float64{
		-3.66252632221442,
		4.32132938627278,
		-5.04312570574053,
		-8.33491012298959,
		570.542293374432,
		7.88445692312387,
		-8.729819635609,
		8.64864040040284,
		-8.99655140842921,
		-2.52174443800165,
	}
	g2.Constants = []float64{
		-8.09076204718161,
		-4.90160832544939,
		-8.66817224646748,
		8.00462202826014,
		0.603961302529984,
		-8.0687887203589,
		8.47999636271249,
		-3.48778252510147,
		-8.01019318216498,
		6.74764244514298,
	}
	g3.Constants = []float64{
		-8.25165715506455,
		-3.08755760368664,
		6.65888210699789,
		-8.59859004486221,
		4.01572573015534,
		0.589922788171026,
		-0.197058015686514,
		-4.66353343302713,
		-3.81057161168249,
		2.91665395062105,
	}
	g4.Constants = []float64{
		-1.4529587694937,
		6.02465895565661,
		-9.12533951841792,
		9.74364452040162,
		-3.01298532059694,
		9.0510095126194,
		-4.48530533768731,
		5.70360423596912,
		8.32840449232459,
		1.25095370342112,
	}
	gn := New([]*gene.Gene{g1, g2, g3, g4}, "+")
	validateEmotivEEG(t, gn)
	w := map[string]int{
		"c2": 3, "d0": 2, "Logi": 1, "d4": 2, "d9": 4, "d7": 1, "+": 9, "d1": 1, "d5": 1,
		"c7": 2, "Avg2": 3, "d2": 1, "*": 3, "d3": 2, "d8": 4, "c3": 1, "/": 2, "-": 6, "Inv": 1,
	}
	if !reflect.DeepEqual(gn.SymbolMap, w) {
		t.Errorf("Genome %q SymbolMap=%#v, want %v", gn, gn.SymbolMap, w)
	}
}
Пример #11
0
func TestMaunaLoaCO2(t *testing.T) {
	g1 := gene.New("Avg2.Avg2.Avg2.Avg2.Max2.+.-.3Rt.*.+.d10.d10.d5.d5.d9.c6.c4.d3.d10.d10.c1")
	g2 := gene.New("5Rt.-.d10.-.Floor.5Rt.Max2.Min2.Max2.Max2.d4.c3.d1.c8.d0.d8.d1.d1.d1.c0.c9")
	g3 := gene.New("-.Ln.*.Avg2.Min2.Min2.+.X4.d6.Min2.d0.c9.d5.d5.c1.c7.d1.d10.d3.c4.d10")
	g4 := gene.New("3Rt.+.NOT.Min2.c0.+./.*.*.-.c5.c4.c1.c8.d3.d8.d10.d8.c8.c0.d0")
	g5 := gene.New("Max2.-./.Min2.-.-.-.d10.-.Max2.d0.d10.d10.c4.c0.d6.c6.c0.c9.d10.d10")
	g1.Constants = []float64{
		2.05725272377697,
		-8.22715872676778,
		7.79778435621204,
		7.45353556932279,
		-7.08975493636891,
		-0.659504989776299,
		-0.828431998544267,
		7.796563615833,
		8.67976928006836,
		-0.453380511185034,
	}
	g2.Constants = []float64{
		-7.99472333506745,
		7.69669331949828,
		-4.70137638477737,
		5.90136417737358,
		-7.22087273781549,
		-2.04016235847041,
		-8.16583758049257,
		-2.9117647236549,
		-4.81935758537553,
		5.00030518509476,
	}
	g3.Constants = []float64{
		-3.52336191900388,
		8.72449690237129,
		2.50770592364269,
		46.134830774865,
		-6.78701132236702,
		-4.95468001342814,
		-5.70726645710624,
		-4.18439283425398,
		8.86043885616627,
		-9.26572466200751,
	}
	g4.Constants = []float64{
		-1.29636461989196,
		-8.2869655446028,
		0.337839899899289,
		-1.92247383037812,
		-3.17300943021943,
		-1.00946073793756,
		-3.5209204382458,
		2.58583330790124,
		9.88402966399121,
		1.73568132572405,
	}
	g5.Constants = []float64{
		-5.82934049501022,
		-1.27964110232856,
		9.83458967864009,
		-3.53862117374187,
		1.03366191595202,
		-7.08120975371563,
		-5.29584643086032,
		2.25257118442335,
		4.03668324839015,
		-3.56907559434797,
	}
	gn := New([]*gene.Gene{g1, g2, g3, g4, g5}, "Avg2")
	validateMaunaLoaCO2(t, gn)
	w := map[string]int{
		"-": 10, "d9": 1, "d0": 3, "c3": 1, "c0": 3, "c4": 3, "Min2": 6, "3Rt": 2,
		"d5": 4, "+": 5, "d4": 1, "X4": 1, "c1": 2, "/": 2, "*": 4, "Max2": 6,
		"d1": 2, "c8": 2, "c9": 2, "d10": 9, "5Rt": 2, "Ln": 1, "NOT": 1, "Avg2": 9,
		"Floor": 1, "d8": 2, "d6": 2, "c5": 1, "d3": 2, "c6": 2, "c7": 1,
	}
	if !reflect.DeepEqual(gn.SymbolMap, w) {
		t.Errorf("Genome %q SymbolMap=%#v, want %v", gn, gn.SymbolMap, w)
	}
}