Пример #1
0
func TestLinprog(t *testing.T) {
	m := 500
	n := 1000
	tol := 1e-8

	A := mat.RandN(m, n)
	c := mat.RandVec(n)
	b := mat.NewVec(m)
	xt := mat.RandVec(n)
	b.Apply(A, xt)

	At := A.TrView()

	rd := mat.NewVec(n)
	rp := mat.NewVec(m)
	rs := mat.NewVec(n)

	prob := NewStandard(c, A, b)

	//Example for printing duality gap and infeasibilities
	result := Solve(prob, nil, NewDisplay(2))

	rd.Sub(c, result.S)
	rd.AddMul(At, result.Y, -1)
	rp.Apply(A, result.X)
	rp.Sub(b, rp)
	rs.Mul(result.X, result.S)
	rs.Neg(rs)

	dev := (rd.Asum() + rp.Asum() + rs.Asum()) / float64(n)
	if dev > tol {
		t.Log(dev)
		t.Fail()
	}
}
Пример #2
0
func TestSolve(t *testing.T) {
	mat.Register(cops)

	xInit := mat.RandVec(10).Scal(10.0)
	sol := NewSolution(xInit)

	result := Solve(opt.Rosenbrock{}, sol, nil, NewDisplay(10))

	t.Log(result.Status, result.ObjX, result.Iter)
	if math.Abs(result.ObjX) > 0.1 {
		t.Fail()
	}

	params := NewParams()
	params.IterMax = 100000

	result = SolveGradProjected(opt.Rosenbrock{}, opt.RealPlus{}, sol,
		params, NewDisplay(1000))
	t.Log(result.Status, result.ObjX, result.Iter)

	params.XTolAbs = 1e-9
	params.XTolRel = 0
	params.FunTolRel = 0
	params.FunTolAbs = 0
	params.FunEvalMax = 100000
	result = Solve(rb{}, sol, params, NewDisplay(1))
	t.Log(result.Status, result.ObjX, result.Iter, result.X)

	xInit = mat.Vec{0, 3}
	sol.SetX(xInit, false)
	result = Solve(rosTest{}, sol, params, NewDisplay(1))
	t.Log(result.Status, result.ObjX, result.Iter)
}
Пример #3
0
func TestRosenbrock(t *testing.T) {
	mat.Register(cops)

	n := 10
	scale := 10.0
	xInit := mat.RandVec(n).Scal(scale)

	//Define input arguments
	obj := opt.Rosenbrock{}
	p := NewParams()
	p.FunEvalMax = 100000
	p.IterMax = 100000
	sol := NewSolution(xInit)

	//Steepest descent with armijo
	stDesc := NewSteepestDescent()
	res1 := stDesc.Solve(obj, sol, p, NewDisplay(100))

	t.Log(res1.ObjX, res1.FunEvals, res1.GradEvals, res1.Status)

	//Steepest descent with Quadratic
	stDesc.LineSearch = uni.DerivWrapper{uni.NewQuadratic()}
	res2 := stDesc.Solve(obj, sol, p, NewDisplay(100))

	t.Log(res2.ObjX, res2.FunEvals, res2.GradEvals, res2.Status)

	//LBFGS with armijo
	lbfgs := NewLBFGS()
	res3 := lbfgs.Solve(obj, sol, p, NewDisplay(10))

	t.Log(res3.ObjX, res3.FunEvals, res3.GradEvals, res3.Status)

	//LBFGS with Quadratic
	lbfgs.LineSearch = uni.DerivWrapper{uni.NewQuadratic()}
	res4 := lbfgs.Solve(obj, sol, p, NewDisplay(10))

	t.Log(res4.ObjX, res4.FunEvals, res4.GradEvals, res4.Status)

	//LBFGS with Cubic
	lbfgs.LineSearch = uni.NewCubic()
	res5 := lbfgs.Solve(obj, sol, p, NewDisplay(10))

	t.Log(res5.ObjX, res5.FunEvals, res5.GradEvals, res5.Status)

	if math.Abs(res1.ObjX) > 0.01 {
		t.Fail()
	}
	if math.Abs(res2.ObjX) > 0.01 {
		t.Fail()
	}
	if math.Abs(res3.ObjX) > 0.01 {
		t.Fail()
	}
	if math.Abs(res4.ObjX) > 0.01 {
		t.Fail()
	}
	if math.Abs(res5.ObjX) > 0.01 {
		t.Fail()
	}
}
Пример #4
0
func BenchmarkLinprog(bench *testing.B) {
	bench.StopTimer()
	m := 50
	n := 100
	tol := 1e-3
	rd := mat.NewVec(n)
	rp := mat.NewVec(m)
	rs := mat.NewVec(n)

	for i := 0; i < bench.N; i++ {
		A := mat.RandN(m, n)
		c := mat.RandVec(n)
		b := mat.NewVec(m)
		xt := mat.RandVec(n)
		b.Apply(A, xt)

		At := A.TrView()

		prob := NewStandard(c, A, b)
		bench.StartTimer()
		result := Solve(prob, nil)
		bench.StopTimer()

		rd.Sub(c, result.S)
		rd.AddMul(At, result.Y, -1)
		rp.Apply(A, result.X)
		rp.Sub(b, rp)
		rs.Mul(result.X, result.S)
		rs.Neg(rs)

		dev := (rd.Asum() + rp.Asum() + rs.Asum()) / float64(n)
		if dev > tol {
			bench.Log(dev)
		}
	}
}