public IterationState(IterationState prevState) { this.varCount = prevState.varCount; this.eqCount = prevState.eqCount; this.P = prevState.P; this.C = prevState.C; this.b = prevState.b; this.cCoeff = prevState.cCoeff; this.basIndInit = prevState.basIndInit; this.basInd = prevState.basInd.Clone() as int[]; this.basInd[prevState.eqIndex] = prevState.basIndex; this.Binv = prevState.BinvNext; this.basIndex = this.eqIndex = this.oldIndex = -1; }
bool SetSwapVarsIllegalDecision(IterationState s) { s.basIndex = -1; double min = double.MaxValue; for (int i = 0; i < eqCount; i++) { double b = s.Xb.Elements[i, 0]; if (b >= 0) { continue; } for (int j = 0; j < varCount; j++) { if (s.IsBaseVar(j)) { continue; } double a = s.GetVarCoeff(j, i); if (a >= 0) { continue; } if (b / a < min) { s.eqIndex = i; s.basIndex = j; min = b / a; } } } if (s.basIndex == -1) { throw new ArgumentException("Допустимое решение не существует"); } s.oldIndex = s.basInd[s.eqIndex]; s.alpha = s.Binv * P[s.basIndex]; s.alphar = s.alpha.Elements[s.eqIndex, 0]; return(true); }
bool SetSwapVarsLegalDecision(IterationState s) { s.basIndex = -1; double epslon = 0.000001; double min = float.MaxValue; for (int i = 0; i < s.zc.N; i++) { if (s.zc.Elements[0, i] < 0 - epslon && s.zc.Elements[0, i] < min) { min = s.zc.Elements[0, i]; s.basIndex = s.zcInd[i]; } } if (s.basIndex == -1) { return(false); } s.alpha = s.Binv * P[s.basIndex]; s.theta = double.MaxValue; for (int i = 0; i < eqCount; i++) { double tmp = s.Xb.Elements[i, 0] / s.alpha.Elements[i, 0]; if (tmp > 0 && tmp < s.theta) { s.oldIndex = s.basInd[i]; s.theta = tmp; s.eqIndex = i; } } if (s.theta == double.MaxValue) { throw new ArgumentException("Допустимое решение не существует"); } s.alphar = s.alpha.Elements[s.eqIndex, 0]; return(true); }
public bool DoIteration() { IterationState s; if (iterNum > 0) { IterationState prevState = states[states.Count - 1] as IterationState; s = new IterationState(prevState); } else { s = new IterationState(varCount, eqCount, P, C, b, cCoeff, basIndInit); } states.Add(s); s.Cb = new ExMatrix(1, eqCount); for (int i = 0; i < eqCount; i++) { s.Cb.Elements[0, i] = C.Elements[0, s.basInd[i]]; } s.gamma = s.Cb * s.Binv; ArrayList zcPMatrices = new ArrayList(); ExMatrix zcC = new ExMatrix(1, varCount - eqCount); s.zcInd = new int[varCount - eqCount]; int k = 0; for (int i = 0; i < varCount; i++) { if (s.IsBaseVar(i)) { continue; } s.zcInd[k] = i; zcPMatrices.Add(P[i]); zcC.Elements[0, k] = C.Elements[0, i]; k++; } ExMatrix zcP = new ExMatrix(zcPMatrices.ToArray(typeof(ExMatrix)) as ExMatrix[]); s.zc = s.gamma * zcP - zcC; s.Xb = s.Binv * b; s.isDecisionLegal = true; for (int i = 0; i < s.Xb.M; i++) { if (s.Xb.Elements[i, 0] < 0) { s.isDecisionLegal = false; break; } } bool res; if (s.isDecisionLegal) { res = SetSwapVarsLegalDecision(s); } else { res = SetSwapVarsIllegalDecision(s); } if (res == false) { return(false); } s.xi = new ExMatrix(eqCount, 1); for (int i = 0; i < s.xi.M; i++) { if (i == s.eqIndex) { s.xi.Elements[i, 0] = 1 / s.alphar; } else { s.xi.Elements[i, 0] = -s.alpha.Elements[i, 0] / s.alphar; } } ExMatrix matr = new ExMatrix(eqCount); for (int i = 0; i < matr.M; i++) { matr.Elements[i, s.eqIndex] = s.xi.Elements[i, 0]; } s.BinvNext = matr * s.Binv; iterNum++; return(true); }