|
5 | 5 |
|
6 | 6 | "github.com/MatProGo-dev/MatProInterface.go/mpiErrors"
|
7 | 7 | "github.com/MatProGo-dev/MatProInterface.go/optim"
|
| 8 | + getKVector "github.com/MatProGo-dev/SymbolicMath.go/get/KVector" |
8 | 9 | "github.com/MatProGo-dev/SymbolicMath.go/symbolic"
|
| 10 | + "gonum.org/v1/gonum/mat" |
9 | 11 | )
|
10 | 12 |
|
11 | 13 | // OptimizationProblem represents the overall constrained linear optimization model to be
|
@@ -340,3 +342,196 @@ func (op *OptimizationProblem) IsLinear() bool {
|
340 | 342 | // All Checks Passed!
|
341 | 343 | return true
|
342 | 344 | }
|
| 345 | + |
| 346 | +/* |
| 347 | +LinearInequalityConstraintMatrices |
| 348 | +Description: |
| 349 | +
|
| 350 | + Returns the linear INEQUALITY constraint matrices and vectors. |
| 351 | + For all linear inequality constraints, we assemble them into the form: |
| 352 | + Ax <= b |
| 353 | + Where A is the matrix of coefficients, x is the vector of variables, and b is the vector of constants. |
| 354 | + We return A and b. |
| 355 | +*/ |
| 356 | +func (op *OptimizationProblem) LinearInequalityConstraintMatrices() (symbolic.KMatrix, symbolic.KVector) { |
| 357 | + // Setup |
| 358 | + |
| 359 | + // Collect the Variables of this Problem |
| 360 | + x := op.Variables |
| 361 | + |
| 362 | + // Iterate through all constraints and collect the linear constraints |
| 363 | + // into a matrix and vector. |
| 364 | + scalar_constraints := make([]symbolic.ScalarConstraint, 0) |
| 365 | + vector_constraints := make([]symbolic.VectorConstraint, 0) |
| 366 | + for _, constraint := range op.Constraints { |
| 367 | + // Skip this constraint if it is not linear |
| 368 | + if !constraint.IsLinear() { |
| 369 | + continue |
| 370 | + } |
| 371 | + // Skip this constraint if it is not an inequality |
| 372 | + if constraint.ConstrSense() == symbolic.SenseEqual { |
| 373 | + continue |
| 374 | + } |
| 375 | + switch c := constraint.(type) { |
| 376 | + case symbolic.ScalarConstraint: |
| 377 | + scalar_constraints = append(scalar_constraints, c) |
| 378 | + case symbolic.VectorConstraint: |
| 379 | + vector_constraints = append(vector_constraints, c) |
| 380 | + } |
| 381 | + } |
| 382 | + |
| 383 | + // Create the matrix and vector elements from the scalar constraints |
| 384 | + A_components_scalar := make([]mat.VecDense, len(scalar_constraints)) |
| 385 | + b_components_scalar := make([]float64, len(scalar_constraints)) |
| 386 | + for ii, constraint := range scalar_constraints { |
| 387 | + A_components_scalar[ii], b_components_scalar[ii] = constraint.LinearInequalityConstraintRepresentation(x) |
| 388 | + } |
| 389 | + |
| 390 | + // Create the matrix and vector elements from the vector constraints |
| 391 | + A_components_vector := make([]mat.Dense, len(vector_constraints)) |
| 392 | + b_components_vector := make([]mat.VecDense, len(vector_constraints)) |
| 393 | + for ii, constraint := range vector_constraints { |
| 394 | + A_components_vector[ii], b_components_vector[ii] = constraint.LinearInequalityConstraintRepresentation(x) |
| 395 | + } |
| 396 | + |
| 397 | + // Assemble the matrix and vector components |
| 398 | + var AOut symbolic.Expression |
| 399 | + var bOut symbolic.Expression |
| 400 | + scalar_constraint_matrices_exist := len(A_components_scalar) > 0 |
| 401 | + if scalar_constraint_matrices_exist { |
| 402 | + AOut = symbolic.VecDenseToKVector(A_components_scalar[0]).Transpose() |
| 403 | + for ii := 1; ii < len(A_components_scalar); ii++ { |
| 404 | + AOut = symbolic.VStack( |
| 405 | + AOut, |
| 406 | + symbolic.VecDenseToKVector(A_components_scalar[ii]).Transpose(), |
| 407 | + ) |
| 408 | + } |
| 409 | + bOut = getKVector.From(b_components_scalar) |
| 410 | + } |
| 411 | + |
| 412 | + vector_constraint_matrices_exist := len(A_components_vector) > 0 |
| 413 | + if vector_constraint_matrices_exist { |
| 414 | + // Create the matrix, if it doesn't already exist |
| 415 | + if !scalar_constraint_matrices_exist { |
| 416 | + AOut = symbolic.DenseToKMatrix(A_components_vector[0]) |
| 417 | + bOut = symbolic.VecDenseToKVector(b_components_vector[0]) |
| 418 | + } else { |
| 419 | + AOut = symbolic.VStack( |
| 420 | + AOut, |
| 421 | + symbolic.DenseToKMatrix(A_components_vector[0]), |
| 422 | + ) |
| 423 | + bOut = symbolic.VStack( |
| 424 | + bOut, |
| 425 | + symbolic.VecDenseToKVector(b_components_vector[0]), |
| 426 | + ) |
| 427 | + } |
| 428 | + for ii := 1; ii < len(A_components_vector); ii++ { |
| 429 | + AOut = symbolic.VStack( |
| 430 | + AOut, |
| 431 | + symbolic.DenseToKMatrix(A_components_vector[ii]), |
| 432 | + ) |
| 433 | + bOut = symbolic.VStack( |
| 434 | + bOut, |
| 435 | + symbolic.VecDenseToKVector(b_components_vector[ii]), |
| 436 | + ) |
| 437 | + } |
| 438 | + } |
| 439 | + |
| 440 | + return AOut.(symbolic.KMatrix), bOut.(symbolic.KVector) |
| 441 | +} |
| 442 | + |
| 443 | +/* |
| 444 | +LinearEqualityConstraintMatrices |
| 445 | +Description: |
| 446 | +
|
| 447 | + Returns the linear EQUALITY constraint matrices and vectors. |
| 448 | + For all linear equality constraints, we assemble them into the form: |
| 449 | + Cx = d |
| 450 | + Where C is the matrix of coefficients, x is the vector of variables, and d is the vector of constants. |
| 451 | + We return C and d. |
| 452 | +*/ |
| 453 | +func (op *OptimizationProblem) LinearEqualityConstraintMatrices() (symbolic.KMatrix, symbolic.KVector) { |
| 454 | + // Setup |
| 455 | + |
| 456 | + // Collect the Variables of this Problem |
| 457 | + x := op.Variables |
| 458 | + |
| 459 | + // Iterate through all constraints and collect the linear constraints |
| 460 | + // into a matrix and vector. |
| 461 | + scalar_constraints := make([]symbolic.ScalarConstraint, 0) |
| 462 | + vector_constraints := make([]symbolic.VectorConstraint, 0) |
| 463 | + for _, constraint := range op.Constraints { |
| 464 | + // Skip this constraint if it is not linear |
| 465 | + if !constraint.IsLinear() { |
| 466 | + continue |
| 467 | + } |
| 468 | + // Skip this constraint if it is not an equality |
| 469 | + if constraint.ConstrSense() != symbolic.SenseEqual { |
| 470 | + continue |
| 471 | + } |
| 472 | + switch c := constraint.(type) { |
| 473 | + case symbolic.ScalarConstraint: |
| 474 | + scalar_constraints = append(scalar_constraints, c) |
| 475 | + case symbolic.VectorConstraint: |
| 476 | + vector_constraints = append(vector_constraints, c) |
| 477 | + } |
| 478 | + } |
| 479 | + |
| 480 | + // Create the matrix and vector elements from the scalar constraints |
| 481 | + C_components_scalar := make([]mat.VecDense, len(scalar_constraints)) |
| 482 | + d_components_scalar := make([]float64, len(scalar_constraints)) |
| 483 | + for ii, constraint := range scalar_constraints { |
| 484 | + C_components_scalar[ii], d_components_scalar[ii] = constraint.LinearEqualityConstraintRepresentation(x) |
| 485 | + } |
| 486 | + |
| 487 | + // Create the matrix and vector elements from the vector constraints |
| 488 | + C_components_vector := make([]mat.Dense, len(vector_constraints)) |
| 489 | + d_components_vector := make([]mat.VecDense, len(vector_constraints)) |
| 490 | + for ii, constraint := range vector_constraints { |
| 491 | + C_components_vector[ii], d_components_vector[ii] = constraint.LinearEqualityConstraintRepresentation(x) |
| 492 | + } |
| 493 | + |
| 494 | + // Assemble the matrix and vector components |
| 495 | + var COut symbolic.Expression |
| 496 | + var dOut symbolic.Expression |
| 497 | + scalar_constraint_matrices_exist := len(C_components_scalar) > 0 |
| 498 | + if scalar_constraint_matrices_exist { |
| 499 | + COut = symbolic.VecDenseToKVector(C_components_scalar[0]).Transpose() |
| 500 | + for ii := 1; ii < len(C_components_scalar); ii++ { |
| 501 | + COut = symbolic.VStack( |
| 502 | + COut, |
| 503 | + symbolic.VecDenseToKVector(C_components_scalar[ii]).Transpose(), |
| 504 | + ) |
| 505 | + } |
| 506 | + dOut = getKVector.From(d_components_scalar) |
| 507 | + } |
| 508 | + vector_constraint_matrices_exist := len(C_components_vector) > 0 |
| 509 | + |
| 510 | + if vector_constraint_matrices_exist { |
| 511 | + // Create the matrix, if it doesn't already exist |
| 512 | + if !scalar_constraint_matrices_exist { |
| 513 | + COut = symbolic.DenseToKMatrix(C_components_vector[0]) |
| 514 | + dOut = symbolic.VecDenseToKVector(d_components_vector[0]) |
| 515 | + } else { |
| 516 | + COut = symbolic.VStack( |
| 517 | + COut, |
| 518 | + symbolic.DenseToKMatrix(C_components_vector[0]), |
| 519 | + ) |
| 520 | + dOut = symbolic.VStack( |
| 521 | + dOut, |
| 522 | + symbolic.VecDenseToKVector(d_components_vector[0]), |
| 523 | + ) |
| 524 | + } |
| 525 | + for ii := 1; ii < len(C_components_vector); ii++ { |
| 526 | + COut = symbolic.VStack( |
| 527 | + COut, |
| 528 | + symbolic.DenseToKMatrix(C_components_vector[ii]), |
| 529 | + ) |
| 530 | + dOut = symbolic.VStack( |
| 531 | + dOut, |
| 532 | + symbolic.VecDenseToKVector(d_components_vector[ii]), |
| 533 | + ) |
| 534 | + } |
| 535 | + } |
| 536 | + return COut.(symbolic.KMatrix), dOut.(symbolic.KVector) |
| 537 | +} |
0 commit comments