
    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)		CHOCO_CMDzThe CHOCO_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/choco_api.pyr   zCHOCO_CMD.__init__&   s'    " 		
    c                 $    | j                  d      S )Nz#choco-parsers-with-dependencies.jar)executableExtension)r   s    r   defaultPathzCHOCO_CMD.defaultPathA   s    ''(MNNr   c                     | j                  d      }| j                  | j                        xr | j                  |      S )zTrue if the solver is availablejava)r   
executabler   )r   	java_paths     r   	availablezCHOCO_CMD.availableD   s3    ,,V4	tyy)Hdooi.HHr   c                 >   | j                  d      }| j                  |      st        d      t        j                  j                  | j                        st        d| j                  z         | j                  |j                  ddd      \  }}}|j                          |j                  ||j                         	 t        j                  |       |dz   | j                  z   d	z   }| j                  |d| j                   dz  z  }|ddj                  | j                  D cg c]  \  }}| d|  c}}      z   z  }|d| z  }|j                  t        j                   k(  r|dz  }|j#                         r!| j$                  st'        j(                  d       t+        |d      }	t-        j.                  ||	|	d      }
|
dk7  rt        d| j                  z         t        j                  j                  |      s#t        j0                  }t        j2                  }d
}n| j5                  |      \  }}}| j7                  |||       |j9                  ||       |t        j:                  t        j0                  fvr|j=                  |       |S #  Y xY wc c}}w )z"Solve a well formulated lp problemr   zHPuLP: java needs to be installed and accesible in order to use CHOCO_CMDzPuLP: cannot execute mpslpsol)mpsSensez -cp "z%" org.chocosolver.parser.mps.ChocoMPSNz -tl i   z -maxz2CHOCO_CMD cannot solve the relaxation of a problemwT)stdoutstderrshellr   z$PuLP: Error while trying to execute )r   r   r   osr   existscreate_tmp_filesnamecheckDuplicateVarswriteMPSsenseremover   joinr   r   
LpMaximizeisMIPr   warningswarnopenr   callLpStatusNotSolvedLpSolutionNoSolutionFoundreadsoldelete_tmp_filesassignStatusLpStatusInfeasibleassignVarsVals)r   r    r   tmpMpstmpLptmpSolcmdkeyvaluepipereturn_codestatus
status_solvaluess                 r   actualSolvezCHOCO_CMD.actualSolveI   sD   ,,V4	y)!Z  ww~~dii(!"9DII"EFF $ 5 5bggudE Rv

FRXX.	IIf ("TYY.1XX>>%U4>>*+d22CsSXX$,,OJC#aw/OPPP6(|88y+++7NC88:88RS FC  ooc$t4P!!"H499"TUUww~~f%00F"<<JF)-f)=&FFJfeV4

+)66	8S8STTf%C	 Ps   ?J &JJc                    t         j                  t         j                  t         j                  t         j                  d}t         j                  t         j
                  t         j                  t         j                  d}t         j                  }t         j                  }i }t        |       5 }|j                         }ddd       D cg c]  }|dd dvs|j                          }}t        |      s|||fS |d   dd dk(  r|d   dd }	||	   }||	   }|dd D ]#  }
|
j                         \  }}t        |      ||<   % |||fS # 1 sw Y   xY wc c}w )zRead a Choco solution file)zOPTIMUM FOUNDSATISFIABLEUNSATISFIABLEUNKNOWNNr   )zo zc r   zs r   )r   LpStatusOptimalr<   r7   LpSolutionOptimalLpSolutionIntegerFeasibleLpSolutionInfeasibler8   r5   	readlinesstriplensplitfloat)filenamechocoStatuschocoSolStatusrF   
sol_statusrH   fcontentl
status_strliner+   rC   s                r   r9   zCHOCO_CMD.readsol|   sS   
 '66$44&99 22	
 '88$>>&;; ::	
 ,,88
(^ 	$qkkmG	$&-K2Al1J1779KK7|6:--1:bq>T! ABJ ,F'
3JABK 	(D**,KD% <F4L	( vz))	$ 	$Ks   ,E	EEE)NFTTNN)__name__
__module____qualname____doc__r+   r   r   r   rI   staticmethodr9    r   r   r
   r
   !   sI    D 
6OI
1f !* !*r   r
   )	corer   r   r   r(    r   r3   r
   re   r   r   <module>rh      s$   6 < ; 	  }* }*r   