Team Ai
Apppublic

khushpatel2002/Optimization

sourceHugging Facemitupdated 3y agoView on Hugging Face
0likes
solver.cpython-310.pyc58 linesDownload Raw Back to __pycache__
1o

2sh%e|�@s&ddlZddlmZGdd�d�ZdS)�N)�EquationKindc@s0eZdZdZdd�Zdd�Zdd�Zdd	�Zd3S)�SolveraMSolver takes in the equations which have been parsed from the input,4    it converts them to matrix arrays and applies the matrix operations on 5    them.6 7    Args:8        objective_functions (list of Equation objects): List of objective functions.9        constraints (list of Equation objects): List of constraint equations.10    c	Cs:|dj|_|j�d�g|_t|�D]\}}|j�||�q|jD]}|j|d9<q#|D]}|j��D]}||jvrDd|j|<q8q1t|j�}d}t|j�D] \}}|jt	j11krrd|jdt|�<d|jdt|�<|d7}qR||kr�d|jdt|�<d|jdt|�<|d7}||ksw|��\|_
|_|_dS)Nr�Z�����g�?�s_�)�	variables�objective_functions�pop�constraints�	enumerate�append�keys�len�kindr�LEQ�str�convert_to_matrices�A�B�C)	�selfr	r�i�_�key�	temp_dict�slack�12constraint�r�2/home/patel/simplex_method/src/algorithm/solver.py�__init__
s6131415��16��zSolver.__init__cCst|�|j|j|j�\}}}t|���}d}tt|��D]}|||�dt||dfd��d�7}q|d�	|�7}|S)Nz'The vector of decision variables is : 17z : r��18zThe optimal solution is {}19)20�advanced_simplexrrr�list�values�ranger�round�format)r�objective_values�solution�variable_names�results_strrrrr�get_results2s(zSolver.get_resultscCs�t|j�}t|j�}t�||f�}t�d|f�}t�|df�}t|j�D]?\}}|j��D]\}}	t|j�	���21|�}22|	|||23f<q-|j��D]\}}	t|j�	���24|�}25|	|d|26f<qG|j||df<q$|||fS)a*Converts objective functions and constraints to matrices.27 28        Returns:29            A (numpy.ndarray): Coefficients matrix for constraints.30            B (numpy.ndarray): Right-hand side matrix for constraints.31            C (numpy.ndarray): Coefficients matrix for objective function.32        rr)rrr	�np�zerosrr�itemsr$r�index�bound)r�num_constraints�
num_variablesrrrrr�
variable_name�coefficient�jrrrr<s333435zSolver.convert_to_matricescCs�|j\}}t�|�}t�d|f�}d}t|j���|d�}	dd�tt|�|	�D�}	t	d�}36	|d7}tj37tj�|�dd	�}|D]
}|D]}
|
dkrMd}
qEqAt�
||�}t�t�
||�d�}t�
||�|}t�t�
||�d�}t|38|�d39kr~|||	fSt�|�}g}t|�D] }|||fdkr�|�||df|||f�q�|�tj�q�t�|�}t|j���}|||	|<|dd�|f|dd�|f<|dd�|f|dd�|f<|}40q0)a�Performs the advanced simplex algorithm to find the optimal solution.41 42        Args:43            A (numpy.ndarray): Coefficients matrix for constraints.44            b (numpy.ndarray): Right-hand side matrix for constraints.45            C (numpy.ndarray): Coefficients matrix for objective function.46 47        Returns:48            objective_values (numpy.ndarray): The values of the objective function variables.49            solution (float): The optimal solution value.50        rrNcSsi|]\}}||�qSrr)�.0r�valuerrr�51<dictcomp>lsz+Solver.advanced_simplex.<locals>.<dictcomp>�infTr!)�decimalsg-C��6?)�shaper.�eyer/r$r	r�zipr&�float�around�linalg�inv�matmulr'�abs�argminr
r;)rr�br�n�mr�C_B�countr+�
prev_solution�	B_inverse�rowr9�X_B�P_tabler)r*�entering_var_idx�ratiosr�exiting_var_idx�	temp_listrrrr#XsF5253��5455 56�zSolver.advanced_simplexN)�__name__�57__module__�__qualname__�__doc__r r-rr#rrrrrs%58r)�numpyr.�ast_parser.parserrrrrrr�<module>s