Skip to content

Commit 07ac53c

Browse files
committed
Added tests for a few more corner cases of LPStandardForm1 inputs
1 parent 227ffe2 commit 07ac53c

1 file changed

Lines changed: 113 additions & 0 deletions

File tree

testing/problem/optimization_problem_test.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2299,3 +2299,116 @@ func TestOptimizationProblem_ToLPStandardForm5(t *testing.T) {
22992299
}
23002300
}
23012301
}
2302+
2303+
/*
2304+
TestOptimizationProblem_ToLPStandardForm6
2305+
Description:
2306+
2307+
This test verifies that the ToLPStandardForm function properly handles
2308+
a simple problem with a single, scalar linear inequality constraint.
2309+
The problem will have:
2310+
- a constant objective
2311+
- 2 variables,
2312+
- and a single scalar linear inequality constraint (SenseLessThanEqual).
2313+
The result should be a problem with 2*2+1 = 5 variables and 1 constraint.
2314+
*/
2315+
func TestOptimizationProblem_ToLPStandardForm6(t *testing.T) {
2316+
// Constants
2317+
p1 := problem.NewProblem("TestOptimizationProblem_ToLPStandardForm6")
2318+
v1 := p1.AddVariable()
2319+
p1.AddVariable()
2320+
c1 := v1.LessEq(1.0)
2321+
2322+
p1.Constraints = append(p1.Constraints, c1)
2323+
2324+
// Create good objective
2325+
p1.Objective = *problem.NewObjective(
2326+
symbolic.K(3.14),
2327+
problem.SenseMaximize,
2328+
)
2329+
2330+
// Algorithm
2331+
p2, _, err := p1.ToLPStandardForm1()
2332+
if err != nil {
2333+
t.Errorf("unexpected error: %v", err)
2334+
}
2335+
2336+
// Check that the number of variables is as expected.
2337+
expectedNumVariables := 0
2338+
expectedNumVariables += 2 * len(p1.Variables) // original variables (positive and negative halfs)
2339+
expectedNumVariables += len(p1.Constraints) // slack variables
2340+
if len(p2.Variables) != expectedNumVariables {
2341+
t.Errorf("expected the number of variables to be %v; received %v",
2342+
expectedNumVariables, len(p2.Variables))
2343+
}
2344+
2345+
// Check that the number of constraints is as expected.
2346+
if len(p2.Constraints) != 1 {
2347+
t.Errorf("expected the number of constraints to be %v; received %v",
2348+
expectedNumVariables, len(p2.Constraints))
2349+
}
2350+
2351+
// Verify that all constraints are equality constraints
2352+
for _, c := range p2.Constraints {
2353+
if c.ConstrSense() != symbolic.SenseEqual {
2354+
t.Errorf("expected the constraint to be an equality constraint; received %v",
2355+
c.ConstrSense())
2356+
}
2357+
}
2358+
}
2359+
2360+
/*
2361+
TestOptimizationProblem_ToLPStandardForm7
2362+
Description:
2363+
2364+
This test verifies that the ToLPStandardForm function properly handles
2365+
a simple problem with a single, scalar equality constraint.
2366+
The problem will have:
2367+
- a constant objective
2368+
- 2 variables,
2369+
- and a single scalar linear equality constraint (SenseEqual).
2370+
The result should be a problem with 2*2 = 4 variables and 1 constraint.
2371+
*/
2372+
func TestOptimizationProblem_ToLPStandardForm7(t *testing.T) {
2373+
// Constants
2374+
p1 := problem.NewProblem("TestOptimizationProblem_ToLPStandardForm7")
2375+
v1 := p1.AddVariable()
2376+
p1.AddVariable()
2377+
c1 := v1.Eq(1.0)
2378+
2379+
p1.Constraints = append(p1.Constraints, c1)
2380+
2381+
// Create good objective
2382+
p1.Objective = *problem.NewObjective(
2383+
symbolic.K(3.14),
2384+
problem.SenseMaximize,
2385+
)
2386+
2387+
// Algorithm
2388+
p2, _, err := p1.ToLPStandardForm1()
2389+
if err != nil {
2390+
t.Errorf("unexpected error: %v", err)
2391+
}
2392+
2393+
// Check that the number of variables is as expected.
2394+
expectedNumVariables := 0
2395+
expectedNumVariables += 2 * len(p1.Variables) // original variables (positive and negative halfs)
2396+
if len(p2.Variables) != expectedNumVariables {
2397+
t.Errorf("expected the number of variables to be %v; received %v",
2398+
expectedNumVariables, len(p2.Variables))
2399+
}
2400+
2401+
// Check that the number of constraints is as expected.
2402+
if len(p2.Constraints) != 1 {
2403+
t.Errorf("expected the number of constraints to be %v; received %v",
2404+
expectedNumVariables, len(p2.Constraints))
2405+
}
2406+
2407+
// Verify that all constraints are equality constraints
2408+
for _, c := range p2.Constraints {
2409+
if c.ConstrSense() != symbolic.SenseEqual {
2410+
t.Errorf("expected the constraint to be an equality constraint; received %v",
2411+
c.ConstrSense())
2412+
}
2413+
}
2414+
}

0 commit comments

Comments
 (0)