Advertisement
Python253

linear_programming_tests

May 27th, 2024
508
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.38 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Filename: linear_programming_tests.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. This script demonstrates operations on matrices and vectors commonly used in optimization problems.
  9.  
  10. Functions:
  11.    - create_w:
  12.        Generates a weighted sum vector W based on conditions in column and headers.
  13.    - change_functional:
  14.        Modifies functional coefficients according to column.
  15.    - create_f:
  16.        Forms a functional vector F using weighted sums of A and B.
  17.    - change_f:
  18.        Updates the functional vector F based on specific row and column operations.
  19.  
  20. Requirements:
  21.    - Python 3.x
  22.  
  23. Example inputs:
  24.    A: Matrix of coefficients.
  25.    B: Vector of constants.
  26.    column:
  27.        List indicating which rows to include.
  28.    headers:
  29.        List indicating which columns to increment.
  30.    functional_coeff:
  31.        Functional coefficients.
  32.  
  33. Additional Notes:
  34.    - The script assumes Python 3.x environment.
  35.    - The inputs A, B, column, headers, and functional_coeff are required for proper functioning of the functions.
  36. """
  37.  
  38. def create_w(A: list, B: list, column_list: list, headers_list: list) -> list:
  39.     """
  40.    Generates a weighted sum vector W based on conditions in column and headers.
  41.  
  42.    Args:
  43.    A (list): Matrix of coefficients.
  44.    B (list): Vector of constants.
  45.    column_list (list): List indicating which rows to include.
  46.    headers_list (list): List indicating which columns to increment.
  47.  
  48.    Returns:
  49.    list: Weighted sum vector W.
  50.    """
  51.     weighted_sum_vector = [0] * (len(A[0]) + 1)
  52.  
  53.     for i, row in enumerate(A):
  54.         if column_list[i][0].startswith("y"):
  55.             for j in range(len(row)):
  56.                 weighted_sum_vector[j] += row[j]
  57.             weighted_sum_vector[-1] += B[i]
  58.  
  59.     for j, header in enumerate(headers_list):
  60.         if header[0].startswith("y"):
  61.             weighted_sum_vector[j] += 1
  62.  
  63.     return weighted_sum_vector
  64.  
  65. def change_functional(column_list: list, functional_coeff_list: list) -> list:
  66.     """
  67.    Modifies functional coefficients according to column.
  68.  
  69.    Args:
  70.    column_list (list): List indicating which coefficients to use.
  71.    functional_coeff_list (list): Original functional coefficients.
  72.  
  73.    Returns:
  74.    list: New functional coefficients.
  75.    """
  76.     return [functional_coeff_list[int(col[0][1:]) - 1] for col in column_list]
  77.  
  78. def create_f(A: list, B: list, functional_coeff_list: list) -> list:
  79.     """
  80.    Forms a functional vector F using weighted sums of A and B.
  81.  
  82.    Args:
  83.    A (list): Matrix of coefficients.
  84.    B (list): Vector of constants.
  85.    functional_coeff_list (list): Functional coefficients.
  86.  
  87.    Returns:
  88.    list: Functional vector F.
  89.    """
  90.     functional_vector = [0] * (len(A[0]) + 1)
  91.  
  92.     for i, row in enumerate(A):
  93.         for j in range(len(row)):
  94.             functional_vector[j] += functional_coeff_list[i] * row[j]
  95.         functional_vector[-1] += functional_coeff_list[i] * B[i]
  96.  
  97.     return functional_vector
  98.  
  99. def change_f(A: list, B: list, F: list, row_index: int, col_index: int) -> list:
  100.     """
  101.    Updates the functional vector F based on specific row and column operations.
  102.  
  103.    Args:
  104.    A (list): Matrix of coefficients.
  105.    B (list): Vector of constants.
  106.    F (list): Functional vector to be updated.
  107.    row_index (int): Index of the row to be used in the update.
  108.    col_index (int): Index of the column to be used in the update.
  109.  
  110.    Returns:
  111.    list: Updated functional vector F.
  112.    """
  113.     new_F = F[:]
  114.     new_F[-1] += B[row_index] * F[col_index]
  115.  
  116.     for j in range(len(A[0])):
  117.         if j == col_index:
  118.             new_F[j] *= A[row_index][col_index]
  119.         else:
  120.             new_F[j] += F[col_index] * A[row_index][j]
  121.  
  122.     return new_F
  123.  
  124. print("\t:: TESTING OPERATIONS ::")
  125.  
  126. # Example inputs
  127. global_A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
  128. global_B = [10, 11, 12]
  129. global_column = [["y1"], ["n2"], ["y3"]]
  130. global_headers = [["y1"], ["n2"], ["y3"]]
  131. global_functional_coeff = [0.1, 0.2, 0.3]
  132.  
  133. # Testing create_w
  134. W = create_w(global_A, global_B, global_column, global_headers)
  135. print(
  136.     "\nCreateW output:\n\tExample inputs:\n\tA =",
  137.     global_A,
  138.     "\n\tB =",
  139.     global_B,
  140.     "\n\tcolumn =",
  141.     global_column,
  142.     "\n\theaders =",
  143.     global_headers,
  144.     "\n\tWeighted sum vector W =",
  145.     W,
  146. )  # Expected output: [9, 10, 13, 22]
  147.  
  148. # Testing change_functional
  149. new_functional = change_functional(global_column, global_functional_coeff)
  150. print(
  151.     "\nChangeFunctional output:\n\tExample inputs:\n\tcolumn =",
  152.     global_column,
  153.     "\n\tfunctional_coeff =",
  154.     global_functional_coeff,
  155.     "\n\tNew functional coefficients =",
  156.     new_functional,
  157. )  # Expected output: [0.1, 0.2, 0.3]
  158.  
  159. # Testing create_f
  160. global_F = create_f(global_A, global_B, new_functional)
  161. print(
  162.     "\nCreateF output:\n\tExample inputs:\n\tA =",
  163.     global_A,
  164.     "\n\tB =",
  165.     global_B,
  166.     "\n\tfunctional_coeff =",
  167.     new_functional,
  168.     "\n\tFunctional vector F =",
  169.     global_F,
  170. )  # Expected output: [3.0, 3.6, 4
  171.  
  172. # Testing change_f
  173. changed_F = change_f(global_A, global_B, global_F, 0, 1)
  174. print(
  175.     "\nChangeF output:\n\tExample inputs:\n\tA =",
  176.     global_A,
  177.     "\n\tB =",
  178.     global_B,
  179.     "\n\tF =",
  180.     global_F,
  181.     "\n\trow_index =",
  182.     0,
  183.     "\n\tcol_index =",
  184.     1,
  185.     "\n\tUpdated functional vector F =",
  186.     changed_F,
  187. )
  188.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement