/// <summary>
        /// Converting problem solution view to database object
        /// </summary>
        /// <param name="psv"></param>
        /// <returns>Problem solution POCO</returns>
        public ProblemSolution Convert_ProblemSolutionViewToPOCO(ProblemSolutionView psv)
        {
            ProblemSolution ps = new ProblemSolution();

            Convert_ProblemSolutionViewToExistingPOCO(psv, ps);
            return(ps);
        }
        public async Task <Result <ProblemSolution> > GetById(string id, string username)
        {
            var res = new Result <ProblemSolution>()
            {
                Success = false
            };

            ProblemSolution item = null;

            try
            {
                var jsonText = await _repository.GetById(id, username);

                if (!string.IsNullOrWhiteSpace(jsonText))
                {
                    item = JsonSerializer.Deserialize <ProblemSolution>(jsonText);
                }

                res.ErrorCode      = 0;
                res.Success        = true;
                res.ReturnedObject = item;
            }
            catch (Exception e)
            {
                res.ErrorCode        = -1;
                res.Success          = false;
                res.ErrorDescription = e.Message;
            }

            return(res);
        }
        public async Task <Result> Save(ProblemSolution solution, string user)
        {
            var res = new Result()
            {
                Success = false
            };

            var key         = solution.Id.ToString();
            var requestDate = DateTime.Now;

            try
            {
                var item = JsonSerializer.Serialize <ProblemSolution>(solution);
                await _repository.Save(key, item, user, requestDate);

                res.Success = true;
            }
            catch (Exception e)
            {
                res.ErrorCode        = -1;
                res.ErrorDescription = e.Message;
            }

            return(res);
        }
 /// <summary>
 /// Converting problem solution database object to view
 /// </summary>
 /// <param name="x">Input problem solution poco</param>
 /// <returns>Problem solution view</returns>
 public ProblemSolutionView Convert_ProblemSolutionPOCOToView(ProblemSolution x)
 {
     return(new ProblemSolutionView
     {
         Active = x.Active,
         Comment = x.Comment,
         EndDate = x.End,
         EndTime = x.End.TimeOfDay,
         ProblemId = x.ProblemId,
         ProblemSolutionId = x.ProblemSolutionId,
         StartDate = x.Start,
         StartTime = x.Start.TimeOfDay,
         Status = x.Status,
         RequestDescription = x.Problem.Comment,
         UserFullName = x.Problem.User.Name + " " + x.Problem.User.Surname
     });
 }
Example #5
0
        public async Task <ActionResult> Create(ProblemSolutionView problemSolutionV, string selectedProblem = "")
        {
            try
            {
                ProblemSolution ps = dc.Convert_ProblemSolutionViewToPOCO(problemSolutionV);
                int             id = int.Parse(selectedProblem);
                ps.ProblemId = problems.FirstOrDefault(x => x.ProblemId == id).ProblemId;
                ps.Active    = true;
                sm.Add(ps);
                //  db.ProblemSolutions.Add(ps);
                await sm.SaveChanges();

                return(RedirectToAction("Index"));
            }
            catch (Exception e)
            {
                TempData["msg"] = "Data is incomplete";
                return(RedirectToAction("Create"));
            }
        }
 /// <summary>
 /// Converting problem solution view to existing database object
 /// </summary>
 /// <param name="psv">View of problem solution</param>
 /// <param name="ps">Existing poco of problem solution</param>
 /// <returns></returns>
 public void Convert_ProblemSolutionViewToExistingPOCO(ProblemSolutionView psv, ProblemSolution ps)
 {
     ps.Active            = psv.Active;
     ps.Comment           = psv.Comment;
     ps.End               = psv.EndDate.Date.AddMilliseconds(psv.EndTime.TotalMilliseconds);
     ps.ProblemId         = psv.ProblemId;
     ps.ProblemSolutionId = psv.ProblemSolutionId;
     ps.Start             = psv.StartDate.Date.AddMilliseconds(psv.StartTime.TotalMilliseconds);
     ps.Status            = psv.Status;
 }
Example #7
0
        public ProblemSolution Solve(ProblemConfiguration config)
        {
            var res = new ProblemSolution();

            //https://developers.google.com/optimization/mip/mip_var_array#c_1

            if (config.AvailableAmount <= 0)
            {
                throw new ArgumentException("Invalid configuration - Available amount");
            }

            if (config.Products == null || config.Products.Count == 0)
            {
                throw new ArgumentException("Invalid configuration - Products");
            }

            // [START solver]
            // Create the linear solver with the CBC backend.
            Solver solver = Solver.CreateSolver(/*"SimpleMipProgram",*/ "CBC");
            // [END solver]

            // [START variables]
            // x, y and z are integer non-negative variables.
            var variables = new List <Variable>();

            foreach (var p in config.Products)
            {
                var variable = solver.MakeIntVar(0.0, double.PositiveInfinity, GetVariableName(p));

                variables.Add(variable);

                if (p.MaxUnits == 0)
                {
                    solver.Add(variable == 0);
                }
                else if (
                    (p.MinUnits >= 0 && p.MaxUnits > 0 && p.MinUnits < p.MaxUnits) ||
                    (p.MaxUnits == p.MinUnits && p.MaxUnits > 0)
                    )
                {
                    // other constraints
                    solver.Add(variable >= p.MinUnits);
                    solver.Add(variable <= p.MaxUnits);
                }
            }

            res.TotalVariables = solver.NumVariables();


            // [END variables]

            // [START constraints]
            //  (unitPriceX * x) + (unitPriceY * y) + (unitPriceZ *z) <= maxValue.

            var c = solver.MakeConstraint(0, config.AvailableAmount);

            int i = 0;

            foreach (var variable in variables)
            {
                c.SetCoefficient(variable, (double)config.Products[i].UnitPrice);
                i++;
            }

            res.TotalConstraints = solver.NumConstraints();
            // [END constraints]


            Objective objective = solver.Objective();

            i = 0;
            foreach (var variable in variables)
            {
                objective.SetCoefficient(variable, (double)config.Products[i].UnitPrice);
                i++;
            }
            objective.SetMaximization();

            // [START solve]
            Solver.ResultStatus resultStatus = solver.Solve();
            // [END solve]

            // [START print_solution]
            // Check that the problem has an optimal solution.
            if (resultStatus != Solver.ResultStatus.OPTIMAL)
            {
                res.HasOptimalSolution = false;
                return(res);
            }

            res.HasOptimalSolution = true;


            res.FinalAmount     = (decimal)solver.Objective().Value();
            res.RemainingAmount = Math.Round(config.AvailableAmount - (double)res.FinalAmount, 2);

            i = 0;
            foreach (var variable in variables)
            {
                var itemValue = variable.SolutionValue() * (double)config.Products[i].UnitPrice;
                itemValue = Math.Round(itemValue, 2);

                res.ResponseVariables.Add(
                    new SolutionVariable()
                {
                    Name          = config.Products[i].Name, //variable.Name(),
                    Code          = config.Products[i].Code,
                    SolutionValue = variable.SolutionValue(),
                    UnitPrice     = config.Products[i].UnitPrice,
                    FinalAmount   = itemValue,
                    Description   = variable.Name(),
                    Details       =
                        $"{variable.Name()} = {variable.SolutionValue()}  ///  {variable.SolutionValue()} * {config.Products[i].UnitPrice} = {itemValue} euros "
                });

                i++;
            }

            res.SolveTimeMs = solver.WallTime();
            res.Iterations  = solver.Iterations();
            res.Nodes       = solver.Nodes();

            return(res);
        }