public bool Pivoting(ref FuzzyTableau tableau, out KeyValuePair <string, double> refVar, out KeyValuePair <string, double> pivot) { bool isCorrect = false; refVar = new KeyValuePair <string, double>("", double.MinValue); if (tableau.FuzzyZRow.IsMax) { refVar = new KeyValuePair <string, double>("", double.MaxValue); } pivot = new KeyValuePair <string, double>(); VectorEquation Zrow = tableau.ZRow; if (tableau != null) { foreach (KeyValuePair <string, double> kv in tableau.ZRow.CuerpoVector) { if (tableau.FuzzyZRow.IsMax && kv.Value != 0 && kv.Value < refVar.Value) { refVar = kv; } else if (!tableau.FuzzyZRow.IsMax && kv.Value != 0 && kv.Value > refVar.Value) { refVar = kv; } } pivot = GetPivot(refVar, ref tableau); isCorrect = true; tableau.isSolution = true; } return(isCorrect); }
public bool GetFuzzyDataSimplex(string path, string NombreHoja, out FuzzyTableau tableau) { bool isCorrect = false; tableau = null; List <string> cabeceraFila = new List <string>(); List <string> cabeceraColumna = new List <string>(); if (!string.IsNullOrWhiteSpace(path) && !string.IsNullOrWhiteSpace(NombreHoja)) { UsoExcel helperExcel = new UsoExcel(path); FuzzyPrimalSimplexService fpsService = new FuzzyPrimalSimplexService(); if (helperExcel.SiProcesoCorrecto && helperExcel.ComprobarSiExisteHoja(NombreHoja, out int indiceHoja) && helperExcel.ObtenerDimensionesHoja(indiceHoja, out int countFila, out int countCol)) { string[,] data = helperExcel.ObtenerDatosHoja(NombreHoja); if (GetEquations(path, data, countFila - 1, countCol - 1, out List <string> header, out List <FuzzyConstraint> fuzzyEquations, out FuzzyObjectiveFunction fuzzyObjectiveFunction, out List <Constraint> equations, out ObjectiveFunction objectiveFunction) && fuzzyEquations.Count > 0) { List <FuzzyVectorEquation> constraints = fpsService.StandardizeConstraints(fuzzyEquations).ToList(); if (fpsService.StandardizeObjectiveFunction(fuzzyEquations, ref fuzzyObjectiveFunction)) { tableau = new FuzzyTableau(constraints, fuzzyObjectiveFunction); isCorrect = true; } } } helperExcel.CerrarLibroExcel(); } return(isCorrect); }
public bool Check(ref FuzzyTableau tableau) { bool isSolution = false; FuzzyTableau initialTableau = null; FuzzyPrimalSimplexService service = new FuzzyPrimalSimplexService(); TRFNOperation fuzzyOperator = new TRFNOperation(); DataManagement dataManagement = new DataManagement(); if (tableau.FuzzyZRow != null && tableau.FuzzyStandardConstraint.Count() > 0 && tableau.FuzzyZRow.FuzzyVector.Any(v => v.Key.Contains("A"))) { Dictionary <string, TRFN> newFO = new Dictionary <string, TRFN>(); foreach (KeyValuePair <string, TRFN> varFO in tableau.FuzzyZRow.FuzzyVector) { if (varFO.Key.Contains("A")) { newFO.Add(varFO.Key, fuzzyOperator.MakeNegative(varFO.Value)); } else { newFO.Add(varFO.Key, new TRFN(0)); } } List <FuzzyVectorEquation> newConstraint = new List <FuzzyVectorEquation>(); int indexA = 1; foreach (FuzzyVectorEquation eqItem in tableau.FuzzyStandardConstraint) { if (eqItem.Name.Contains("e")) { newConstraint.Add(new FuzzyVectorEquation(string.Format("A{0}", indexA.ToString()), eqItem.Vector, eqItem.IndependentTerm)); indexA++; } else { newConstraint.Add(eqItem); } } FuzzyObjectiveFunction initialProblemFO = new FuzzyObjectiveFunction(tableau.FuzzyZRow.Name, dataManagement.OrderDictionaryByVariable(newFO), tableau.FuzzyZRow.IndependentTerm, false); initialTableau = new FuzzyTableau(newConstraint, ReduceArtificialVar(initialProblemFO, newConstraint)); //Se reduce las variables artificiales de la función objetivo. --> Falta colocar bien la base para la reducción while (!service.CheckEnd(initialTableau)) { service.Pivoting(ref initialTableau, out KeyValuePair <string, double> minvar, out KeyValuePair <string, double> pivot); service.ReduceColumns(ref initialTableau, pivot, minvar.Key); } } if (initialTableau != null && fuzzyOperator.IsZero(initialTableau.FuzzyZRow.IndependentTerm)) { tableau = EliminateArtificialColum(new FuzzyTableau(initialTableau.FuzzyStandardConstraint, ReduceBaseVar(tableau.FuzzyZRow, initialTableau.FuzzyStandardConstraint))); isSolution = true; } return(!tableau.FuzzyZRow.FuzzyVector.Any(v => v.Key.Contains("A")) || isSolution); }
public bool ReduceColumns(ref FuzzyTableau tableau, KeyValuePair <string, double> pivot, string minVar) { bool siCorrecto = false; if (tableau != null && !string.IsNullOrEmpty(pivot.Key) && !string.IsNullOrEmpty(minVar)) { OperacionesVectoriales op = new OperacionesVectoriales(); TRFNOperation fop = new TRFNOperation(); List <FuzzyVectorEquation> resultado = new List <FuzzyVectorEquation>(); FuzzyVectorEquation evreferencia = tableau.FuzzyStandardConstraint.Where(r => r.Name == pivot.Key).FirstOrDefault(); evreferencia = new FuzzyVectorEquation(evreferencia.Name, evreferencia.Header, op.OperacionV1parametro(evreferencia.Numbers, "/", pivot.Value), fop.OperateConstant(evreferencia.IndependentTerm, Constantes.Division, pivot.Value)); double varValue = evreferencia.Vector.Where(v => v.Key == minVar).FirstOrDefault().Value; foreach (FuzzyVectorEquation ev in tableau.FuzzyStandardConstraint) { if (ev.Name == pivot.Key) { resultado.Add(evreferencia); } else if (ev.Name != pivot.Key) { double pivoteev = ev.Vector.Where(r => r.Key == minVar).FirstOrDefault().Value; //string opPivot = (pivoteev > 0 && varValue > 0 || pivoteev < 0 && varValue < 0) ? "-" : "+"; if (pivoteev != 0) { resultado.Add(new FuzzyVectorEquation(ev.Name, ev.Header, op.OperacionV1parametroV2(ev.Numbers, "-", pivoteev, evreferencia.Numbers), fop.Subtraction(ev.IndependentTerm, fop.OperateConstant(evreferencia.IndependentTerm, Constantes.Multiplicacion, pivoteev)))); } else { resultado.Add(ev); } } } TRFN pivotefo = tableau.FuzzyZRow.FuzzyVector.Where(r => r.Key == minVar).FirstOrDefault().Value; FuzzyObjectiveFunction fo = null; if (!fop.IsZero(pivotefo)) { fo = new FuzzyObjectiveFunction(tableau.FuzzyZRow.Header, fop.ReduceFuzzyRows(tableau.FuzzyZRow.FuzzyNums, fop.OperateFuzzyConstant(evreferencia.Numbers, Constantes.Multiplicacion, pivotefo)), fop.Addition(tableau.FuzzyZRow.IndependentTerm, fop.Multiplication(fop.MakeNegative(pivotefo), evreferencia.IndependentTerm)), tableau.FuzzyZRow.IsMax); } else { fo = tableau.FuzzyZRow; } //Actualizamos nombre pivote en ecuación tableau = new FuzzyTableau(resultado, fo, RefreshBase(tableau.Base, minVar, pivot.Key)); tableau.FuzzyStandardConstraint.Where(c => c.Name.Contains(pivot.Key)).FirstOrDefault().Name = minVar; tableau.isSolution = true; } return(siCorrecto); }
public FuzzyTableau EliminateArtificialColum(FuzzyTableau tableau) { List <FuzzyVectorEquation> constraint = new List <FuzzyVectorEquation>(); FuzzyObjectiveFunction fuzzyObjective = new FuzzyObjectiveFunction(tableau.FuzzyZRow.FuzzyVector.Where(v => !v.Key.Contains("A")).ToDictionary(x => x.Key, x => x.Value), tableau.FuzzyZRow.IndependentTerm, tableau.FuzzyZRow.IsMax); foreach (FuzzyVectorEquation equation in tableau.FuzzyStandardConstraint) { constraint.Add(new FuzzyVectorEquation(equation.Name, equation.Vector.Where(v => !v.Key.Contains("A")).ToDictionary(x => x.Key, x => x.Value), equation.IndependentTerm)); } return(new FuzzyTableau(constraint, fuzzyObjective)); }
public List <double> GetColum(string var, FuzzyTableau tableau) { List <double> colum = new List <double>(); if (tableau.FuzzyStandardConstraint.Count() > 0) { foreach (FuzzyVectorEquation constraint in tableau.FuzzyStandardConstraint) { colum.Add(constraint.Vector.Where(v => v.Key == var).FirstOrDefault().Value); } } return(colum); }
public bool CheckEnd(FuzzyTableau tableau) { bool isEnd = false; if (tableau != null && tableau.FuzzyZRow != null) { foreach (string item in tableau.Base) { if (tableau.FuzzyZRow.IsMax && tableau.ZRow.CuerpoNum.Min() >= 0) { isEnd = true; } else if (!tableau.FuzzyZRow.IsMax && tableau.ZRow.CuerpoNum.Max() <= 0) { isEnd = true; } else if (GetColum(item, tableau).Max() < 0) { isEnd = true; } } } return(isEnd); }
public FuzzySimplexSolution GetSolution(FuzzyTableau tableau) { return(new FuzzySimplexSolution(tableau.FuzzyStandardConstraint.Select(x => x.Name).Zip(tableau.FuzzyStandardConstraint.Select(x => x.IndependentTerm), (n, i) => new { n, i }).ToDictionary(x => x.n, x => x.i), tableau.FuzzyZRow.IndependentTerm)); }
private KeyValuePair <string, double> GetPivot(KeyValuePair <string, double> variableMinima, ref FuzzyTableau tabla) { double valorCompareIteracion = new double(); string restriccionS = string.Empty; double pivoteValor = double.NaN; valorCompareIteracion = double.MaxValue; foreach (FuzzyVectorEquation ev in tabla.FuzzyStandardConstraint) { double iteracionN = ev.Vector.Where(v => v.Key == variableMinima.Key).FirstOrDefault().Value; string iteracionS = !string.IsNullOrEmpty(ev.Name) ? ev.Name : string.Empty; if (iteracionN > 0 && (tabla.RColumn.CuerpoVector.Where(v => v.Key == ev.Name).FirstOrDefault().Value / iteracionN) < valorCompareIteracion) { pivoteValor = iteracionN; restriccionS = iteracionS; valorCompareIteracion = (tabla.RColumn.CuerpoVector.Where(v => v.Key == ev.Name).FirstOrDefault().Value / iteracionN); } if (tabla.isSolution) { tabla.isSolution = (iteracionN > 0); } } return(new KeyValuePair <string, double>(restriccionS, pivoteValor)); }