
    g2=              
       2   d Z ddlZddlZddlZddlZddlZddlmZ ddl	Z	ddl
mZ e	j                  ZddlmZ ddlmZ ddlZ	 ddlZ ej,                  e      ZddlZej4                  Zd Zdd	lmZ  G d
 dej>                        Z d!dZ!dZ"ejF                  dkD  Z$e$r&dZ% ejL                         jO                         dv rdZ%ndZ%dZ(ej                  dv rdZ(e"dz  Z"nej                  dv r
dZ(dZ%e"dz  Z"ndZ(e"dz  Z"ejR                  jU                  e+      Z,ejR                  j[                  ejR                  j]                  e,de"            Z/ e!e/e(e%      \
  Z0Z1Z2Z3Z4Z5Z6Z7Z8Z9 G d d      Z: G d de:      Z;ejx                  fd Z=y# e$ r ddlZY Mw xY w)"z
This file contains the solver classes for PuLP
Note that the solvers that require a compiled extension may not work in
the current version
    N)	monotonic)Union   )sparse)	constantsc                 4    t        |       j                         S N)strencode)_objs    K/var/www/dash_apps/app1/venv/lib/python3.12/site-packages/pulp/apis/core.py<lambda>r   >   s    T))+     )uuid4c                       e Zd ZdZy)PulpSolverErrorz(
    Pulp Solver-related exceptions
    N)__name__
__module____qualname____doc__ r   r   r   r   C   s     	r   r   linux64c           
         t         j                  j                  |       }t        |||d      }|j	                  |        	 |j                  dd      }	 	 |j                  dd      j                  d      j                  dd	      }	 |j                  dd
      }	 |j                  dd      j                  d      }	 |j                  dd      }		 |j                  dd      }
	 |j                  dd      }	 |j                  dd      }	 |j                  dd      }	 |j                  dd      }t        |      D ]i  \  }}t         j                  j                  |      r&t         j                  j                  t         j                  j                  t              |      ||<   k |||||	|
||||f
S # t        j                  $ r d}Y uw xY w# t        $ r& |j                  dd      j                  dd	      }Y tw xY w# t        j                  $ r d	}Y w xY w# t        j                  $ r d}Y w xY w# t        j                  $ r dg}Y w xY w# t        j                  $ r d}	Y w xY w# t        j                  $ r d}
Y w xY w# t        j                  $ r d}Y w xY w# t        j                  $ r d}Y w xY w# t        j                  $ r d}Y w xY w# t        j                  $ r d}Y w xY w)z5reads the configuration file to initialise the module)hereosarch	locations	CplexPathzlibcplex110.solicensesilm_cplex_licensezstring-escape" ilm_cplex_license_signaturer   
CoinMPPathz, zlibCoinMP.so
GurobiPathz$/opt/gurobi201/linux32/lib/python2.5CbcPathcbcGlpkPathglpsolPulpCbcPathScipPathscip	FscipPathfscip)r   pathdirnameParserreadgetconfigparserErrordecodereplaceAttributeErrorgetintsplit	enumeratejoinconfig_filename)filenameoperating_systemr   r   configcplex_dll_pathr!   r$   coinMP_pathgurobi_pathcbc_path	glpk_pathpulp_cbc_path	scip_path
fscip_pathir0   s                    r   
initializerK   L   s   77??8$DT)94HIF
KK*K=		

:':;(b! (&,mm5'
#
'jjl;AA$G=jjl;::k95JJ{J7	

;>JJ{J7	ZZ[9
 [) R4wwt$WW\\"''///*JDQKNR
 	# k  *)*  	 &

:7J K S SR!	    (&'#(  '%&'  =<=    	    	  
s   F0 1G H !H6 <I I/ "J 5J' K K 0G	G	+G;7G> :G;;G> >HHH32H36III,+I,/JJJ$#J$'K ?K KKK87K8zpulp.cfgl        )aarch64arm64rM   32)win32cliwinz.win)darwinosxz.osxz.linuxz..c                   Z    e Zd ZdZd Z	 ddZd Zd Zd Zd Z	d Z
	 dd	Zd
 ZeZd ZeZy)LpSolverzA generic LP SolverNc                     |g }|| _         || _        || _        || _        |j	                         D ci c]  \  }}|	|| c}}| _        yc c}}w )a  
        :param bool mip: if False, assume LP even if integer variables
        :param bool msg: if False, no log is shown
        :param list options:
        :param float timeLimit: maximum time for solver (in seconds)
        :param args:
        :param kwargs: optional named options to pass to each solver,
                        e.g. gapRel=0.1, gapAbs=10, logPath="",
        N)mipmsgoptions	timeLimititemsoptionsDict)	selfrW   rX   rY   rZ   argskwargskvs	            r   __init__zLpSolver.__init__   sT     ?G" .4\\^MTQq}AqDMMs
   
AAc                     t         )zTrue if the solver is availableNotImplementedErrorr]   s    r   	availablezLpSolver.available       !!r   c                     t         )z"Solve a well formulated lp problemrd   r]   lps     r   actualSolvezLpSolver.actualSolve   rh   r   c                 *     | j                   |fi | y)z
        uses existing problem information and solves the problem
        If it is not implemented in the solver
        just solve again
        N)rl   )r]   rk   r_   s      r   actualResolvezLpSolver.actualResolve   s     	&v&r   c                     | j                         }| j                  |_        | j                  |_        | j                  |_        |S zMake a copy of self)	__class__rW   rX   rY   r]   aCopys     r   copyzLpSolver.copy   s6      HH	HH	r   c                 $    |j                  |       S )zSolve the problem lp)solverj   s     r   rv   zLpSolver.solve   s     xx~r   c                 
   |2t         j                  dt         j                  dt         j                  di}|"t         j                  dt         j
                  di}|"t         j                  dt         j                  di}d	dl}d	}t        |j                               }t        |      }	t        |	      D 
ci c]  }
||
   |

 c}
| _        t        |	      D 
ci c]  }
||
   j                  |
 c}
| _        t        |	      D 
ci c]  }
|
||
   
 c}
| _        ||j"                     }|j$                  |	z  } |       }|j&                  j)                         D ]  \  }}||| j                  |   <     |j$                  d
      }|j*                  |	z  } |       } |       } |       } |       }|j                         D ]  }t-        |j                        || j                  |   <   d
|| j                  |   <   |j.                  dk7  r|j.                  || j                  |   <   n| || j                  |   <   |j0                  dk7  r|j0                  || j                  |   <   ||| j                  |   <    t        |j2                        }|j$                  |z  }|j*                  |z  }|j4                  |z  } |       } |       } |       } |       }i | _        i | _        d	}
|j2                  D ]}  }|j2                  |   j:                   ||
<   d
||
<   t-        |      ||
<   t-        ||j2                  |   j"                           ||
<   |
| j6                  |<   || j8                  |
<   |
dz   }
 |j=                         }t?        j@                  t        t        |            t        t        |	                  } |D ]3  \  }!}"}#| jC                  | j6                  |"   | j                  |!   |#       5 | jE                         \  }$}%}&}'}(tG        |(|j$                        })tG        |'|jH                        }*tG        |%|jH                        }+tG        |&|jH                        },|j4                  |	z  }- |-       }.|jK                         r=|j                         D ]*  }t-        ||jL                           |.| j                  |   <   , |	| _'        || _(        |	||$||||||||+|,|*|)||||||.| j                   | j8                  fS c c}
w c c}
w c c}
w )zreturns the arrays suitable to pass to a cdll Cplex
        or other solvers that are similar

        Copyright (c) Stuart Mitchell 2007
        NELGCI   r   g        ))constLpConstraintEQLpConstraintLELpConstraintGELpContinuous	LpInteger
LpMaximize
LpMinimizectypeslist	variableslenrangev2nnamevname2nn2vsensec_double	objectiver[   c_char_p	to_stringlowBoundupBoundconstraintsc_charc2nn2cconstantcoefficientsr   Matrixaddcol_based_arraysctypesArrayFillc_intisMIPcat	addedVars	addedRows)/r]   rk   	senseDictLpVarCategoriesLpObjSensesinfBoundr   
rangeCountr   numVarsrJ   objSenseNumVarDoubleArrayobjectCoeffsra   valobjectConstNumVarStrArraycolNameslowerBoundsupperBounds
initValuesnumRowsNumRowDoubleArrayNumRowStrArrayNumRowCharArray	rhsValuesrangeValuesrowNamesrowTypeccoeffssparseMatrixvarrowcoeffnumelsmystartsBase	mylenBase	myindBase
myelemBaseelemBaseindBase
startsBaselenBaseNumVarCharArray
columnTypes/                                                  r   getCplexStyleArrayszLpSolver.getCplexStyleArrays   s    $$c$$c$$cI
 "$113MO ++R1A1A1EK
(	i.-27^<IaL!O<6;GnE	!))1,E-27^<Ay|O<rxx("OOg5(*ll((* 	,FAs(+L!%	, &fooc*72!#')')&(
 
	4A$-aff$5HTXXa[!&)Jtxx{#zzT!+,::DHHQK(,49DHHQK(yyD +,99DHHQK(+3DHHQK(
	4 bnn%"OOg572 --'1%'	')!#!# 	ANN1-666IaL KN#A,HQK"9R^^A->-D-D#EFGAJDHHQKDHHQKAA	 "}}T%.%94g;OP% 	FOCeTXXc]DLL,=uE	F ))+	
":v?!)V\\:$\6<<@
!)V\\: --'1$&
88:\\^ L*3OAEE4J*K
488A;'L  HHHH-
 	
S =E<s   4S=TTc                    t        | j                        }dD ]  }	 t        | |      ||<    dD ]  }	 t        | |      }|r|||<    |j	                  | j
                         |S # t        $ r Y Xw xY w# t        $ r Y Pw xY w)N)solver)rW   rX   	keepFiles)rZ   rY   )dictr   getattrr9   updater\   )r]   datar`   values       r   toDictzLpSolver.toDictr  s    499%, 	A!$*Q	
 * 	Aa(#DG	 	D$$% "  " s"   A&A5&	A21A25	B Bc                     t        |d      5 }t        j                  | j                         |g|i | d d d        y # 1 sw Y   y xY w)Nw)openjsondumpr   )r]   r?   r^   r_   fs        r   toJsonzLpSolver.toJson  sA    (C  	9AIIdkkmQ888	9 	9 	9s	   )?A)TTNN)NNNg@xD)r   r   r   r   r   rb   rg   rl   rn   rt   rv   r   r   to_dictr   to_jsonr   r   r   rU   rU      sT    D ;?N.""' TXy
v$ G9 Gr   rU   c                       e Zd ZdZd ZddZd Zd Zd Zde	e
eej                  f   ddfd	Zd
 Zd Zed        Zed        Zy)LpSolver_CMDz A generic command line LP SolverNc                     t        j                  | g|i | || j                         | _        n|| _        || _        | j                          y)aX  

        :param bool mip: if False, assume LP even if integer variables
        :param bool msg: if False, no log is shown
        :param list options: list of additional options to pass to solver (format depends on the solver)
        :param float timeLimit: maximum time for solver (in seconds)
        :param str path: a path to the solver binary
        :param bool keepFiles: if True, files are saved in the current directory and not deleted after solving
        :param args: parameters to pass to :py:class:`LpSolver`
        :param kwargs: parameters to pass to :py:class:`LpSolver`
        N)rU   rb   defaultPathr0   r   	setTmpDir)r]   r0   r   r^   r_   s        r   rb   zLpSolver_CMD.__init__  sI     	$000<((*DIDI"r   c                     t         j                  |       }| j                  |_        | j                  |_        | j                  |_        |S rp   )rU   rt   r0   r   tmpDirrr   s     r   rt   zLpSolver_CMD.copy  s8     d#YY
..{{r   c                    t         j                  dk7  rUt         j                  j                  dd      | _        t         j                  j                  d| j                        | _        nt         j                  j                  dd      | _        t         j                  j                  d| j                        | _        t         j                  j                  d| j                        | _        t         j
                  j                  | j                        sd| _        yt        j                  | j                  t         j                  t         j                  z         sd| _        yy)zTSet the tmpDir attribute to a reasonnable location for a temporary
        directoryntTMPDIRz/tmpTMPr#   TEMPN)
r   r   environr4   r   r0   isdiraccessF_OKW_OKrf   s    r   r   zLpSolver_CMD.setTmpDir  s     77d?**..6:DK**..<DK **..26DK**..<DK**..=DKww}}T[[)DK4;;"''(9:DK ;r   c                     | j                   r|n<t        j                  j                  | j                  t               j                        fd|D        S )Nc              3   ,   K   | ]  } d |   yw)z-pulp.Nr   ).0nprefixs     r   	<genexpr>z0LpSolver_CMD.create_tmp_files.<locals>.<genexpr>  s     36(&$3s   )r   r   r0   r=   r   r   hex)r]   r   r^   r   s      @r   create_tmp_fileszLpSolver_CMD.create_tmp_files  s8    >>FWW\\$++uw{{;F3d33r   filereturnc                 N    	 t        j                  |       y # t        $ r Y y w xY wr	   )r   removeFileNotFoundError)r]   r   s     r   silent_removezLpSolver_CMD.silent_remove  s$    	IIdO  		s    	$$c                 N    | j                   ry |D ]  }| j                  |        y r	   )r   r   )r]   r^   r   s      r   delete_tmp_fileszLpSolver_CMD.delete_tmp_files  s)    >> 	%Dt$	%r   c                     t         r	   rd   rf   s    r   r   zLpSolver_CMD.defaultPath  s    !!r   c                 6    t         j                  dk7  r| S | dz   S )Nr   z.exe)r   r   )r   s    r   executableExtensionz LpSolver_CMD.executableExtension  s    77d?K&= r   c                 ,    t        j                  |       S )zXChecks that the solver command is executable,
        And returns the actual path to it.)shutilwhich)commands    r   
executablezLpSolver_CMD.executable  s     ||G$$r   )NF)r   r   r   r   r   rb   rt   r   r   r   r
   bytesr   PathLiker   r  r   staticmethodr  r
  r   r   r   r   r     ss    *D("4%UBKK(?"@ T %" ! ! % %r   r   c                 `    |t        |       z  } |       }t        |       D ]
  \  }}|||<    |S )z^
    Creates a c array with ctypes from a python list
    type is the type of the c array
    )r   r<   )myListtypectypecListrJ   elems         r   r   r     s?    
 3v;EGEV$ 4aLr   )r   r   )>r   r   platformr  sysr   timer   clockr5   typingr   ConfigParserr2   r#   r   r   r   loggingujsonr   ImportError	getLoggerr   log
subprocessDEVNULLdevnullr   uuidr   	PulpErrorr   rK   PULPCFGFILEmaxsize	is_64bitsr   machinelowerr@   r0   r1   __file__DIRNAMEnormpathr=   r>   rB   r!   r$   rC   rD   rE   rF   rG   rH   rI   rU   r   r   r   r   r   r   <module>r,     s  6 
   
  $  		"	"  !  g! 


+	 	eoo 	HX KK%	Dx!%99D <<##6K\\ZD6K8K
''//(
#''""277<<{#KL  0$7N NbT%8 T%n "( 	]  s   F	 		FF