Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # Filename: linear_programming_tests.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- This script demonstrates operations on matrices and vectors commonly used in optimization problems.
- Functions:
- - create_w:
- Generates a weighted sum vector W based on conditions in column and headers.
- - change_functional:
- Modifies functional coefficients according to column.
- - create_f:
- Forms a functional vector F using weighted sums of A and B.
- - change_f:
- Updates the functional vector F based on specific row and column operations.
- Requirements:
- - Python 3.x
- Example inputs:
- A: Matrix of coefficients.
- B: Vector of constants.
- column:
- List indicating which rows to include.
- headers:
- List indicating which columns to increment.
- functional_coeff:
- Functional coefficients.
- Additional Notes:
- - The script assumes Python 3.x environment.
- - The inputs A, B, column, headers, and functional_coeff are required for proper functioning of the functions.
- """
- def create_w(A: list, B: list, column_list: list, headers_list: list) -> list:
- """
- Generates a weighted sum vector W based on conditions in column and headers.
- Args:
- A (list): Matrix of coefficients.
- B (list): Vector of constants.
- column_list (list): List indicating which rows to include.
- headers_list (list): List indicating which columns to increment.
- Returns:
- list: Weighted sum vector W.
- """
- weighted_sum_vector = [0] * (len(A[0]) + 1)
- for i, row in enumerate(A):
- if column_list[i][0].startswith("y"):
- for j in range(len(row)):
- weighted_sum_vector[j] += row[j]
- weighted_sum_vector[-1] += B[i]
- for j, header in enumerate(headers_list):
- if header[0].startswith("y"):
- weighted_sum_vector[j] += 1
- return weighted_sum_vector
- def change_functional(column_list: list, functional_coeff_list: list) -> list:
- """
- Modifies functional coefficients according to column.
- Args:
- column_list (list): List indicating which coefficients to use.
- functional_coeff_list (list): Original functional coefficients.
- Returns:
- list: New functional coefficients.
- """
- return [functional_coeff_list[int(col[0][1:]) - 1] for col in column_list]
- def create_f(A: list, B: list, functional_coeff_list: list) -> list:
- """
- Forms a functional vector F using weighted sums of A and B.
- Args:
- A (list): Matrix of coefficients.
- B (list): Vector of constants.
- functional_coeff_list (list): Functional coefficients.
- Returns:
- list: Functional vector F.
- """
- functional_vector = [0] * (len(A[0]) + 1)
- for i, row in enumerate(A):
- for j in range(len(row)):
- functional_vector[j] += functional_coeff_list[i] * row[j]
- functional_vector[-1] += functional_coeff_list[i] * B[i]
- return functional_vector
- def change_f(A: list, B: list, F: list, row_index: int, col_index: int) -> list:
- """
- Updates the functional vector F based on specific row and column operations.
- Args:
- A (list): Matrix of coefficients.
- B (list): Vector of constants.
- F (list): Functional vector to be updated.
- row_index (int): Index of the row to be used in the update.
- col_index (int): Index of the column to be used in the update.
- Returns:
- list: Updated functional vector F.
- """
- new_F = F[:]
- new_F[-1] += B[row_index] * F[col_index]
- for j in range(len(A[0])):
- if j == col_index:
- new_F[j] *= A[row_index][col_index]
- else:
- new_F[j] += F[col_index] * A[row_index][j]
- return new_F
- print("\t:: TESTING OPERATIONS ::")
- # Example inputs
- global_A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
- global_B = [10, 11, 12]
- global_column = [["y1"], ["n2"], ["y3"]]
- global_headers = [["y1"], ["n2"], ["y3"]]
- global_functional_coeff = [0.1, 0.2, 0.3]
- # Testing create_w
- W = create_w(global_A, global_B, global_column, global_headers)
- print(
- "\nCreateW output:\n\tExample inputs:\n\tA =",
- global_A,
- "\n\tB =",
- global_B,
- "\n\tcolumn =",
- global_column,
- "\n\theaders =",
- global_headers,
- "\n\tWeighted sum vector W =",
- W,
- ) # Expected output: [9, 10, 13, 22]
- # Testing change_functional
- new_functional = change_functional(global_column, global_functional_coeff)
- print(
- "\nChangeFunctional output:\n\tExample inputs:\n\tcolumn =",
- global_column,
- "\n\tfunctional_coeff =",
- global_functional_coeff,
- "\n\tNew functional coefficients =",
- new_functional,
- ) # Expected output: [0.1, 0.2, 0.3]
- # Testing create_f
- global_F = create_f(global_A, global_B, new_functional)
- print(
- "\nCreateF output:\n\tExample inputs:\n\tA =",
- global_A,
- "\n\tB =",
- global_B,
- "\n\tfunctional_coeff =",
- new_functional,
- "\n\tFunctional vector F =",
- global_F,
- ) # Expected output: [3.0, 3.6, 4
- # Testing change_f
- changed_F = change_f(global_A, global_B, global_F, 0, 1)
- print(
- "\nChangeF output:\n\tExample inputs:\n\tA =",
- global_A,
- "\n\tB =",
- global_B,
- "\n\tF =",
- global_F,
- "\n\trow_index =",
- 0,
- "\n\tcol_index =",
- 1,
- "\n\tUpdated functional vector F =",
- changed_F,
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement