public void solve() { EquationSystem system = new EquationSystem(_limits); var result = system.solve(); StartMethod?.Invoke(new StartMethodObject(new PointS(result, solveFunction(result.X, result.Y)), _limits, _func)); if (Utils.IsInteger(result)) { _maxS = new PointS(result, solveFunction(result.X, result.Y)); } else { List <int> list = new List <int>(); if (!Utils.IsInteger(result.X)) { solve(_limits, new Equation(1, 0, ">=", Math.Ceiling(result.X)), new List <int>(list)); solve(_limits, new Equation(1, 0, "<=", Math.Floor(result.X)), new List <int>(list)); } else { solve(_limits, new Equation(0, 1, ">=", Math.Ceiling(result.Y)), new List <int>(list)); solve(_limits, new Equation(0, 1, "<=", Math.Floor(result.Y)), new List <int>(list)); } } EndMethod.Invoke(this, new EndMethodObject(_maxS, true)); }
public BranchMethodObject(PointS point, List <int> list /* Содержит путь ветки, 1 лево 0 право */, bool valid /* решается ли ветка */, Equation[] limits, Equation limit, Equation func) { _list = list; _point = point; _valid = valid; _limits = limits; _limit = limit; _func = func; }
private void solve(Equation[] limits, Equation limit, List <int> list) { bool isBranchX = true; if (limit.Sign == 1) { list.Add(1); } else { list.Add(0); } if (limit.X == 0) { isBranchX = false; } double maxValue = Double.MaxValue; foreach (var equation in limits) { if (!Utils.IsLineParallel(limit, equation)) { Equation[] equations = new Equation[2]; equations[0] = limit; equations[1] = equation; EquationSystem system = new EquationSystem(equations); var temp = system.solve(); if (isBranchX && maxValue > temp.Y) { maxValue = temp.Y; } if (!isBranchX && maxValue > temp.X) { maxValue = temp.X; } } } bool valid = true; foreach (var equation in limits) { if (isBranchX) { if (!equation.check(limit.X, maxValue)) { valid = false; } } else { if (!equation.check(maxValue, limit.Y)) { valid = false; } } } Point result = null; if (isBranchX) { result = new Point(limit.Result, maxValue); } else { result = new Point(maxValue, limit.Result); } Equation[] newLimits = new Equation[limits.Length + 1]; for (int i = 0; i < limits.Length; i++) { newLimits[i] = limits[i]; } newLimits[limits.Length] = limit; /* Метод инициирует событие */ BranchMethod?.Invoke(new BranchMethodObject(new PointS(result, solveFunction(result.X, result.Y)), new List <int>(list), valid, limits, limit, _func)); // И передаёт данные по текущей ветке if (!valid) { return; } if (Utils.IsInteger(result)) { var temp = solveFunction(result.X, result.Y); if (_maxS == null || temp > _maxS.D) { _maxS = new PointS(result, temp); } } else { if (!Utils.IsInteger(result.X)) { solve(newLimits, new Equation(1, 0, ">=", Math.Ceiling(result.X)), copyList(list)); solve(newLimits, new Equation(1, 0, "<=", Math.Floor(result.X)), copyList(list)); } if (!Utils.IsInteger(result.Y)) { solve(newLimits, new Equation(0, 1, ">=", Math.Ceiling(result.Y)), copyList(list)); solve(newLimits, new Equation(0, 1, "<=", Math.Floor(result.Y)), copyList(list)); } } }
public MethodLandAndDoig(Equation func, Equation[] limit) { _limits = limit; _func = func; _maxS = null; }
public EndMethodObject(PointS point, bool valid) { _point = point; _valid = valid; }
public StartMethodObject(PointS point, Equation[] limits, Equation func) { _point = point; _limits = limits; _func = func; }