@@ -2414,6 +2414,168 @@ func TestOptimizationProblem_ToLPStandardForm1_7(t *testing.T) {
2414
2414
}
2415
2415
}
2416
2416
2417
+ /*
2418
+ TestOptimizationProblem_ToLPStandardForm1_8
2419
+ Description:
2420
+
2421
+ Tests the LinearEqualityConstraintMatrices function properly produces
2422
+ a matrix:
2423
+ [ 1 -1 0 0 0 0 1 0 0 ]
2424
+ C = [ 0 0 1 -1 0 0 0 1 0 ]
2425
+ [ 0 0 0 0 1 -1 0 0 1 ]
2426
+ and
2427
+ b = [ 1 2 3 ]
2428
+ By creating a problem with 3 variables and 3 linear inequality constraints.
2429
+ The results should produce equality constraint matrix C
2430
+ with 3 rows and 6 columns and a vector b with 3 elements.
2431
+ */
2432
+ func TestOptimizationProblem_ToLPStandardForm1_8 (t * testing.T ) {
2433
+ // Constants
2434
+ p1 := problem .NewProblem ("TestOptimizationProblem_ToLPStandardForm1_8" )
2435
+ vv1 := p1 .AddVariableVector (3 )
2436
+ c1 := vv1 .AtVec (0 ).LessEq (1.0 )
2437
+ c2 := vv1 .AtVec (1 ).LessEq (2.0 )
2438
+ c3 := vv1 .AtVec (2 ).LessEq (3.0 )
2439
+
2440
+ p1 .Constraints = append (p1 .Constraints , c1 )
2441
+ p1 .Constraints = append (p1 .Constraints , c2 )
2442
+ p1 .Constraints = append (p1 .Constraints , c3 )
2443
+
2444
+ // Create good objective
2445
+ p1 .Objective = * problem .NewObjective (
2446
+ symbolic .K (3.14 ),
2447
+ problem .SenseMaximize ,
2448
+ )
2449
+
2450
+ // Algorithm
2451
+ p1Prime , _ , err := p1 .ToLPStandardForm1 ()
2452
+ if err != nil {
2453
+ t .Errorf ("unexpected error: %v" , err )
2454
+ }
2455
+
2456
+ A , b , err := p1Prime .LinearEqualityConstraintMatrices ()
2457
+ if err != nil {
2458
+ t .Errorf ("unexpected error: %v" , err )
2459
+ }
2460
+
2461
+ // Check that the number of rows is as expected.
2462
+ if A .Dims ()[0 ] != 3 {
2463
+ t .Errorf ("expected the number of rows to be %v; received %v" ,
2464
+ 3 , A .Dims ()[0 ])
2465
+ }
2466
+
2467
+ // Check that the number of columns is as expected.
2468
+ if A .Dims ()[1 ] != 9 {
2469
+ t .Errorf ("expected the number of columns to be %v; received %v" ,
2470
+ 6 , A .Dims ()[1 ])
2471
+ }
2472
+
2473
+ // Check that each of the entries in A is as expected.
2474
+ expectedA := getKMatrix .From ([][]float64 {
2475
+ {1.0 , - 1.0 , 0.0 , 0.0 , 0.0 , 0.0 , 1.0 , 0.0 , 0.0 },
2476
+ {0.0 , 0.0 , 1.0 , - 1.0 , 0.0 , 0.0 , 0.0 , 1.0 , 0.0 },
2477
+ {0.0 , 0.0 , 0.0 , 0.0 , 1.0 , - 1.0 , 0.0 , 0.0 , 1.0 },
2478
+ })
2479
+ AAsDense := A .ToDense ()
2480
+ expectedAAsDense := expectedA .ToDense ()
2481
+ if ! mat .EqualApprox (
2482
+ & AAsDense ,
2483
+ & expectedAAsDense ,
2484
+ 1e-10 ,
2485
+ ) {
2486
+ t .Errorf ("expected A to be %v; received %v" , expectedA , A )
2487
+ t .Errorf ("Variables in A: %v" , p1Prime .Variables )
2488
+ t .Errorf ("Linear coefficient for constraint 1: %v" , p1Prime .Constraints [0 ].Left ().(symbolic.ScalarExpression ).LinearCoeff (p1Prime .Variables ))
2489
+ }
2490
+
2491
+ // Check that the number of elements in b is as expected.
2492
+ if len (b ) != 3 {
2493
+ t .Errorf ("expected the number of elements in b to be %v; received %v" ,
2494
+ 3 , len (b ))
2495
+ }
2496
+ }
2497
+
2498
+ /*
2499
+ TestOptimizationProblem_ToLPStandardForm1_9
2500
+ Description:
2501
+
2502
+ Tests the LinearEqualityConstraintMatrices function properly produces
2503
+ a matrix:
2504
+ [ 1 -1 0 0 0 0 -1 0 0 ]
2505
+ C = [ 0 0 1 -1 0 0 0 -1 0 ]
2506
+ [ 0 0 0 0 1 -1 0 0 -1 ]
2507
+ and
2508
+ b = [ 1 2 3 ]
2509
+ By creating a problem with 3 variables and 3 linear inequality constraints.
2510
+ The results should produce equality constraint matrix C
2511
+ with 3 rows and 6 columns and a vector b with 3 elements.
2512
+ */
2513
+ func TestOptimizationProblem_ToLPStandardForm1_9 (t * testing.T ) {
2514
+ // Constants
2515
+ p1 := problem .NewProblem ("TestOptimizationProblem_ToLPStandardForm1_9" )
2516
+ vv1 := p1 .AddVariableVector (3 )
2517
+ c1 := vv1 .AtVec (0 ).GreaterEq (1.0 )
2518
+ c2 := vv1 .AtVec (1 ).GreaterEq (2.0 )
2519
+ c3 := vv1 .AtVec (2 ).GreaterEq (3.0 )
2520
+
2521
+ p1 .Constraints = append (p1 .Constraints , c1 )
2522
+ p1 .Constraints = append (p1 .Constraints , c2 )
2523
+ p1 .Constraints = append (p1 .Constraints , c3 )
2524
+
2525
+ // Create good objective
2526
+ p1 .Objective = * problem .NewObjective (
2527
+ symbolic .K (3.14 ),
2528
+ problem .SenseMaximize ,
2529
+ )
2530
+
2531
+ // Algorithm
2532
+ p1Prime , _ , err := p1 .ToLPStandardForm1 ()
2533
+ if err != nil {
2534
+ t .Errorf ("unexpected error: %v" , err )
2535
+ }
2536
+
2537
+ A , b , err := p1Prime .LinearEqualityConstraintMatrices ()
2538
+ if err != nil {
2539
+ t .Errorf ("unexpected error: %v" , err )
2540
+ }
2541
+
2542
+ // Check that the number of rows is as expected.
2543
+ if A .Dims ()[0 ] != 3 {
2544
+ t .Errorf ("expected the number of rows to be %v; received %v" ,
2545
+ 3 , A .Dims ()[0 ])
2546
+ }
2547
+
2548
+ // Check that the number of columns is as expected.
2549
+ if A .Dims ()[1 ] != 9 {
2550
+ t .Errorf ("expected the number of columns to be %v; received %v" ,
2551
+ 6 , A .Dims ()[1 ])
2552
+ }
2553
+
2554
+ // Check that each of the entries in A is as expected.
2555
+ expectedA := getKMatrix .From ([][]float64 {
2556
+ {1.0 , - 1.0 , 0.0 , 0.0 , 0.0 , 0.0 , - 1.0 , 0.0 , 0.0 },
2557
+ {0.0 , 0.0 , 1.0 , - 1.0 , 0.0 , 0.0 , 0.0 , - 1.0 , 0.0 },
2558
+ {0.0 , 0.0 , 0.0 , 0.0 , 1.0 , - 1.0 , 0.0 , 0.0 , - 1.0 },
2559
+ })
2560
+ AAsDense := A .ToDense ()
2561
+ expectedAAsDense := expectedA .ToDense ()
2562
+ if ! mat .EqualApprox (
2563
+ & AAsDense ,
2564
+ & expectedAAsDense ,
2565
+ 1e-10 ,
2566
+ ) {
2567
+ t .Errorf ("expected A to be %v; received %v" , expectedA , A )
2568
+ t .Errorf ("Variables in A: %v" , p1Prime .Variables )
2569
+ t .Errorf ("Linear coefficient for constraint 1: %v" , p1Prime .Constraints [0 ].Left ().(symbolic.ScalarExpression ).LinearCoeff (p1Prime .Variables ))
2570
+ }
2571
+
2572
+ // Check that the number of elements in b is as expected.
2573
+ if len (b ) != 3 {
2574
+ t .Errorf ("expected the number of elements in b to be %v; received %v" ,
2575
+ 3 , len (b ))
2576
+ }
2577
+ }
2578
+
2417
2579
/*
2418
2580
TestOptimizationProblem_CheckIfLinear1
2419
2581
Description:
0 commit comments