예제 #1
0
// Maximize a + b subject to both 0 ≤ 2a + b ≤ 10 and 3 ≤ 2b − a ≤ 8.
func ExampleSimplex_LoadProblem() {
	// Set up the problem.
	mat := clp.NewPackedMatrix()
	mat.AppendColumn([]clp.Nonzero{
		{Index: 0, Value: 2.0},  // 2a
		{Index: 1, Value: -1.0}, // -a
	})
	mat.AppendColumn([]clp.Nonzero{
		{Index: 0, Value: 1.0}, // b
		{Index: 1, Value: 2.0}, // 2b
	})
	rb := []clp.Bounds{
		{Lower: 0, Upper: 10}, // [0, 10]
		{Lower: 3, Upper: 8},  // [3, 8]
	}
	obj := []float64{1.0, 1.0} // a + b
	simp := clp.NewSimplex()
	simp.LoadProblem(mat, nil, obj, rb, nil)
	simp.SetOptimizationDirection(clp.Maximize)

	// Solve the optimization problem.
	simp.Primal(clp.NoValuesPass, clp.NoStartFinishOptions)
	val := simp.ObjectiveValue()
	soln := simp.PrimalColumnSolution()

	// Output the results.
	fmt.Printf("a = %.1f\nb = %.1f\na + b = %.1f\n", soln[0], soln[1], val)
	// Output:
	// a = 2.4
	// b = 5.2
	// a + b = 7.6
}
예제 #2
0
// Test if we can solve a complete optimization problem with the simplex model.
func TestPrimalSolve(t *testing.T) {
	// Set up the following problem: Minimize a + 2b subject to {4 ≤ a + b
	// ≤ 9, -5 ≤ 3a − b ≤ 3}.
	mat := clp.NewPackedMatrix()
	mat.AppendColumn([]clp.Nonzero{
		{Index: 0, Value: 1.0}, // a
		{Index: 1, Value: 3.0}, // 3a
	})
	mat.AppendColumn([]clp.Nonzero{
		{Index: 0, Value: 1.0},  // b
		{Index: 1, Value: -1.0}, // -b
	})
	rb := []clp.Bounds{
		{Lower: 4, Upper: 9},  // [4, 9]
		{Lower: -5, Upper: 3}, // [-5, 3]
	}
	obj := []float64{1.0, 2.0} // a + 2b
	simp := clp.NewSimplex()
	simp.LoadProblem(mat, nil, obj, rb, nil)
	simp.SetOptimizationDirection(clp.Minimize)

	// Solve the optimization problem.
	simp.Primal(clp.NoValuesPass, clp.NoStartFinishOptions)
	v := simp.ObjectiveValue()
	soln := simp.PrimalColumnSolution()

	// Check the results.
	if !closeTo(soln[0], 1.75, 0.005) || !closeTo(soln[1], 2.25, 0.005) {
		t.Fatalf("Expected [1.75 2.25] but observed %v", soln)
	}
	if !closeTo(v, 6.25, 0.005) {
		t.Fatalf("Expected 6.25 but observed %.10g", v)
	}
}
예제 #3
0
// Test if we can add columns to a packed matrix and get the expected dump in
// return.
func TestDumpMatrix(t *testing.T) {
	m := clp.NewPackedMatrix()
	addColumns(m, 5, 5)
	expected := `Dumping matrix...

colordered: 1
major: 5   minor: 5
vec 0 has length 2 with entries:
                      0               0.0000000000000000000000000
                      4              -0.0000000000000000000000000
vec 1 has length 2 with entries:
                      1              10.0000000000000000000000000
                      3             -10.0000000000000000000000000
vec 2 has length 2 with entries:
                      2              20.0000000000000000000000000
                      2             -20.0000000000000000000000000
vec 3 has length 2 with entries:
                      3              30.0000000000000000000000000
                      1             -30.0000000000000000000000000
vec 4 has length 2 with entries:
                      4              40.0000000000000000000000000
                      0             -40.0000000000000000000000000

Finished dumping matrix
`
	var buf bytes.Buffer
	m.DumpMatrix(&buf)
	actual := buf.String()
	if actual != expected {
		t.Logf("Expected output follows:\n%s", expected)
		t.Logf("Actual output follows:\n%s", actual)
		t.Fatalf("Mismatch between expected and actual matrix contents")
	}
}
예제 #4
0
// Test if we can query a packed matrix's dimensions.
func TestDims(t *testing.T) {
	for _, trials := range [...][2]int{
		{66, 7},  // Wide
		{55, 68}, // Tall
	} {
		nr, nc := trials[0], trials[1]
		m := clp.NewPackedMatrix()
		addColumns(m, nr, nc)
		r, c := m.Dims()
		if r != nr || c != nc {
			t.Fatalf("Expected %dx%d but saw %dx%d", nr, nc, r, c)
		}
	}
}
예제 #5
0
// Test if we can load a problem into a simplex model.
func TestLoadProblem(t *testing.T) {
	s := clp.NewSimplex()
	m := clp.NewPackedMatrix()
	s.LoadProblem(m, nil, nil, nil, nil)
}
예제 #6
0
// Test if we can add columns to a packed matrix.
func TestAddColumns(t *testing.T) {
	m := clp.NewPackedMatrix()
	addColumns(m, 1000, 1000)
}
예제 #7
0
// Test if we can create a packed matrix.
func TestCreateMatrix(t *testing.T) {
	_ = clp.NewPackedMatrix()
}