private void RefreshTable() { var vars = new Dictionary<int, string>(); var rows = new Dictionary<int, Fraction[]>(); var freeCoefficients = new Dictionary<int, Fraction>(); int i; for (i = 0; i < Rows.Count - 1; i++) { vars.Add(i, Rows[i].Cells[0].Value.ToString()); var tmp = new Fraction[_table.Variables.Count]; for (var j = 1; j < Rows[i].Cells.Count - 1; j++) tmp[j - 1] = new Fraction(Rows[i].Cells[j].Value.ToString()); rows.Add(i, tmp); freeCoefficients.Add(i, new Fraction(Rows[i].Cells[Rows[i].Cells.Count - 1].Value.ToString())); } _table.CopyRows(vars, rows, freeCoefficients); var ratings = new Dictionary<int, MaxCoefficient>(); int k; for (k = 1; k < Rows[i].Cells.Count - 1; k++) { if (Rows[i].Cells[k].Value == null) { ratings = null; break; } ratings[k - 1] = new MaxCoefficient(Rows[i].Cells[k].Value.ToString()); } _table.CopyRatings(ratings); _table.FunctionValue = Rows[i].Cells[k].Value == null || Rows[i].Cells[k].Value.ToString() == "" ? null : new MaxCoefficient(Rows[i].Cells[k].Value.ToString()); }
/// <summary> /// Subtract maxCoefficient from current maxCoefficient /// </summary> /// <exception cref="NullReferenceException"></exception> public void Subtract(MaxCoefficient maxCoefficient) { _mCoefficient.Add(maxCoefficient._mCoefficient * -1); _freeCoefficient.Add(maxCoefficient._freeCoefficient * -1); }
/// <summary> /// Represents copy of maxCoefficient /// </summary> /// <exception cref="NullReferenceException"></exception> public MaxCoefficient(MaxCoefficient maxCoefficient) { _mCoefficient = new Fraction(maxCoefficient._mCoefficient); _freeCoefficient = new Fraction(maxCoefficient._freeCoefficient); }
/// <summary> /// Method for value comparison of maxCoefficients /// </summary> /// <returns> /// 0: if they are equal /// 1: if current maxCoefficient > input maxCoefficient /// -1: if input maxCoefficient > current maxCoefficient /// </returns> /// <exception cref="NullReferenceException"></exception> public int CompareTo(MaxCoefficient maxCoefficient) { if (ReferenceEquals(maxCoefficient, null)) return -1; return _mCoefficient == maxCoefficient._mCoefficient ? _freeCoefficient.CompareTo(maxCoefficient._freeCoefficient) : _mCoefficient.CompareTo(maxCoefficient._mCoefficient); }
/// <summary> /// Method for value comparison of current and input maxCoefficients /// </summary> /// <param name="maxCoefficient">MaxCoefficient for comparison</param> /// <returns>Return true if values of input and current maxCoefficient are equal</returns> /// <exception cref="NullReferenceException"></exception> public bool Equals(MaxCoefficient maxCoefficient) { if (ReferenceEquals(null, maxCoefficient)) return false; if (ReferenceEquals(this, maxCoefficient)) return true; return Equals(maxCoefficient._mCoefficient, _mCoefficient) && Equals(maxCoefficient._freeCoefficient, _freeCoefficient); }
/// <summary> /// Method for value comparison of maxCoefficients /// </summary> /// <returns> /// 0: if they are equal /// 1: if first > second /// -1: if second > first /// </returns> /// <exception cref="NullReferenceException"></exception> public static int Compare(MaxCoefficient first, MaxCoefficient second) { if (ReferenceEquals(first, null)) if (ReferenceEquals(second, null)) return 0; else throw new NullReferenceException(); return first.CompareTo(second); }
/// <summary> /// Add maxCoefficient to current maxCoefficient /// </summary> /// <exception cref="NullReferenceException"></exception> public void Add(MaxCoefficient maxCoefficient) { _mCoefficient.Add(maxCoefficient._mCoefficient); _freeCoefficient.Add(maxCoefficient._freeCoefficient); }
/// <summary> /// Method for value comparison of maxCoefficient and fraction /// </summary> /// <returns> /// 0: if they are equal /// 1: if maxCoefficient > fraction /// -1: if fraction > maxCoefficient /// </returns> /// <exception cref="NullReferenceException"></exception> public static int Compare(MaxCoefficient maxCoefficient, Fraction fraction) { return maxCoefficient.CompareTo(fraction); }
/// <summary> /// Method for value comparison of maxCoefficient and fraction /// </summary> /// <returns> /// 0: if they are equal /// 1: if fraction > maxCoefficient /// -1: if maxCoefficient > fraction /// </returns> /// <exception cref="NullReferenceException"></exception> public static int Compare(Fraction fraction, MaxCoefficient maxCoefficient) { return (maxCoefficient.CompareTo(fraction) * -1); }
/// <summary> /// Method for value comparison of maxCoefficient and number /// </summary> /// <returns> /// 0: if they are equal /// 1: if number > maxCoefficient /// -1: if maxCoefficient > number /// </returns> /// <exception cref="NullReferenceException"></exception> public static int Compare(int number, MaxCoefficient maxCoefficient) { return (maxCoefficient.CompareTo(number) * -1); }
/// <summary> /// Method for value comparison of maxCoefficient and number /// </summary> /// <returns> /// 0: if they are equal /// 1: if maxCoefficient > number /// -1: if number > maxCoefficient /// </returns> /// <exception cref="NullReferenceException"></exception> public static int Compare(MaxCoefficient maxCoefficient, int number) { return maxCoefficient.CompareTo(number); }
/// <summary> /// Returns index of solving cell(or -1 if table has no positive rating) /// </summary> /// <param name="table">Table with calculated ratings</param> /// <param name="solvingRowIndex">Index of solving row</param> private int GetSolvingCellIndex(SimplexTable table, int solvingRowIndex) { if (solvingRowIndex < 0) return -1; var minRatioIndex = -1; var minRatio = new MaxCoefficient(); for (var i = 0; i < table.Variables.Count; i++) { var matrixElement = table.GetMatrixElement(solvingRowIndex, i); if (matrixElement >= 0) continue; var rating = table.Ratings[i]; var currRatio = new MaxCoefficient(rating); currRatio.Multiply(1 / matrixElement); if (currRatio.CompareTo(minRatio) != -1 && minRatioIndex != -1) continue; minRatioIndex = i; minRatio = currRatio; } return minRatioIndex; }
/// <summary> /// Calculates ratings /// </summary> public void CalculateRatings() { _ratings = new Dictionary<int, MaxCoefficient>(); var rowsCount = _rows.Count; foreach (var variableId in _allVariables.Keys) { var rating = new MaxCoefficient(); for (var rowIndex = 0; rowIndex < rowsCount; rowIndex++) { var tmp = new MaxCoefficient(_targetFunctionCoefficients[_basisVariables[rowIndex]]); var fraction = new Fraction(_rows[rowIndex].ElementAt(variableId)); tmp.Multiply(fraction); rating.Add(tmp); } rating.Subtract(_targetFunctionCoefficients[variableId]); _ratings.Add(variableId, rating); } FunctionValue = CalculateFunctionValue(); }
/// <summary> /// Gets function value /// </summary> public MaxCoefficient CalculateFunctionValue() { var tmpResult = new MaxCoefficient(); foreach (var elem in _basisVariables) { var varCoefficient = new MaxCoefficient(_targetFunctionCoefficients[elem.Value]); var varValue = new Fraction(_freeCoefficients[elem.Key]); varCoefficient.Multiply(varValue); tmpResult.Add(varCoefficient); } return tmpResult; }