internal RevisedSolver(int[] basisArray,
                               double[] xSt,
                               int startOfArtificials,
                               ExtendedConstraintMatrix APar,
                               double[] costsPar,
                               bool[] lowBounds,
                               double[] lowBoundValues,
                               bool[] upperBounds,
                               double[] upperBoundValues,
                               int[] forbiddenPairsPar,
                               List <Constraint> constrs,
                               double etaEps
                               )
        {
            etaEpsilon       = etaEps;
            constraints      = constrs;
            forbiddenPairs   = forbiddenPairsPar;
            basis            = basisArray;
            xStar            = xSt;
            artificialsStart = startOfArtificials;
            nVars            = APar.NumberOfColumns;
            A     = APar;
            costs = costsPar;
            y     = new double[BasisSize];
            index = new int[nVars];
            for (int i = 0; i < nVars; i++)
            {
                index[i] = notInBasis;
            }
            for (int i = 0; i < basis.Length; i++)
            {
                index[basis[i]] = i; //that is the i-th element of basis
            }
            lowBoundIsSet    = lowBounds;
            this.lowBounds   = lowBoundValues;
            upperBoundIsSet  = upperBounds;
            this.upperBounds = upperBoundValues;

            U = new UMatrix(basis.Length);
        }
        void HandleArtificialVariablesAndDecideOnStatus(int artificialsStart, ref int[] basis,
                                                        ExtendedConstraintMatrix AOfSolver)
        {
            Contract.Requires(AOfSolver != null);

            //check that there are no artificial variables having positive values
            //see box 8.2 on page 130
            for (int k = 0; k < basis.Length; k++)
            {
                if (basis[k] >= artificialsStart)
                {
                    if (Math.Abs(solver.XStar[basis[k]]) > EpsilonForArtificials)
                    {
                        status = Status.Infeasible;
                        return;
                    }

                    var r = new double[basis.Length];
                    r[k] = 1;
                    solver.Factorization.Solve_yBEquals_cB(r);

                    var cloned = r.Clone() as double[];
                    Contract.Assume(cloned != null);

                    var vr = new Vector(cloned);

                    foreach (int j in solver.NBasis())
                    {
                        if (j < artificialsStart)
                        {
                            Contract.Assume(j >= 0); // F: Need quantified invariants here
                            Contract.Assume(j < AOfSolver.NumberOfColumns);

                            var colVector = new ColumnVector(AOfSolver, j);

                            Contract.Assume(vr.Length == colVector.NumberOfRows);

                            if (Math.Abs(vr * colVector) > epsilon)
                            {
                                AOfSolver.FillColumn(j, r);
                                solver.Factorization.Solve_BdEqualsa(r);
                                solver.Factorization.AddEtaMatrix(new EtaMatrix(k, r));
                                solver.ReplaceInBasis(j, basis[k]);
                                break;
                            }
                        }
                    }
                }
            }
            //now remove the remaining artificial variables if any
            for (int k = 0; k < basis.Length; k++)
            {
                Contract.Assume(k < constraints.Count); // F: basis's length is < of the # of constraints

                if (basis[k] >= artificialsStart)
                {
                    //cross out k-th constraint
                    //shrink basis
                    var nBas = new int[basis.Length - 1];
                    int j    = 0;
                    for (int i = 0; i < basis.Length; i++)
                    {
                        if (i != k)
                        {
                            Contract.Assume(j < nBas.Length);
                            nBas[j++] = basis[i];
                        }
                    }
                    //taking care of the solver extended matrix
                    constraints.RemoveAt(k);
                    int[] sa = AOfSolver.slacksAndArtificials;
                    for (int i = 0; i < sa.Length; i++)
                    {
                        if (sa[i] > k)
                        {
                            sa[i]--;
                        }
                        else if (sa[i] == k)
                        {
                            sa[i] = -1; //this will take put the column to zero
                        }
                    }
                    solver.index[basis[k]] = RevisedSolver.forgottenVar;
                    solver.basis           = basis = nBas;
                    for (int i = k; i < basis.Length; i++)
                    {
                        solver.index[basis[i]] = i;
                    }
                    k--;
                    solver.Factorization = null; // to ensure the refactorization

                    solver.A = new ExtendedConstraintMatrix(constraints, sa);
                    solver.U = new UMatrix(nBas.Length);
                }
            }
            status = Status.Feasible;
        }
        void StageOne()
        {
            #region  Preparing the first stage solver

            if (UseScaling)
            {
                IntroduceVariableScaleFactors();
            }


            var xStar = new double[NVars];
            FillAcceptableValues(xStar);
            CountSlacksAndArtificials(xStar);
            int artificialsStart = NVars + nSlacks;
            int totalVars        = NVars + nSlacks + nArtificials;

            Contract.Assume(totalVars > 0); // F: it seems to be some property of the implementation

            var nxstar = new double[totalVars];
            xStar.CopyTo(nxstar, 0);
            xStar = nxstar;

            var basis = new int[constraints.Count];

            var solverLowBoundIsSet   = new bool[totalVars];
            var solverLowBounds       = new double[totalVars];
            var solverUpperBoundIsSet = new bool[totalVars];
            var solverUpperBounds     = new double[totalVars];
            var solverCosts           = new double[totalVars];

            lowBoundIsSet.CopyTo(solverLowBoundIsSet, 0);
            lowBounds.CopyTo(solverLowBounds, 0);
            upperBoundIsSet.CopyTo(solverUpperBoundIsSet, 0);
            upperBounds.CopyTo(solverUpperBounds, 0);

            Contract.Assume(nSlacks + nArtificials >= 0); // F: It seems to be some property of the implementation
            var AOfSolver = new ExtendedConstraintMatrix(constraints, nSlacks + nArtificials);

            FillFirstStageSolverAndCosts(solverLowBoundIsSet, solverLowBounds,
                                         solverUpperBoundIsSet, solverUpperBounds, solverCosts, AOfSolver, basis, xStar);


            solver = new RevisedSolver(basis,
                                       xStar,
                                       artificialsStart,
                                       AOfSolver,
                                       solverCosts,
                                       solverLowBoundIsSet, solverLowBounds,
                                       solverUpperBoundIsSet, solverUpperBounds,
                                       ForbiddenPairs,
                                       constraints,
                                       etaEpsilon
                                       );

            #endregion

            ProcessKnownValues();

            solver.Solve();
            if (solver.Status == Status.FloatingPointError)
            {
                return;
            }
            HandleArtificialVariablesAndDecideOnStatus(artificialsStart, ref basis,
                                                       AOfSolver as ExtendedConstraintMatrix);
        }
Example #4
0
 internal BMatrix(int[] bas, ExtendedConstraintMatrix Am)
 {
     this.A     = Am;
     this.basis = bas;
 }
Example #5
0
 internal BMatrix(int[] bas, ExtendedConstraintMatrix Am)
 {
   this.A = Am;
   this.basis = bas;
 }
    internal RevisedSolver(int[] basisArray,
                           double[] xSt,
                           int startOfArtificials,
                           ExtendedConstraintMatrix APar,
                           double[] costsPar,
                           bool[] lowBounds,
                           double[] lowBoundValues,
                           bool[] upperBounds,
                           double[] upperBoundValues,
                           int[] forbiddenPairsPar,
                           List<Constraint> constrs,
                           double etaEps
        )
    {
      etaEpsilon = etaEps;
      constraints = constrs;
      forbiddenPairs = forbiddenPairsPar;
      basis = basisArray;
      xStar = xSt;
      artificialsStart = startOfArtificials;
      nVars = APar.NumberOfColumns;
      A = APar;
      costs = costsPar;
      y = new double[BasisSize];
      index = new int[nVars];
      for (int i = 0; i < nVars; i++)
        index[i] = notInBasis;
      for (int i = 0; i < basis.Length; i++)
        index[basis[i]] = i; //that is the i-th element of basis

      lowBoundIsSet = lowBounds;
      this.lowBounds = lowBoundValues;
      upperBoundIsSet = upperBounds;
      this.upperBounds = upperBoundValues;

      U = new UMatrix(basis.Length);
    }