コード例 #1
0
        /// <summary>
        /// returns a copy of Equation with the given additional VariableFactor
        /// </summary>
        /// <param name="vf"></param>
        /// <returns></returns>
        public Equation AddFactor(VariableFactor vf)
        {
            var factors = new VariableFactor[this.Factors.Length + 1];

            Array.Copy(Factors, 0, factors, 1, Factors.Length);
            factors[0] = vf;

            return(new Equation(LeftTerm, factors, Coefficient));
        }
コード例 #2
0
        private Tableau PreprocessTableau(Tableau t)
        {
            //check if there is no need to preprocess the table
            if (t.Equations.All(e => e.LeftTerm.Type == VariableType.Target || e.Coefficient >= 0))
            {
                return(t);
            }

            var eqs  = new Equation[t.Equations.Length];
            var tVar = Variable.Problem(0);

            for (var i = 0; i < eqs.Length - 1; i++)
            {
                eqs[i] = t.Equations[i].AddFactor(new VariableFactor(tVar, 1));
            }

            var f = new VariableFactor[t.Equations[0].Factors.Length];

            for (var i = 0; i < f.Length; i++)
            {
                f[i] = new VariableFactor(t.Equations[0].Factors[i].Variable, 0);
            }

            var target = new Equation(Variable.Target(), f, 0);

            target = target.AddFactor(new VariableFactor(tVar, -1));

            eqs[eqs.Length - 1] = target;
            t = new Tableau(eqs);

            for (var i = 0; i < eqs.Length; i++)
            {
                if (t[i].LeftTerm.Type != VariableType.Target && t[i].Coefficient < 0)
                {
                    t = NextTableau(t, tVar, t[i].LeftTerm);
                }
            }

            SolveTableau(ref t);

            var mainEquations = new Equation[t.Equations.Length];

            for (var i = 0; i < mainEquations.Length - 1; i++)
            {
                mainEquations[i] = t.Equations[i].RemoveFactor(tVar);
            }

            var mainTarget = target.RemoveFactor(tVar);

            mainEquations[mainEquations.Length - 1] = mainTarget;
            return(new Tableau(mainEquations));
        }
コード例 #3
0
        /// <summary>
        /// Returns an Equation which has applied the given Equation to the current Equation. The LeftTerm is equal to the current.
        /// </summary>
        /// <param name="equation">An Equation whith a LeftTerm Variable that exists in the current Equation Summands</param>
        /// <returns></returns>
        public Equation Resolve(Equation equation)
        {
            var fac = this[equation.LeftTerm];

            var vars = new VariableFactor[equation.Factors.Length];

            for (var i = 0; i < vars.Length; i++)
            {
                vars[i] = (equation[i] * fac) + this[equation[i].Variable];
            }

            var coefficient = Coefficient + equation.Coefficient * fac;

            return(new Equation(LeftTerm, vars, coefficient));
        }
コード例 #4
0
        private VariableFactor[] GetFactorsByLine(Variable[] vars, string[] fCells, bool negate)
        {
            var f = new VariableFactor[vars.Length];

            for (var i = 0; i < f.Length; i++)
            {
                var val = double.Parse(fCells[i]);

                if (negate)
                {
                    val = 0 - val;
                }

                f[i] = new VariableFactor(vars[i], val);
            }

            return(f);
        }
コード例 #5
0
        public Equation Switch(Variable leftTerm)
        {
            var baseFac = (-1) / this[leftTerm];

            var factors = new VariableFactor[Factors.Length];
            var si      = IndexOf(leftTerm);

            factors[si] = new VariableFactor(LeftTerm, -baseFac);

            for (var i = 0; i < Factors.Length; i++)
            {
                if (i != si)
                {
                    factors[i] = Factors[i] * baseFac;
                }
            }

            return(new Equation(leftTerm, factors, Coefficient * baseFac));
        }
コード例 #6
0
        public Solver(StreamReader reader)
        {
            var cells = ReadAllLines(reader).Select(l => l.Split(';')).ToArray();

            var vCnt = int.Parse(cells[0][0]);
            var cCnt = int.Parse(cells[0][1]);

            var pVars = new Variable[vCnt];
            var sVars = new Variable[cCnt];

            for (var i = 0; i < pVars.Length; i++)
            {
                pVars[i] = Variable.Problem(i + 1);
            }

            var eqs = new List <Equation>();

            for (var i = 0; i < cCnt; i++)
            {
                var op     = cells[i + 3][0];
                var fCells = cells[i + 3].Skip(1).ToArray();

                if (op == "<=" || op == "=")
                {
                    eqs.Add(new Equation(Variable.Slack(i + 1), GetFactorsByLine(pVars, fCells, true), double.Parse(cells[i + 3][vCnt + 1])));
                }

                if (op == ">=" || op == "=")
                {
                    eqs.Add(new Equation(Variable.Slack(i + 1), GetFactorsByLine(pVars, fCells, false), double.Parse(cells[i + 3][vCnt + 1])));
                }
            }

            //target function
            var tFactors = new VariableFactor[vCnt];
            var minimize = cells[1][0].Contains("min");

            for (var i = 0; i < tFactors.Length; i++)
            {
                var val = double.Parse(cells[1][i + 1]);

                if (minimize)
                {
                    val = -val;
                }

                tFactors[i] = new VariableFactor(pVars[i], val);
            }
            var targetVal = double.Parse(cells[1][vCnt + 1]);

            if (minimize)
            {
                targetVal = -targetVal;
            }

            var target = new Equation(Variable.Target(), tFactors, targetVal);

            eqs.Add(target);

            _equations = eqs.ToArray();
        }
コード例 #7
0
 public bool Equals(VariableFactor other)
 {
     return(Variable.Equals(other.Variable) && Factor.Equals(other.Factor));
 }