void CountSlacksAndArtificialsForConstraint(Constraint constraint, double[] xStar)
        {
            double rightSide = constraint.rightSide - LP.DotProduct(constraint.coeffs, xStar);

            switch (constraint.relation)
            {
            case Relation.Equal:
                nArtificials++;
                break;

            case Relation.GreaterOrEqual:
                nSlacks++;
                if (rightSide > 0)
                {
                    nArtificials++;
                }
                break;

            case Relation.LessOrEqual:
                nSlacks++;
                if (rightSide < 0)
                {
                    nArtificials++;
                }
                break;
            }
        }
 public LPTestSolver(bool lookForZeroColumns)
 {
     calls++;
     Console.WriteLine(calls);
     lp  = new LP(lookForZeroColumns);
     rsm = new RevisedSimplexMethod();
 }
Exemple #3
0
        static public LP FromFile(string fileName)
        {
            StreamReader sr = new StreamReader(fileName);

            LP lp = new LP(false);

            int nvars = Int32.Parse(sr.ReadLine());

            double[] costs = new double[nvars];

            string[] cw = sr.ReadLine().Split(new char[] { ' ' });

            for (int i = 0; i < nvars; i++)
            {
                costs[i] = Double.Parse(cw[i]);
            }

            lp.InitCosts(costs);


            sr.ReadLine();//swallow the line "constraints"

            int nConstraints = Int32.Parse(sr.ReadLine());

            for (int i = 0; i < nConstraints; i++)
            {
                string constr = sr.ReadLine();

                string[] words = constr.Split(new char[] { ' ' });

                List <string> ws = new List <string>();

                foreach (string s in words)
                {
                    if (s != "")
                    {
                        ws.Add(s);
                    }
                }

                double[] coeff = new double[nvars];
                for (int j = 0; j < nvars; j++)
                {
                    coeff[j] = Double.Parse((string)ws[j]);
                }
                Relation rel = (string)ws[nvars] == "=" ? Relation.Equal :
                               (string)ws[nvars] == "<=" ? Relation.LessOrEqual : Relation.GreaterOrEqual;
                double rs = Double.Parse((string)ws[nvars + 1]);
                lp.AddConstraint(coeff, rel, rs);
                sr.ReadLine(); //eat the empty line
            }

            sr.Close();

            return(lp);
        }
Exemple #4
0
        public void ToFileInMathFormat(string fileName)
        {
            LP lp = new LP(false);

            StreamWriter sw = new StreamWriter(fileName, false, System.Text.Encoding.ASCII);

            sw.Write("f=[");

            for (int i = 0; i < nVars; i++)
            {
                sw.Write(this.costs[i].ToString() + "; ");
            }

            sw.WriteLine("]");


            int k = 0;

            sw.Write("A=[");
            foreach (double[] coeff in this.constraintCoeffs)
            {
                k = WriteConstraint(sw, k, coeff);
            }
            sw.WriteLine("];");
            sw.Write("b=[");

            k = 0;
            foreach (double[] coeff in this.constraintCoeffs)
            {
                Relation r  = this.relations[k];
                double   rs = this.rightSides[k];
                if (r == Relation.LessOrEqual)
                {
                    sw.Write(rs + ";");
                }
                else if (r == Relation.GreaterOrEqual)
                {
                    sw.Write(-rs + ";");
                }
                else
                {
                    sw.Write(-rs + ";");
                    sw.Write(rs + ";");
                }
                k++;
            }
            sw.WriteLine("];");
            sw.WriteLine("lb = zeros({0},1);", this.nVars);
            sw.Close();
        }
        public double GetMinimalValue()
        {
            if (Status == Status.Unknown || Status == Status.Feasible)
            {
                Minimize();
            }
            if (Status == Status.Optimal)
            {
                double[] sol = MinimalSolution();

                return(-LP.DotProduct(sol, costs, costs.Length)); //minus since we reversed the costs
            }
            return(0);
        }
Exemple #6
0
        public void ToFile(string fileName)
        {
            LP lp = new LP(false);

            StreamWriter sw = new StreamWriter(fileName);

            sw.WriteLine(costs.Length);

            for (int i = 0; i < nVars; i++)
            {
                sw.Write(this.costs[i].ToString() + " ");
            }

            sw.WriteLine("\nconstraints");

            sw.WriteLine(this.constraintCoeffs.Count);

            int k = 0;

            foreach (double[] coeff in this.constraintCoeffs)
            {
                foreach (double c in coeff)
                {
                    sw.Write(c + " ");
                }


                Relation r   = (Relation)this.relations[k];
                string   rel = r == Relation.Equal ? "  =  " : r == Relation.LessOrEqual ? " <= " : " >= ";

                sw.WriteLine(rel + " " + this.rightSides[k] + "\n");

                k++;
            }

            sw.WriteLine("end");
            sw.Close();
        }
        void FillFirstStageSolverAndCosts(
            bool[] solverLowBoundIsSet,
            double[] solverLowBounds,
            bool[] solverUpperBoundIsSet,
            double[] solverUpperBounds,
            double[] solverCosts,
            Matrix A,
            int[] basis,
            double[] xStar)
        {
            Contract.Requires(solverUpperBounds != null);
            Contract.Requires(solverLowBounds != null);
            Contract.Requires(solverLowBoundIsSet != null);
            Contract.Requires(solverUpperBoundIsSet != null);
            Contract.Requires(solverCosts != null);
            Contract.Requires(A != null);
            Contract.Requires(xStar != null);

            int slackVar      = NVars;
            int artificialVar = NVars + nSlacks;
            int row           = 0;

            foreach (Constraint c in constraints)
            {
                Contract.Assume(row < basis.Length);

                //we need to bring the program to the form Ax=b
                double rs = c.rightSide - LP.DotProduct(xStar, c.coeffs, nVars);
                switch (c.relation)
                {
                case Relation.Equal: //no slack variable here
                    if (rs >= 0)
                    {
                        SetZeroBound(solverLowBoundIsSet, solverLowBounds, artificialVar);
                        Contract.Assume(artificialVar < solverCosts.Length);
                        solverCosts[artificialVar] = -1;
                        //we are maximizing, so the artificial, which is non-negatiive, will be pushed to zero
                    }
                    else
                    {
                        SetZeroBound(solverUpperBoundIsSet, solverUpperBounds, artificialVar);
                        Contract.Assume(artificialVar < solverCosts.Length);
                        solverCosts[artificialVar] = 1;
                    }
                    basis[row] = artificialVar;
                    A[row, artificialVar++] = 1;
                    break;

                case Relation.GreaterOrEqual:
                    //introduce a non-positive slack variable,
                    Contract.Assume(slackVar < solverUpperBoundIsSet.Length);
                    Contract.Assume(slackVar < solverUpperBounds.Length);
                    SetZeroBound(solverUpperBoundIsSet, solverUpperBounds, slackVar);
                    A[row, slackVar] = 1;
                    if (rs > 0)
                    {
                        //adding one artificial which is non-negative
                        SetZeroBound(solverLowBoundIsSet, solverLowBounds, artificialVar);
                        A[row, artificialVar]      = 1;
                        solverCosts[artificialVar] = -1;
                        basis[row] = artificialVar++;
                    }
                    else
                    {
                        //we can put slackVar into basis, and avoid adding an artificial variable
                        //We will have an equality c.coefficients*acceptableCosts+x[slackVar]=c.rightSide, or x[slackVar]=rs<=0.
                        basis[row] = slackVar;
                    }
                    slackVar++;

                    break;

                case Relation.LessOrEqual:
                    //introduce a non-negative slack variable,
                    Contract.Assume(slackVar < solverLowBoundIsSet.Length);
                    Contract.Assume(slackVar < solverLowBounds.Length);
                    SetZeroBound(solverLowBoundIsSet, solverLowBounds, slackVar);
                    A[row, slackVar] = 1;
                    if (rs < 0)
                    {
                        //adding one artificial which is non-positive
                        SetZeroBound(solverUpperBoundIsSet, solverUpperBounds, artificialVar);
                        A[row, artificialVar] = 1;
                        Contract.Assume(artificialVar < solverCosts.Length);
                        solverCosts[artificialVar] = 1;
                        basis[row] = artificialVar++;
                    }
                    else
                    {
                        //we can put slackVar into basis, and avoid adding an artificial variable
                        //We will have an equality c.coefficients*acceptableCosts+x[slackVar]=c.rightSide, or x[slackVar]=rs<=0.
                        basis[row] = slackVar;
                    }
                    slackVar++;
                    break;
                }

                xStar[basis[row]] = rs;

                row++;
            }
        }
 public LPTestSolver(bool lookForZeroColumns) {
     calls++;
     Console.WriteLine(calls);
     lp = new LP(lookForZeroColumns);
     rsm = new RevisedSimplexMethod();           
 }
Exemple #9
0
        public void ToFile(string fileName)
        {

            LP lp = new LP(false);

            StreamWriter sw = new StreamWriter(fileName);

            sw.WriteLine(costs.Length);

            for (int i = 0; i < nVars; i++)
            {
                sw.Write(this.costs[i].ToString() + " ");
            }

            sw.WriteLine("\nconstraints");

            sw.WriteLine(this.constraintCoeffs.Count);

            int k = 0;

            foreach (double[] coeff in this.constraintCoeffs)
            {
                foreach (double c in coeff)
                    sw.Write(c + " ");


                Relation r = (Relation)this.relations[k];
                string rel = r == Relation.Equal ? "  =  " : r == Relation.LessOrEqual ? " <= " : " >= ";

                sw.WriteLine(rel + " " + this.rightSides[k] + "\n");

                k++;

            }

            sw.WriteLine("end");
            sw.Close();


        }
Exemple #10
0
        public void ToFileInMathFormat(string fileName) {
            LP lp = new LP(false);

            StreamWriter sw = new StreamWriter(fileName, false,System.Text.Encoding.ASCII);

            sw.Write("f=[");

            for (int i = 0; i < nVars; i++) {
                sw.Write(this.costs[i].ToString() + "; ");
            }

            sw.WriteLine("]");

         
            int k = 0;
            sw.Write("A=[");
            foreach (double[] coeff in this.constraintCoeffs) {
                k = WriteConstraint(sw, k, coeff);
            }
          sw.WriteLine("];");
          sw.Write("b=[");

          k = 0;
          foreach (double[] coeff in this.constraintCoeffs) {
              Relation r = this.relations[k];
              double rs=this.rightSides[k];
              if (r == Relation.LessOrEqual)
                  sw.Write(rs + ";");
              else if (r == Relation.GreaterOrEqual)
                  sw.Write(-rs + ";");
              else {
                  sw.Write(-rs + ";");
                  sw.Write(rs + ";");              
              }
              k++;
          }
          sw.WriteLine("];");
          sw.WriteLine("lb = zeros({0},1);",this.nVars);
            sw.Close();

        }
Exemple #11
0
        static public LP FromFile(string fileName)
        {
            StreamReader sr = new StreamReader(fileName);

            LP lp = new LP(false);

            int nvars = Int32.Parse(sr.ReadLine());

            double[] costs = new double[nvars];

            string[] cw = sr.ReadLine().Split(new char[] { ' ' });

            for (int i = 0; i < nvars; i++)
                costs[i] = Double.Parse(cw[i]);

            lp.InitCosts(costs);


            sr.ReadLine();//swallow the line "constraints"

            int nConstraints = Int32.Parse(sr.ReadLine());

            for (int i = 0; i < nConstraints; i++)
            {
                string constr = sr.ReadLine();

                string[] words = constr.Split(new char[] { ' ' });

                List<string> ws = new List<string>();

                foreach (string s in words)
                {
                    if (s != "")
                        ws.Add(s);
                }

                double[] coeff = new double[nvars];
                for (int j = 0; j < nvars; j++)
                {
                    coeff[j] = Double.Parse((string)ws[j]);
                }
                Relation rel = (string)ws[nvars] == "=" ? Relation.Equal :
                              (string)ws[nvars] == "<=" ? Relation.LessOrEqual : Relation.GreaterOrEqual;
                double rs = Double.Parse((string)ws[nvars + 1]);
                lp.AddConstraint(coeff, rel, rs);
                sr.ReadLine(); //eat the empty line
            }

            sr.Close();

            return lp;

        }