Exemple #1
0
func Test_prms03(tst *testing.T) {

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

	var prms Prms
	prms = []*Prm{
		&Prm{N: "klx", V: 1.0},
		&Prm{N: "kly", V: 2.0},
		&Prm{N: "klz", V: 3.0},
	}
	io.Pforan("%v\n", prms)

	values, found := prms.GetValues([]string{"klx", "kly", "klz"})
	chk.Vector(tst, "values", 1e-15, values, []float64{1, 2, 3})
	chk.Bools(tst, "found", found, []bool{true, true, true})

	values, found = prms.GetValues([]string{"klx", "klY", "klz"})
	chk.Vector(tst, "values", 1e-15, values, []float64{1, 0, 3})
	chk.Bools(tst, "found", found, []bool{true, false, true})
}
Exemple #2
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})
}
Exemple #3
0
func Test_munkres01(tst *testing.T) {

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

	C := [][]float64{
		{1, 2, 3},
		{2, 4, 6},
		{3, 6, 9},
	}
	Ccor := [][]float64{
		{0, 1, 2},
		{0, 2, 4},
		{0, 3, 6},
	}
	Mcor := [][]Mask_t{
		{STAR, NONE, NONE},
		{NONE, NONE, NONE},
		{NONE, NONE, NONE},
	}

	var mnk Munkres
	mnk.Init(len(C), len(C[0]))
	mnk.SetCostMatrix(C)

	// 1:
	io.PfYel("1: after step 0:\n")
	io.Pf("%v", mnk.StrCostMatrix())

	// 2: step 1
	next_step := mnk.step1()
	io.PfYel("\n2: after step 1:\n")
	io.Pf("%v", mnk.StrCostMatrix())
	chk.IntAssert(next_step, 2)
	chk.Matrix(tst, "C", 1e-17, mnk.C, Ccor)
	chk.Bools(tst, "row_covered", mnk.row_covered, []bool{false, false, false})
	chk.Bools(tst, "col_covered", mnk.col_covered, []bool{false, false, false})

	// 3: step 2
	next_step = mnk.step2()
	io.PfYel("\n3: after step 2:\n")
	io.Pf("%v", mnk.StrCostMatrix())
	chk.IntAssert(next_step, 3)
	chk.Matrix(tst, "C", 1e-17, mnk.C, Ccor)
	check_mask_matrix(tst, "M", mnk.M, Mcor)
	chk.Bools(tst, "row_covered", mnk.row_covered, []bool{false, false, false})
	chk.Bools(tst, "col_covered", mnk.col_covered, []bool{false, false, false})

	// 4: step 3
	next_step = mnk.step3()
	io.PfYel("\n4: after step 3:\n")
	io.Pf("%v", mnk.StrCostMatrix())
	chk.IntAssert(next_step, 4)
	chk.Matrix(tst, "C", 1e-17, mnk.C, Ccor)
	check_mask_matrix(tst, "M", mnk.M, Mcor)
	chk.Bools(tst, "row_covered", mnk.row_covered, []bool{false, false, false})
	chk.Bools(tst, "col_covered", mnk.col_covered, []bool{true, false, false})

	// 5: step 4
	next_step = mnk.step4()
	io.PfYel("\n5: after step 4:\n")
	io.Pf("%v", mnk.StrCostMatrix())
	chk.IntAssert(next_step, 6)
	chk.Matrix(tst, "C", 1e-17, mnk.C, Ccor)
	check_mask_matrix(tst, "M", mnk.M, Mcor)
	chk.Bools(tst, "row_covered", mnk.row_covered, []bool{false, false, false})
	chk.Bools(tst, "col_covered", mnk.col_covered, []bool{true, false, false})

	// 6: step 6
	Ccor = [][]float64{
		{0, 0, 1},
		{0, 1, 3},
		{0, 2, 5},
	}
	next_step = mnk.step6()
	io.PfYel("\n6: after step 6:\n")
	io.Pf("%v", mnk.StrCostMatrix())
	chk.IntAssert(next_step, 4)
	chk.Matrix(tst, "C", 1e-17, mnk.C, Ccor)
	check_mask_matrix(tst, "M", mnk.M, Mcor)
	chk.Bools(tst, "row_covered", mnk.row_covered, []bool{false, false, false})
	chk.Bools(tst, "col_covered", mnk.col_covered, []bool{true, false, false})

	// 7: step 4 again (1)
	Mcor = [][]Mask_t{
		{STAR, PRIM, NONE},
		{PRIM, NONE, NONE},
		{NONE, NONE, NONE},
	}
	next_step = mnk.step4()
	io.PfYel("\n7: after step 4 again (1):\n")
	io.Pf("%v", mnk.StrCostMatrix())
	chk.IntAssert(next_step, 5)
	chk.Matrix(tst, "C", 1e-17, mnk.C, Ccor)
	check_mask_matrix(tst, "M", mnk.M, Mcor)
	chk.Bools(tst, "row_covered", mnk.row_covered, []bool{true, false, false})
	chk.Bools(tst, "col_covered", mnk.col_covered, []bool{false, false, false})

	// 8: step 5
	Mcor = [][]Mask_t{
		{NONE, STAR, NONE},
		{STAR, NONE, NONE},
		{NONE, NONE, NONE},
	}
	next_step = mnk.step5()
	io.PfYel("\n8: after step 5:\n")
	io.Pf("%v", mnk.StrCostMatrix())
	chk.IntAssert(next_step, 3)
	chk.Matrix(tst, "C", 1e-17, mnk.C, Ccor)
	check_mask_matrix(tst, "M", mnk.M, Mcor)
	chk.Bools(tst, "row_covered", mnk.row_covered, []bool{false, false, false})
	chk.Bools(tst, "col_covered", mnk.col_covered, []bool{false, false, false})

	// 9: step 3 again (1)
	next_step = mnk.step3()
	io.PfYel("\n9: after step 3 again (1):\n")
	io.Pf("%v", mnk.StrCostMatrix())
	chk.IntAssert(next_step, 4)
	chk.Matrix(tst, "C", 1e-17, mnk.C, Ccor)
	check_mask_matrix(tst, "M", mnk.M, Mcor)
	chk.Bools(tst, "row_covered", mnk.row_covered, []bool{false, false, false})
	chk.Bools(tst, "col_covered", mnk.col_covered, []bool{true, true, false})

	// 10: step 4 again (2)
	next_step = mnk.step4()
	io.PfYel("\n10: after step 4 again (2):\n")
	io.Pf("%v", mnk.StrCostMatrix())
	chk.IntAssert(next_step, 6)
	chk.Matrix(tst, "C", 1e-17, mnk.C, Ccor)
	check_mask_matrix(tst, "M", mnk.M, Mcor)
	chk.Bools(tst, "row_covered", mnk.row_covered, []bool{false, false, false})
	chk.Bools(tst, "col_covered", mnk.col_covered, []bool{true, true, false})

	// 11: step 6 again (1)
	Ccor = [][]float64{
		{0, 0, 0},
		{0, 1, 2},
		{0, 2, 4},
	}
	next_step = mnk.step6()
	io.PfYel("\n11: after step 6 again (1):\n")
	io.Pf("%v", mnk.StrCostMatrix())
	chk.IntAssert(next_step, 4)
	chk.Matrix(tst, "C", 1e-17, mnk.C, Ccor)
	check_mask_matrix(tst, "M", mnk.M, Mcor)
	chk.Bools(tst, "row_covered", mnk.row_covered, []bool{false, false, false})
	chk.Bools(tst, "col_covered", mnk.col_covered, []bool{true, true, false})

	// 12: step 4 again (3)
	Mcor = [][]Mask_t{
		{NONE, STAR, PRIM},
		{STAR, NONE, NONE},
		{NONE, NONE, NONE},
	}
	next_step = mnk.step4()
	io.PfYel("\n12: after step 4 again (3):\n")
	io.Pf("%v", mnk.StrCostMatrix())
	chk.IntAssert(next_step, 6)
	chk.Matrix(tst, "C", 1e-17, mnk.C, Ccor)
	check_mask_matrix(tst, "M", mnk.M, Mcor)
	chk.Bools(tst, "row_covered", mnk.row_covered, []bool{true, false, false})
	chk.Bools(tst, "col_covered", mnk.col_covered, []bool{true, false, false})

	// 13: step 6 again (2)
	Ccor = [][]float64{
		{1, 0, 0},
		{0, 0, 1},
		{0, 1, 3},
	}
	next_step = mnk.step6()
	io.PfYel("\n13: after step 6 again (2):\n")
	io.Pf("%v", mnk.StrCostMatrix())
	chk.IntAssert(next_step, 4)
	chk.Matrix(tst, "C", 1e-17, mnk.C, Ccor)
	check_mask_matrix(tst, "M", mnk.M, Mcor)
	chk.Bools(tst, "row_covered", mnk.row_covered, []bool{true, false, false})
	chk.Bools(tst, "col_covered", mnk.col_covered, []bool{true, false, false})

	// 14: step 4 again (4)
	Mcor = [][]Mask_t{
		{NONE, STAR, PRIM},
		{STAR, PRIM, NONE},
		{PRIM, NONE, NONE},
	}
	next_step = mnk.step4()
	io.PfYel("\n14: after step 4 again (4):\n")
	io.Pf("%v", mnk.StrCostMatrix())
	chk.IntAssert(next_step, 5)
	chk.Matrix(tst, "C", 1e-17, mnk.C, Ccor)
	check_mask_matrix(tst, "M", mnk.M, Mcor)
	chk.Bools(tst, "row_covered", mnk.row_covered, []bool{true, true, false})
	chk.Bools(tst, "col_covered", mnk.col_covered, []bool{false, false, false})

	// 15: step 5 again (1)
	Mcor = [][]Mask_t{
		{NONE, NONE, STAR},
		{NONE, STAR, NONE},
		{STAR, NONE, NONE},
	}
	next_step = mnk.step5()
	io.PfYel("\n15: after step 5 again (1):\n")
	io.Pf("%v", mnk.StrCostMatrix())
	chk.IntAssert(next_step, 3)
	chk.Matrix(tst, "C", 1e-17, mnk.C, Ccor)
	check_mask_matrix(tst, "M", mnk.M, Mcor)
	chk.Bools(tst, "row_covered", mnk.row_covered, []bool{false, false, false})
	chk.Bools(tst, "col_covered", mnk.col_covered, []bool{false, false, false})

	// 15: step 3 again (2)
	next_step = mnk.step3()
	io.PfYel("\n15: after step 3 again (2):\n")
	io.Pf("%v", mnk.StrCostMatrix())
	chk.IntAssert(next_step, 7)
	chk.Matrix(tst, "C", 1e-17, mnk.C, Ccor)
	check_mask_matrix(tst, "M", mnk.M, Mcor)
	chk.Bools(tst, "row_covered", mnk.row_covered, []bool{false, false, false})
	chk.Bools(tst, "col_covered", mnk.col_covered, []bool{true, true, true})
}