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 FuzzyObjectiveFunction ReduceBaseVar(FuzzyObjectiveFunction foArtificial, IEnumerable <FuzzyVectorEquation> constraints) { FuzzyObjectiveFunction newFO = foArtificial; TRFNOperation fop = new TRFNOperation(); if (foArtificial != null && foArtificial.FuzzyVector.Count() > 0) { foreach (string varName in constraints.Select(x => x.Name)) { FuzzyVectorEquation vectorRef = constraints.Where(c => c.Vector.Any(v => v.Key == varName && v.Value == 1)).FirstOrDefault(); TRFN pivotefo = foArtificial.FuzzyVector.Where(r => r.Key == varName).FirstOrDefault().Value; if (!fop.IsZero(pivotefo)) { newFO = new FuzzyObjectiveFunction(foArtificial.Header, fop.ReduceFuzzyRows(newFO.FuzzyNums, fop.OperateFuzzyConstant(vectorRef.Numbers, Constantes.Multiplicacion, pivotefo)), fop.Addition(newFO.IndependentTerm, fop.Multiplication(fop.MakeNegative(pivotefo), vectorRef.IndependentTerm)), newFO.IsMax); } } } return(newFO); }