@@ -3,14 +3,22 @@ import { formatNumber, EPSILON } from './formatters.js'
33
44// Constantes de configuración
55const MAX_ITERATIONS = 100 // Máximo de iteraciones permitidas
6- const BIG_M = 1e6 // Penalización para variables artificiales (Método de la M Grande)
76
87export class SimplexSolver {
98 constructor ( problemData ) {
109 this . problemData = problemData
1110 this . iterations = [ ]
1211 this . finalSolution = null
1312 this . status = null
13+
14+ // BIG_M dinámico: al menos 1000x el coeficiente más grande del problema
15+ const allValues = [
16+ ...problemData . objective . map ( Math . abs ) ,
17+ ...problemData . constraints . flat ( ) . map ( Math . abs ) ,
18+ ...problemData . rhs . map ( Math . abs )
19+ ]
20+ const maxCoef = Math . max ( ...allValues , 1 )
21+ this . BIG_M = maxCoef * 1000
1422 }
1523
1624 /**
@@ -184,13 +192,13 @@ export class SimplexSolver {
184192 tiposDeVariablesAgregadas . push ( { type : 'surplus' , constraintIndex : indiceRestriccion } )
185193 tiposDeVariablesAgregadas . push ( { type : 'artificial' , constraintIndex : indiceRestriccion } )
186194 coeficientesObjetivo . push ( 0 ) // Variable de exceso: no afecta Z
187- coeficientesObjetivo . push ( - BIG_M ) // Variable artificial: penalización Big-M (maximización)
195+ coeficientesObjetivo . push ( - this . BIG_M ) // Variable artificial: penalización Big-M (maximización)
188196 } else if ( tipoDeRestriccion === '=' ) {
189197 // Restricción tipo =: Agregar variable artificial
190198 // Ejemplo: X₁ + X₂ = 8 → X₁ + X₂ + A₁ = 8
191199 contadorVariablesArtificiales ++
192200 tiposDeVariablesAgregadas . push ( { type : 'artificial' , constraintIndex : indiceRestriccion } )
193- coeficientesObjetivo . push ( - BIG_M ) // Variable artificial: penalización Big-M (maximización)
201+ coeficientesObjetivo . push ( - this . BIG_M ) // Variable artificial: penalización Big-M (maximización)
194202 }
195203 }
196204
@@ -900,4 +908,4 @@ export class SimplexSolver {
900908}
901909
902910// Exportar constantes para uso en otros módulos si es necesario
903- export { EPSILON , MAX_ITERATIONS , BIG_M }
911+ export { EPSILON , MAX_ITERATIONS }
0 commit comments