
    kh@                     |    d dl mZmZmZmZmZ d dl mZmZ ddlZddl	m
Z
 dZ G d d	e      ZeZda G d
 de      Zy)   )LpSolver_CMDLpSolver
subprocessPulpSolverErrorclock)operating_systemlog    N   )	constantsglpsolc                   @    e Zd ZdZd Z	 	 	 	 	 	 ddZd Zd Zd Zd Z	y)	GLPK_CMDzThe GLPK LP 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          x/var/www/html/SchoolMeal/SchoolMeal/pds_admin_SchoolMeal/Backend/venv/lib/python3.12/site-packages/pulp/apis/glpk_api.pyr   zGLPK_CMD.__init__(   s'    " 		
    c                 ,    | j                  t              S N)executableExtension	glpk_pathr   s    r   defaultPathzGLPK_CMD.defaultPathC   s    ''	22r   c                 8    | j                  | j                        S )True if the solver is available)
executabler   r   s    r   	availablezGLPK_CMD.availableF   s    tyy))r   c                    | j                  | j                        st        d| j                  z         | j                  |j                  dd      \  }}|j                  |d       dd|d|g}| j                  r&|j                  d	t        | j                        g       | j                  s|j                  d
       |j                  | j                         t               | _        | j                  s| j                  |d<   t        t         j"                  d      }t$        dk(  rQt'        j(                         }|xj*                  t&        j,                  z  c_        t'        j.                  ||||      }nt'        j.                  |||      }|rt        d| j                  z         |j1                          nt         j                  dk7  r0t!        j2                  t         j4                  | j                  |      }n>t!        j6                  t         j4                  | j                  | j                        |      }|dk(  rt        d| j                  z         | xj                  t               z  c_        t         j                  j9                  |      st        d| j                  z         | j;                  |      \  }}	|j=                  |	       |j?                  |       | jA                  ||       |S )"Solve a well formulated lp problemzPuLP: cannot execute lpsolr
   )writeSOSr   z--cpxlpz-oz--tmlimz--nomipwwin)stdoutstderrstartupinfo)r,   r-   z$PuLP: Error while trying to execute nt   zPuLP: Error while executing )!r#   r   r   create_tmp_filesnamewriteLPr   extendstrr   appendr   r   solution_timer   openosdevnullr   r   STARTUPINFOdwFlagsSTARTF_USESHOWWINDOWcallclosespawnvpP_WAITspawnvexistsreadsolassignVarsValsassignStatusdelete_tmp_files)
r   r'   tmpLptmpSolprocpiper.   rcstatusvaluess
             r   actualSolvezGLPK_CMD.actualSolveJ   s7   tyy)!"9DII"EFF--bggtUCv


51
%)UD&9>>KKC$789xxKK	"DLL!"WxxiiDG

C(D5((446##z'F'FF#__d  __T$tD%:TYYF  JJLww$ZZ		499d;YYryy$//$))*DdKSy%:TYYF  	eg%ww~~f%!"@499"LMMf-
&!
eV,r   c           
         t        |      5 }|j                          t        |j                         j                         d         }t        |j                         j                         d         }|j                          |j                         dd }t        j
                  t        j
                  t        j
                  t        j                  t        j                  t        j                  t        j                  t        j                  d}||vrt        d      ||   }|dv }i }	t        d      D ]  }
|j                           t        |      D ]?  }
|j                         j                         }t        |      dk(  s0|j                          A t        d	      D ]  }
|j                           t        |      D ]  }
|j                         j                         }|d   }t        |      dk(  r#d
d
g|j                         j                         z   }|r/|d   dk(  rt        t        |d	               }nt        |d         }nt        |d	         }||	|<    	 ddd       ||	fS # 1 sw Y   	fS xY w)zRead a GLPK solution filer      )INTEGER OPTIMALINTEGER NON-OPTIMALOPTIMALzINFEASIBLE (FINAL)INTEGER UNDEFINED	UNBOUNDED	UNDEFINEDINTEGER EMPTYzUnknown status returned by GLPK)rT   rS   rV   rY      r      r
   *N)r8   readlineintsplitr   LpStatusOptimalLpStatusInfeasibleLpStatusUndefinedLpStatusUnboundedr   rangelenfloat)r   filenamefrowscolsstatusString
glpkStatusrM   	isIntegerrN   iliner2   values                 r   rD   zGLPK_CMD.readsol|   s,   (^ .	%qJJLqzz|))+A./Dqzz|))+A./DJJL::<2.L#,#<#<'0'@'@$44&/&B&B%.%@%@&88&88!*!=!=	J :-%&GHH-F$ ) I F1X 

4[ !zz|))+t9>JJL! 1X 

4[ %zz|))+Awt9>q6AJJL$6$6$88DAw#~ #E$q'N 3 %d1g!$q'NE$t%E.	%^ v~_.	%^ v~s   E7I+CI++I7)NFTTNN)
__name__
__module____qualname____doc__r2   r   r    r$   rO   rD    r   r   r   r   #   s8    D 
63*0d1r   r   c                   x    e Zd ZdZd Z	 ddlma 	 ddZd Z	d Z
d ZddZd	 Zdd
ZddZy#  d Z
ddZY yxY w)PYGLPKa  
    The glpk LP/MIP solver (via its python interface)

    Copyright Christophe-Marie Duquesne 2012

    The glpk variables are available (after a solve) in var.solverVar
    The glpk constraints are available in constraint.solverConstraint
    The Model is in prob.solverModel
    r
   Nc                     t        j                  | ||       | j                  s$t        j	                  t        j
                         yy)aB  
            Initializes the glpk solver.

            @param mip: if False the solver will solve a MIP as an LP
            @param msg: displays information from the solver to stdout
            @param timeLimit: not handled
            @param gapRel: not handled
            @param solverParams: not handled
            N)r   r   r   glpkglp_term_outGLP_OFF)r   r   r   r   gapRelsolverParamss         r   r   zPYGLPK.__init__   s4     dC-88!!$,,/ r   c                    |j                   }| j                  r1| j                  |j                         rt        j	                  |      }nt        j                  |      }t        j                  t        j                  t        j                  t        j                  t        j                  t        j                  t        j                  t        j                  t        j                  t        j                  t        j                  t        j                   i}|j#                         D ]  }| j                  rA| j                  |j                         r&t        j%                  ||j&                        |_        n%t        j+                  ||j&                        |_        t        j-                  ||j&                        |_         |j0                  j3                         D ]  }| j                  r<| j                  |j                         r!t        j5                  ||j&                        }n t        j7                  ||j&                        }|j8                   |z
  |_        t        j=                  ||j&                        |_         d|_         |j#                         D ]	  }d|_!         |jE                  |t        j                        }|jG                  |       |S )NTF)$solverModelr   hasMIPConstraintsry   glp_mip_statusglp_get_statusGLP_OPTr   r`   	GLP_UNDEFrb   GLP_FEAS
GLP_INFEASra   
GLP_NOFEAS	GLP_UNBNDrc   	variablesglp_mip_col_val
glpk_indexvarValueglp_get_col_primglp_get_col_dualdjconstraintsrN   glp_mip_row_valglp_get_row_primconstantslackglp_get_row_dualpi	resolveOK
isModifiedgetrF   )	r   r'   probsolutionStatusglpkLpStatusvarconstrrow_valrM   s	            r   findSolutionValueszPYGLPK.findSolutionValues   s    >>DxxD222>>B!%!4!4T!:!%!4!4T!:i77	 ; ;y88!=!=!=!=	 ; ;L ||~ E88 6 6r~~ F#'#7#7cnn#MCL#'#8#8s~~#NCL..tS^^DE ..//1 K88 6 6r~~ F"2249J9JKG"33D&:K:KLG &/'9 11$8I8IJ	K  BL||~ '!&'!%%ni6Q6QRFOOF#Mr   c                      y)r"   Tru   r   s    r   r$   zPYGLPK.available  s    r   c                 f    t         j                  |      dkD  xs t         j                  |      dkD  S )Nr
   )ry   glp_get_num_intglp_get_num_bin)r   r   s     r   r   zPYGLPK.hasMIPConstraints  s2    $$[1A5 9''4q8r   c                    t                | _        t        j                  |j                  d       t        j                  |j                  d       | j                  r| j                  |j                        rpt        j                  |j                        }|t        j                  t        j                  t        j                  fv r t        j                  |j                  d       | xj                  t               z  c_        y)zSolves the problem with glpkr
   N)r   	solveTimery   glp_adv_basisr   glp_simplexr   r   r   r   r   r   
glp_intopt)r   r'   callbackrM   s       r   
callSolverzPYGLPK.callSolver  s    #gXDNr~~q1R^^T2xxD222>>B,,R^^<dllDNNDMMJJOOBNND9NNeg%Nr   c           	      
   t        j                  d       t        j                         }t        j	                  ||j
                         t        j                  d       |j                  t        j                  k(  r$t        j                  |t        j                         t        j                  d       t        j                  |t        t        |j                  j                                            t!        |j                  j#                         d      D ]/  \  }}|\  }}t        j%                  |||       |j                  t        j&                  k(  r3t        j)                  ||t        j*                  d|j,                          n|j                  t        j.                  k(  r3t        j)                  ||t        j0                  |j,                   d       nf|j                  t        j2                  k(  r>t        j)                  ||t        j4                  |j,                   |j,                          nt7        d      ||_        2 t        j                  d       t        j;                  |t        |j=                                      t!        |j=                         d      D ]T  \  }}t        j?                  |||j
                         d}	d}
t        j@                  }|jB                  |jB                  }	t        j0                  }|jD                  |jD                  }
t        j*                  }|jD                  2|jB                  &|
|	k(  rt        j4                  }nt        jF                  }t        jI                  ||||	|
       |jJ                  t        jL                  k(  rNt        jO                  ||t        jP                         t        jS                  ||      t        jP                  k(  sJ ||_        W t        j                  d
       |j=                         D ]A  }|jT                  jW                  |      }|s!t        jY                  ||j8                  |       C t        j                  d       |j                  j[                         D ]  }t        t        |j#                                     }t        j]                  |dz         }t        j_                  |dz         }t!        |j#                         d      D ]  \  }}|\  }}|j8                  ||<   |||<     t        ja                  ||j8                  |||        ||_1        y	)zU
            Takes the pulp lp model and translates it into a glpk model
            zcreate the glpk modelzset the sense of the problemz"add the constraints to the problemr   )start        #Detected an invalid constraint typez add the variables to the problemNzset the objective functionzset the problem matrix)2r	   debugry   glp_create_probglp_set_prob_namer2   senser   
LpMaximizeglp_set_obj_dirGLP_MAXglp_add_rowsre   listr   keys	enumerateitemsglp_set_row_nameLpConstraintLEglp_set_row_bndsGLP_UPr   LpConstraintGEGLP_LOLpConstraintEQGLP_FXr   r   glp_add_colsr   glp_set_col_nameGLP_FRlowBoundupBoundGLP_DBglp_set_col_bndscat	LpIntegerglp_set_col_kindGLP_IVglp_get_col_kind	objectiver   glp_set_obj_coefrN   intArraydoubleArrayglp_set_mat_rowr   )r   r'   r   rn   vr2   
constraintjr   lbubtrp   lindvals                   r   buildSolverModelzPYGLPK.buildSolverModel  s    II-.'')D""41II45xx9///$$T4<<8II:;dCR^^-@-@-B(C$DE!".."6"6"8B *1#$ j%%dAt4##y'?'??))acJ4G4G3G  %%)A)AA))az/B/B.BC  %%)A)AA))az/B/B.BZEXEXDX **OPP()
%#*$ II89dC$78#BLLN!< #3%%dAsxx8KK||+BA{{*BA{{*3<<3GRx KK KK%%dAq"b977i111))$4;;?00q9T[[HHH!")#* II23||~ G((-))$FG II./ nn335 O
Z--/01mmAE*&&q1u-%j&6&6&8B #DAq!"JC ^^CF"CF# $$T:+@+@!S#NO "BNr   c                    | j                  |       t        j                  d       | j                  ||       | j	                  |      }|j                         D ]	  }d|_         |j                  j                         D ]	  }d|_         |S )z
            Solve a well formulated lp problem

            creates a glpk model, variables and constraints and attaches
            them to the lp model which it then solves
            zSolve the Model using glpkr   F)	r   r	   r   r   r   r   modifiedr   rN   )r   r'   r   r   r   r   s         r   rO   zPYGLPK.actualSolvec  s     !!"%II23OOBO2!44R8N||~ %$% nn335 ,
&+
#,!!r   c                    |j                   }t        j                  d       |j                  j	                         D ]!  }|j
                  }|j                  s|j                  t        j                  k(  r3t        j                  ||t        j                  d|j                          m|j                  t        j                  k(  r3t        j                  ||t        j                  |j                   d       |j                  t        j                   k(  r?t        j                  ||t        j"                  |j                   |j                          t%        d       | j'                  ||       | j)                  |      }|j+                         D ]	  }d|_         |j                  j	                         D ]	  }d|_         |S )z
            Solve a well formulated lp problem

            uses the old solver and modifies the rhs of the modified
            constraints
            zResolve the Model using glpkr   r   r   F)r   r	   r   r   rN   r   r   r   r   r   ry   r   r   r   r   r   r   r   r   r   r   r   )r   r'   r   r   r   rn   r   r   s           r   actualResolvezPYGLPK.actualResolvev  s    >>DII45 nn335 U
))&&!''9+C+CC-- !T[[#
8K8K7K $))Y-E-EE-- !T[[:3F3F2F $))Y-E-EE--  KK'000'000 ..STT)U* OOBO2!44R8N||~ %$% nn335 ,
&+
#,!!r   c                      y)r"   Fru   r   s    r   r$   zPYGLPK.available   s    r   c                     t        d      )r&   zGLPK: Not Available)r   )r   r'   r   s      r   rO   zPYGLPK.actualSolve   s    !"788r   )TTNNr   )rq   rr   rs   rt   r2   glpk.glpkpiglpkpiry   r   r   r$   r   r   r   rO   r   ru   r   r   rw   rw      s\     DX" 	# >B	0 "	H				&D	"N	"&%	"_9		9s   . 	9rw   )corer   r   r   r   r   r   r	   r9    r   r   r   GLPKry   rw   ru   r   r   <module>r      sG   6 M L ' 	 	J| JZ  e"X e"r   