Example #1
0
func Test_parsing03(tst *testing.T) {

	//verbose()
	chk.PrintTitle("parsing03")

	keys := []string{"ux", "uy", "uz", "pl", "sl"}
	k1, k2, k3 := []string{"ex1", "ex2"}, []string{"more1", "more2"}, []string{"a", "b", "c"}
	allkeys := JoinKeys4(keys, k1, k2, k3, "")
	Pfpink("allkeys = %q\n", allkeys)
	chk.String(tst, allkeys, "ux uy uz pl sl ex1 ex2 more1 more2 a b c")

	allkeys = JoinKeys4(keys, k1, k2, k3, ",")
	Pfpink("allkeys = %q\n", allkeys)
	chk.String(tst, allkeys, "ux uy uz pl sl, ex1 ex2, more1 more2, a b c")

	r0, r1, r2, r3 := SplitKeys4(allkeys)
	Pfblue2("r0 = %v\n", r0)
	Pfblue2("r1 = %v\n", r1)
	Pfblue2("r2 = %v\n", r2)
	Pfblue2("r3 = %v\n", r3)
	chk.Strings(tst, "r0", r0, keys)
	chk.Strings(tst, "r1", r1, k1)
	chk.Strings(tst, "r2", r2, k2)
	chk.Strings(tst, "r3", r3, k3)

	allkeys = JoinKeys4(keys, []string{}, k2, []string{}, ",")
	Pfpink("allkeys = %q\n", allkeys)
	chk.String(tst, allkeys, "ux uy uz pl sl,, more1 more2,")
}
Example #2
0
func Test_ind02(tst *testing.T) {

	//verbose()
	chk.PrintTitle("ind02. copy into")

	rnd.Init(0)

	nbases := 1
	A := get_individual(0, nbases)
	B := get_individual(1, nbases)

	fmts := map[string][]string{
		"int": {"%2d", "%4d", "%5d"}, // ints
		"flt": {"%6g", "%6g", "%5g"}, // floats
		"str": {"%4s", "%2s", "%2s"}, // strings
		"key": {"%3x", "%3x", "%3x"}, // keys
		"byt": {"%4s", "%4s", "%4s"}, // bytes
		"fun": {"%3s", "%3s", "%3s"}, // funcs
	}
	io.Pfpink("A = %v\n", A.Output(fmts, false))
	io.Pfcyan("B = %v\n", B.Output(fmts, false))

	var ops OpsData
	ops.SetDefault()
	ops.Pc = 1.0
	ops.Cuts = []int{1, 2}
	ops.Xrange = [][]float64{{0, 1}, {-20, 20}, {-300, 300}}

	a := A.GetCopy()
	b := A.GetCopy()
	IndCrossover(a, b, A, B, 0, &ops)

	io.Pforan("a = %v\n", a.Output(fmts, false))
	io.Pfblue2("b = %v\n", b.Output(fmts, false))

	chk.Ints(tst, "a.Ints   ", a.Ints, []int{1, -20, 300})
	chk.Ints(tst, "b.Ints   ", b.Ints, []int{-1, 20, -300})
	chk.Strings(tst, "a.Strings", a.Strings, []string{"abc", "Y", "c"})
	chk.Strings(tst, "b.Strings", b.Strings, []string{"X", "b", "Z"})
	// TODO: add other tests here
	io.Pf("\n")

	x := get_individual(0, nbases)
	x.Ovas = []float64{0, 0}
	x.Oors = []float64{0, 0, 0}
	io.Pfblue2("x = %v\n", x.Output(fmts, false))
	B.CopyInto(x)

	chk.Scalar(tst, "ova0", 1e-17, x.Ovas[0], 200)
	chk.Scalar(tst, "ova1", 1e-17, x.Ovas[1], 100)
	chk.Scalar(tst, "oor0", 1e-17, x.Oors[0], 15)
	chk.Scalar(tst, "oor1", 1e-17, x.Oors[1], 25)
	chk.Scalar(tst, "oor2", 1e-17, x.Oors[2], 35)

	io.Pforan("x = %v\n", x.Output(fmts, false))
	chk.String(tst, x.Output(fmts, false), B.Output(fmts, false))
}
Example #3
0
func Test_keycode02(tst *testing.T) {

	//verbose()
	chk.PrintTitle("keycode02")

	str := "!flagA !typeA:keycodeA !typeB:keycodeB!typeC  : keycodeC !FlagB"
	res := Keycodes(str)
	chk.Strings(tst, "keycodes", res, []string{"flagA", "typeA", "typeB", "typeC", "FlagB"})
}
Example #4
0
func Test_mylab01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("mylab01")

	I := make([]int, 5)
	IntFill(I, 666)
	J := IntVals(5, 666)
	Js := StrVals(5, "666")
	M := IntsAlloc(3, 4)
	N := DblsAlloc(3, 4)
	S := StrsAlloc(2, 3)
	A := IntRange(-1)
	a := IntRange2(0, 0)
	b := IntRange2(0, 1)
	c := IntRange2(0, 5)
	C := IntRange3(0, -5, -1)
	d := IntRange2(2, 5)
	D := IntRange2(-2, 5)
	e := IntAddScalar(D, 2)
	f := DblOnes(5)
	ff := DblVals(5, 666)
	g := []int{1, 2, 3, 4, 3, 4, 2, 1, 1, 2, 3, 4, 4, 2, 3, 7, 8, 3, 8, 3, 9, 0, 11, 23, 1, 2, 32, 12, 4, 32, 4, 11, 37}
	h := IntUnique(g)
	G := []int{1, 2, 3, 38, 3, 5, 3, 1, 2, 15, 38, 1, 11}
	H := IntUnique(D, C, G, []int{16, 39})
	X, Y := MeshGrid2D(3, 6, 10, 20, 4, 3)
	P := [][]int{
		{1, 2, 3, 4, 5},
		{-1, -2, -3, -4, -5},
		{6, 7, 8, 9, 10},
	}
	Pc := IntsClone(P)
	chk.Ints(tst, "I", I, []int{666, 666, 666, 666, 666})
	chk.Ints(tst, "J", J, []int{666, 666, 666, 666, 666})
	chk.Strings(tst, "Js", Js, []string{"666", "666", "666", "666", "666"})
	chk.Ints(tst, "A", A, []int{})
	chk.Ints(tst, "a", a, []int{})
	chk.Ints(tst, "b", b, []int{0})
	chk.Ints(tst, "c", c, []int{0, 1, 2, 3, 4})
	chk.Ints(tst, "C", C, []int{0, -1, -2, -3, -4})
	chk.Ints(tst, "d", d, []int{2, 3, 4})
	chk.Ints(tst, "D", D, []int{-2, -1, 0, 1, 2, 3, 4})
	chk.Ints(tst, "e", e, []int{0, 1, 2, 3, 4, 5, 6})
	chk.Vector(tst, "f", 1e-16, f, []float64{1, 1, 1, 1, 1})
	chk.Vector(tst, "ff", 1e-16, ff, []float64{666, 666, 666, 666, 666})
	chk.Ints(tst, "h", h, []int{0, 1, 2, 3, 4, 7, 8, 9, 11, 12, 23, 32, 37})
	chk.Ints(tst, "H", H, []int{-4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 11, 15, 16, 38, 39})
	chk.IntMat(tst, "M", M, [][]int{{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}})
	chk.Matrix(tst, "N", 1e-17, N, [][]float64{{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}})
	chk.Matrix(tst, "X", 1e-17, X, [][]float64{{3, 4, 5, 6}, {3, 4, 5, 6}, {3, 4, 5, 6}})
	chk.Matrix(tst, "Y", 1e-17, Y, [][]float64{{10, 10, 10, 10}, {15, 15, 15, 15}, {20, 20, 20, 20}})
	chk.StrMat(tst, "S", S, [][]string{{"", "", ""}, {"", "", ""}})
	chk.IntMat(tst, "Pc", Pc, P)
}
Example #5
0
func TestReadTable01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("ReadTable 01")

	keys, res := ReadTableOrPanic("data/table01.dat")

	chk.Strings(tst, "keys", keys, []string{"a", "b", "c", "d"})
	chk.Vector(tst, "a", 1.0e-17, res["a"], []float64{1, 4, 7})
	chk.Vector(tst, "b", 1.0e-17, res["b"], []float64{2, 5, 8})
	chk.Vector(tst, "c", 1.0e-17, res["c"], []float64{3, 6, 9})
	chk.Vector(tst, "d", 1.0e-17, res["d"], []float64{666, 777, 641})
}
Example #6
0
func TestReadTable01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("ReadTable 01")

	keys, res, err := ReadTable("data/table01.dat")
	if err != nil {
		tst.Errorf("file cannot be read:\n%v\n", err.Error())
	}

	chk.Strings(tst, "keys", keys, []string{"a", "b", "c", "d"})
	chk.Vector(tst, "a", 1.0e-17, res["a"], []float64{1, 4, 7})
	chk.Vector(tst, "b", 1.0e-17, res["b"], []float64{2, 5, 8})
	chk.Vector(tst, "c", 1.0e-17, res["c"], []float64{3, 6, 9})
	chk.Vector(tst, "d", 1.0e-17, res["d"], []float64{666, 777, 641})
}
Example #7
0
func Test_parsing02(tst *testing.T) {

	//verbose()
	chk.PrintTitle("parsing02")

	keys := []string{"ux", "uy", "uz", "pl", "sl"}
	skeys := JoinKeys(keys)
	kkeys := SplitKeys(skeys)
	Pforan("keys  = %+v\n", keys)
	Pforan("skeys = %q\n", skeys)
	Pforan("kkeys = %+v\n", kkeys)
	chk.Strings(tst, "keys", keys, kkeys)

	k1, k2 := []string{"ex1", "ex2"}, []string{"more1", "more2"}
	allkeys := JoinKeys3(keys, k1, k2, "")
	Pfpink("allkeys = %q\n", allkeys)
	chk.String(tst, allkeys, "ux uy uz pl sl ex1 ex2 more1 more2")

	allkeys = JoinKeys3(keys, k1, k2, ",")
	Pfpink("allkeys = %q\n", allkeys)
	chk.String(tst, allkeys, "ux uy uz pl sl, ex1 ex2, more1 more2")

	r0, r1, r2 := SplitKeys3(allkeys)
	Pfblue2("r0 = %v\n", r0)
	Pfblue2("r1 = %v\n", r1)
	Pfblue2("r2 = %v\n", r2)
	chk.Strings(tst, "r0", r0, keys)
	chk.Strings(tst, "r1", r1, k1)
	chk.Strings(tst, "r2", r2, k2)

	allkeys = JoinKeys3(keys, []string{}, k2, ",")
	Pfpink("allkeys = %q\n", allkeys)
	chk.String(tst, allkeys, "ux uy uz pl sl,, more1 more2")

	allkeys = JoinKeys3(keys, []string{}, []string{}, ",")
	Pfpink("allkeys = %q\n", allkeys)
	chk.String(tst, allkeys, "ux uy uz pl sl,,")

	allkeys = JoinKeys3([]string{}, k1, k2, ",")
	Pfpink("allkeys = %q\n", allkeys)
	chk.String(tst, allkeys, ", ex1 ex2, more1 more2")

	r0, r1, r2 = SplitKeys3("a ,b c,")
	Pfblue2("r0 = %v\n", r0)
	Pfblue2("r1 = %v\n", r1)
	Pfblue2("r2 = %v\n", r2)
	chk.Strings(tst, "r0", r0, []string{"a"})
	chk.Strings(tst, "r1", r1, []string{"b", "c"})
	chk.Strings(tst, "r2", r2, []string{})
}
Example #8
0
func Test_sort05(tst *testing.T) {

	//verbose()
	chk.PrintTitle("sort05")

	a := map[string]int{"a": 1, "z": 2, "c": 3, "y": 4, "d": 5, "b": 6, "x": 7}
	b := map[string]float64{"a": 1, "z": 2, "c": 3, "y": 4, "d": 5, "b": 6, "x": 7}
	c := map[string]bool{"a": false, "z": true, "c": false, "y": true, "d": true, "b": false, "x": true}
	ka := StrIntMapSort(a)
	kb := StrDblMapSort(b)
	kc := StrBoolMapSort(c)
	io.Pforan("sorted_keys(a) = %v\n", ka)
	io.Pforan("sorted_keys(b) = %v\n", kb)
	io.Pforan("sorted_keys(c) = %v\n", kc)
	chk.Strings(tst, "ka", ka, []string{"a", "b", "c", "d", "x", "y", "z"})
	chk.Strings(tst, "kb", kb, []string{"a", "b", "c", "d", "x", "y", "z"})
	chk.Strings(tst, "kc", kc, []string{"a", "b", "c", "d", "x", "y", "z"})

	ka, va := StrIntMapSortSplit(a)
	io.Pfpink("sorted_keys(a) = %v\n", ka)
	io.Pfpink("sorted_vals(a) = %v\n", va)
	chk.Strings(tst, "ka", ka, []string{"a", "b", "c", "d", "x", "y", "z"})
	chk.Ints(tst, "va", va, []int{1, 6, 3, 5, 7, 4, 2})

	kb, vb := StrDblMapSortSplit(b)
	io.Pfcyan("sorted_keys(b) = %v\n", kb)
	io.Pfcyan("sorted_vals(b) = %v\n", vb)
	chk.Strings(tst, "kb", kb, []string{"a", "b", "c", "d", "x", "y", "z"})
	chk.Vector(tst, "vb", 1e-16, vb, []float64{1, 6, 3, 5, 7, 4, 2})

	kc, vc := StrBoolMapSortSplit(c)
	io.Pfcyan("sorted_keys(c) = %v\n", kc)
	io.Pfcyan("sorted_vals(c) = %v\n", vc)
	chk.Strings(tst, "kc", kc, []string{"a", "b", "c", "d", "x", "y", "z"})
	chk.Bools(tst, "vc", vc, []bool{false, false, false, true, true, true, true})
}
Example #9
0
func Test_mylab01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("mylab01")

	I := make([]int, 5)
	IntFill(I, 666)
	J := IntVals(5, 666)
	Js := StrVals(5, "666")
	M := IntsAlloc(3, 4)
	N := DblsAlloc(3, 4)
	A := IntRange(-1)
	a := IntRange2(0, 0)
	b := IntRange2(0, 1)
	c := IntRange2(0, 5)
	C := IntRange3(0, -5, -1)
	d := IntRange2(2, 5)
	D := IntRange2(-2, 5)
	e := IntAddScalar(D, 2)
	f := DblOnes(5)
	ff := DblVals(5, 666)
	g := []int{1, 2, 3, 4, 3, 4, 2, 1, 1, 2, 3, 4, 4, 2, 3, 7, 8, 3, 8, 3, 9, 0, 11, 23, 1, 2, 32, 12, 4, 32, 4, 11, 37}
	h := IntUnique(g)
	G := []int{1, 2, 3, 38, 3, 5, 3, 1, 2, 15, 38, 1, 11}
	H := IntUnique(D, C, G, []int{16, 39})
	X, Y := MeshGrid2D(3, 6, 10, 20, 4, 3)
	io.Pf("I  = %v\n", I)
	io.Pf("Js = %v\n", Js)
	io.Pf("J  = %v\n", J)
	io.Pf("A  = %v\n", A)
	io.Pf("a  = %v\n", a)
	io.Pf("b  = %v\n", b)
	io.Pf("c  = %v\n", c)
	io.Pf("C  = %v\n", C)
	io.Pf("d  = %v\n", d)
	io.Pf("D  = %v\n", D)
	io.Pf("e  = %v\n", e)
	io.Pf("f  = %v\n", f)
	io.Pf("G  = %v\n", G)
	io.Pf("H  = %v\n", H)
	io.Pf("g  = %v\n", g)
	io.Pf("h  = %v\n", h)
	io.Pf("M  = %v\n", M)
	io.Pf("N  = %v\n", N)
	io.Pf("X  = %v\n", X)
	io.Pf("Y  = %v\n", Y)
	chk.Ints(tst, "I", I, []int{666, 666, 666, 666, 666})
	chk.Ints(tst, "J", J, []int{666, 666, 666, 666, 666})
	chk.Strings(tst, "Js", Js, []string{"666", "666", "666", "666", "666"})
	chk.Ints(tst, "A", A, []int{})
	chk.Ints(tst, "a", a, []int{})
	chk.Ints(tst, "b", b, []int{0})
	chk.Ints(tst, "c", c, []int{0, 1, 2, 3, 4})
	chk.Ints(tst, "C", C, []int{0, -1, -2, -3, -4})
	chk.Ints(tst, "d", d, []int{2, 3, 4})
	chk.Ints(tst, "D", D, []int{-2, -1, 0, 1, 2, 3, 4})
	chk.Ints(tst, "e", e, []int{0, 1, 2, 3, 4, 5, 6})
	chk.Vector(tst, "f", 1e-16, f, []float64{1, 1, 1, 1, 1})
	chk.Vector(tst, "ff", 1e-16, ff, []float64{666, 666, 666, 666, 666})
	chk.Ints(tst, "h", h, []int{0, 1, 2, 3, 4, 7, 8, 9, 11, 12, 23, 32, 37})
	chk.Ints(tst, "H", H, []int{-4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 11, 15, 16, 38, 39})
	chk.IntMat(tst, "M", M, [][]int{{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}})
	chk.Matrix(tst, "N", 1e-17, N, [][]float64{{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}})
	chk.Matrix(tst, "X", 1e-17, X, [][]float64{{3, 4, 5, 6}, {3, 4, 5, 6}, {3, 4, 5, 6}})
	chk.Matrix(tst, "Y", 1e-17, Y, [][]float64{{10, 10, 10, 10}, {15, 15, 15, 15}, {20, 20, 20, 20}})
}
Example #10
0
func Test_cx01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("cx01")

	var ops OpsData
	ops.SetDefault()
	ops.Pc = 1
	ops.Cuts = []int{2, 4}

	A := []float64{1.1, 2.2, 3.3, 4.4, 5.5, 6.6}
	B := []float64{9.1, 9.2, 9.3, 9.4, 9.5, 9.6}
	a := make([]float64, len(A))
	b := make([]float64, len(A))
	FltCrossover(a, b, A, B, 0, &ops)
	io.Pfred("A = %3v\n", A)
	io.PfRed("B = %3v\n", B)
	io.Pfcyan("a = %3v\n", a)
	io.Pfblue2("b = %3v\n", b)
	chk.Vector(tst, "a", 1e-17, a, []float64{1.1, 2.2, 9.3, 9.4, 5.5, 6.6})
	chk.Vector(tst, "b", 1e-17, b, []float64{9.1, 9.2, 3.3, 4.4, 9.5, 9.6})
	io.Pf("\n")

	C := []string{"A", "B", "C", "D", "E", "F"}
	D := []string{"-", "o", "+", "@", "*", "&"}
	c := make([]string, len(A))
	d := make([]string, len(A))
	ops.Cuts = []int{1, 3}
	StrCrossover(c, d, C, D, 0, &ops)
	io.Pfred("C = %3v\n", C)
	io.PfRed("D = %3v\n", D)
	io.Pfcyan("c = %3v\n", c)
	io.Pfblue2("d = %3v\n", d)
	chk.Strings(tst, "c", c, []string{"A", "o", "+", "D", "E", "F"})
	chk.Strings(tst, "d", d, []string{"-", "B", "C", "@", "*", "&"})
	io.Pf("\n")

	E := [][]byte{[]byte("A"), []byte("B"), []byte("C"), []byte("D"), []byte("E"), []byte("F")}
	F := [][]byte{[]byte("-"), []byte("o"), []byte("+"), []byte("@"), []byte("*"), []byte("&")}
	e := make([][]byte, len(A))
	f := make([][]byte, len(A))
	for i := 0; i < len(A); i++ {
		e[i] = make([]byte, 1)
		f[i] = make([]byte, 1)
	}
	ops.Cuts = []int{1, 3}
	BytCrossover(e, f, E, F, 0, &ops)
	io.Pfred("E = %3s\n", E)
	io.PfRed("F = %3s\n", F)
	io.Pfcyan("e = %3s\n", e)
	io.Pfblue2("f = %3s\n", f)
	e_s := make([]string, len(A))
	f_s := make([]string, len(A))
	for i := 0; i < len(A); i++ {
		e_s[i] = string(e[i])
		f_s[i] = string(f[i])
	}
	chk.Strings(tst, "e_s", e_s, []string{"A", "o", "+", "D", "E", "F"})
	chk.Strings(tst, "f_s", f_s, []string{"-", "B", "C", "@", "*", "&"})
	io.Pf("\n")

	G := []byte("ABCDEF")
	H := []byte("-o+@*&")
	g := make([]byte, len(A))
	h := make([]byte, len(A))
	ops.Cuts = []int{1, 3}
	KeyCrossover(g, h, G, H, 0, &ops)
	io.Pfred("G = %3v\n", G)
	io.PfRed("H = %3v\n", H)
	io.Pfcyan("g = %3v\n", g)
	io.Pfblue2("h = %3v\n", h)
	g_s := make([]string, len(A))
	h_s := make([]string, len(A))
	for i := 0; i < len(A); i++ {
		g_s[i] = string(g[i])
		h_s[i] = string(h[i])
	}
	chk.Strings(tst, "g_s", g_s, []string{"A", "o", "+", "D", "E", "F"})
	chk.Strings(tst, "h_s", h_s, []string{"-", "B", "C", "@", "*", "&"})
	io.Pf("\n")

	M := []Func_t{func(i *Individual) string { return "A" }, func(i *Individual) string { return "B" }, func(i *Individual) string { return "C" }, func(i *Individual) string { return "D" }, func(i *Individual) string { return "E" }, func(i *Individual) string { return "F" }}
	N := []Func_t{func(i *Individual) string { return "-" }, func(i *Individual) string { return "o" }, func(i *Individual) string { return "+" }, func(i *Individual) string { return "@" }, func(i *Individual) string { return "*" }, func(i *Individual) string { return "&" }}
	m := make([]Func_t, len(A))
	n := make([]Func_t, len(A))
	ops.Cuts = []int{1, 3}
	FunCrossover(m, n, M, N, 0, &ops)
	io.Pfred("M = %3v\n", M)
	io.PfRed("N = %3v\n", N)
	io.Pfcyan("m = %3v\n", m)
	io.Pfblue2("n = %3v\n", n)
	m_s := make([]string, len(A))
	n_s := make([]string, len(A))
	for i := 0; i < len(A); i++ {
		m_s[i] = m[i](nil)
		n_s[i] = n[i](nil)
	}
	chk.Strings(tst, "m_s", m_s, []string{"A", "o", "+", "D", "E", "F"})
	chk.Strings(tst, "n_s", n_s, []string{"-", "B", "C", "@", "*", "&"})
	io.Pf("\n")
}