
    kh                     J    d dl mZmZmZ ddlZddlmZ ddlZ G d de      Zy)   )LpSolver_CMD
subprocessPulpSolverError    N   )	constantsc                   J    e Zd ZdZd Z	 	 	 	 	 	 ddZd Zd Zd Ze	d        Z
y)		MIPCL_CMDzThe MIPCL_CMD solverNc           	      <    t        j                  | ||||||       y)a  
        :param bool mip: if False, assume LP even if integer variables
        :param bool msg: if False, no log is shown
        :param float timeLimit: maximum time for solver (in seconds)
        :param list options: list of additional options to pass to solver
        :param bool keepFiles: if True, files are saved in the current directory and not deleted after solving
        :param str path: path to the solver binary
        )mipmsg	timeLimitoptionspath	keepFilesN)r   __init__)selfr   r   r   r   r   r   s          y/var/www/html/SchoolMeal/SchoolMeal/pds_admin_SchoolMeal/Backend/venv/lib/python3.12/site-packages/pulp/apis/mipcl_api.pyr   zMIPCL_CMD.__init__&   s'    " 		
    c                 $    | j                  d      S )N	mps_mipcl)executableExtensionr   s    r   defaultPathzMIPCL_CMD.defaultPathA   s    ''44r   c                 8    | j                  | j                        S )zTrue if the solver is available)
executabler   r   s    r   	availablezMIPCL_CMD.availableD   s    tyy))r   c                 ~   | j                  | j                        st        d| j                  z         | j                  |j                  dd      \  }}|j
                  t        j                  k(  r%t        j                  d       ||j                   z  }|j                          |j                  d       |j                  ||j
                         	 t        j                  |       | j                  }|d| z  }|d| z  }| j                   |d
| j                    z  }| j"                  D ]
  }|d|z   z  } |j%                         r!| j&                  st        j                  d       | j(                  rd	}nt+        t        j,                  d      }t/        j0                  |j3                         ||      }|j
                  t        j                  k(  r||j                   z  }|dk7  rt        d| j                  z         t        j                  j5                  |      s#t        j6                  }t        j8                  }	d	}
n| j;                  |      \  }}
}	| j=                  ||       |j?                  ||	       |t        j@                  t        j6                  fvr|jC                  |
       |S #  Y xY w)z"Solve a well formulated lp problemzPuLP: cannot execute mpssolz^MIPCL_CMD does not allow maximization, we will minimize the inverse of the objective function.4   )mpsSense z
 -solfile Nz -time z2MIPCL_CMD cannot solve the relaxation of a problemw)stdoutstderrr   z$PuLP: Error while trying to execute )"r   r   r   create_tmp_filesnamesenser   
LpMaximizewarningswarn	objectivecheckDuplicateVarscheckLengthVarswriteMPSosremover   r   isMIPr   r   opendevnullr   callsplitexistsLpStatusNotSolvedLpSolutionNoSolutionFoundreadsoldelete_tmp_filesassignStatusLpStatusInfeasibleassignVarsVals)r   lptmpMpstmpSolcmdoptionpipereturn_codestatus
status_solvaluess              r   actualSolvezMIPCL_CMD.actualSolveH   sL   tyy)!"9DII"EFF..rwwuE88y+++ MMJ 2<<-B

2
FRXX.	IIf ii6(|F8$$>>%WT^^,--Cll 	 F3<C	 88:88RS88D

C(D oociik$tL88y+++2<<-B!!"H499"TUUww~~f%00F"<<JF)-f)=&FFJff-

+)66	8S8STTf%E	s   J7 7J<c                    t        |       5 }|j                         }ddd       D cg c]  }|j                          }}i }t        |      s!t        j
                  |t        j                  fS |d   }|dk(  r!t        j                  |t        j                  fS |j                         \  }}t        t        |            dk\  r!t        j                  |t        j                  fS |dd D ]#  }|j                         \  }	}t        |      ||	<   % t        j                  |t        j                  fS # 1 sw Y   xY wc c}w )zRead a MIPCL solution fileNr   z=infeas=g  vH7Br   )r4   	readlinesstriplenr   r9   r:   r>   LpSolutionInfeasibler7   absfloatLpStatusUnboundedLpSolutionUnboundedLpStatusOptimalLpSolutionIntegerFeasible)
filenamefcontentlrI   
first_liner-   valueliner(   s
             r   r;   zMIPCL_CMD.readsol   s*    (^ 	$qkkmG	$&-.1779..7|++33 
 QZ
#//9W9WWW%++-	5 uU|...	8U8UUUABK 	(D**,KD% <F4L	(
 ((&)2U2UUU/	$ 	$.s   D2D?2D<)NFTTNN)__name__
__module____qualname____doc__r(   r   r   r   rJ   staticmethodr;    r   r   r
   r
   !   sI    D 
65*6p V Vr   r
   )	corer   r   r   r1    r   r+   r
   rb   r   r   <module>re      s&   6 < ; 	  yV yVr   