public ProblemOutput Solve(ProblemInput problem)
        {
            ProblemOutput solution = new ProblemOutput
            {
                D             = problem.D,
                HasSolution   = false,
                Value         = double.NaN,
                Solution      = Enumerable.Repeat(double.NaN, problem.N).ToArray(),
                SkipedIndexes = problem.SkipedIndexes
            };

            if (problem.N > 0)
            {
                MWArray[]      input = new MWArray[4];
                MWNumericArray _h    = problem.Quadratic;
                MWNumericArray _f    = problem.Linear;
                MWNumericArray _A    = problem.ConstraintMatrix;
                MWNumericArray _B    = problem.ConstraintValues;
                input[0] = _h;
                input[1] = _f;
                input[2] = _A;
                input[3] = _B;
                try
                {
                    SolveQPClass r      = new SolveQPClass();
                    MWArray[]    result = new MWArray[3];
                    r.SolveQP(result.Length, ref result, input);
                    bool success = ((MWNumericArray)result[2]).ToScalarDouble() > 0;
                    if (success)
                    {
                        double[] x   = (double[])(((MWNumericArray)result[0]).ToVector(MWArrayComponent.Real));
                        double   val = ((MWNumericArray)result[1]).ToScalarDouble();
                        solution.HasSolution = success;
                        solution.Value       = -val;
                        solution.Solution    = x;
                    }
                    r.Dispose();
                }
                catch (Exception e)
                {
                    return(solution);
                }
            }
            return(solution);
        }
Beispiel #2
0
        public static void CancelOrders(List <Drone> drones, ProblemOutput output)
        {
            if (drones.Count != 0)
            {
                return;
            }

            for (int i = 0; i < drones.Count; i++)
            {
                int startIndex = output.Count - 2 - i * 2;
                drones[drones.Count - 1 - i].CurrentTime     = output[startIndex].Time;
                drones[drones.Count - 1 - i].CurrentPosition = output[startIndex].StartPosition;
            }

            for (int i = 0; i < 2 * drones.Count; i++)
            {
                output.RemoveAt(output.Count - 1 - i);
            }
        }
Beispiel #3
0
 public ProblemOutput Solve(ProblemInput problem)
 {
     try {
         GRBEnv   env      = new GRBEnv();
         double[] lb       = ArrayHelper.CreateArray <double>(problem.Linear.Length);
         double[] sol      = ArrayHelper.CreateArray <double>(problem.Linear.Length);
         double   val      = 0.0;
         var      success  = GurobiSolve(env, problem.ConstraintMatrix.GetLength(0), problem.ConstraintMatrix.GetLength(1), problem.Linear, problem.Quadratic, problem.ConstraintMatrix, problem.ConstraintValues, lb, null, null, sol, ref val);
         var      solution = new ProblemOutput
         {
             HasSolution = success,
             Value       = success ? val : double.NaN,
             Solution    = success ? sol : Enumerable.Repeat(double.NaN, problem.N).ToArray()
         };
         // Dispose of environment
         env.Dispose();
         return(solution);
     } catch (GRBException e) {
         return(null);
     }
 }
        public ProblemOutput[] Solve(ProblemInput[] problem)
        {
            ProblemOutput[] solutions   = new ProblemOutput[problem.Length];
            var             inputLength = problem.Where(c => c.N > 0).Count();

            MWArray[] input = new MWArray[4 * inputLength];
            int       index = 0;

            for (int i = 0; i < problem.Length; i++)
            {
                if (problem[i].N > 0)
                {
                    MWNumericArray _h = problem[i].Quadratic;
                    MWNumericArray _f = problem[i].Linear;
                    MWNumericArray _A = problem[i].ConstraintMatrix;
                    MWNumericArray _B = problem[i].ConstraintValues;
                    input[index * 4]     = _h;
                    input[index * 4 + 1] = _f;
                    input[index * 4 + 2] = _A;
                    input[index * 4 + 3] = _B;
                    index++;
                }

                solutions[i] = new ProblemOutput
                {
                    D             = problem[i].D,
                    HasSolution   = false,
                    Value         = double.NaN,
                    Solution      = Enumerable.Repeat(double.NaN, problem[i].N).ToArray(),
                    SkipedIndexes = problem[i].SkipedIndexes
                };
            }
            try
            {
                if (inputLength > 0)
                {
                    SolveQPClass r      = new SolveQPClass();
                    MWArray[]    result = new MWArray[3 * inputLength];
                    r.SolveQP(result.Length, ref result, input);
                    index = 0;
                    for (int i = 0; i < problem.Length; i++)
                    {
                        if (problem[i].N > 0)
                        {
                            bool success = ((MWNumericArray)result[index * 3 + 2]).ToScalarDouble() > 0;
                            if (success)
                            {
                                double[] x   = (double[])(((MWNumericArray)result[index * 3]).ToVector(MWArrayComponent.Real));
                                double   val = ((MWNumericArray)result[index * 3 + 1]).ToScalarDouble();
                                solutions[i].HasSolution = success;
                                solutions[i].Value       = -val;
                                solutions[i].Solution    = x;
                            }
                            index++;
                        }
                    }
                    r.Dispose();
                }
                return(solutions);
            }
            catch (Exception e)
            {
                return(solutions);
            }
        }