public IActionResult CreateRoutine(CreateRoutineReceiver data) { // var db = Database.GetDatabase(_env.ContentRootPath); List <(Guid exerciseId, decimal amount, TimeSpan ts)> ps = new List <(Guid exerciseId, decimal amount, TimeSpan ts)>(); for (int i = 0; i < data.exercises.Length; i++) { var result = data.exercises[i].Split("_"); ps.Add((Guid.Parse(result[0]), decimal.Parse(result[1]), TimeSpan.FromSeconds(double.Parse(data.times[i])))); } var user = db.Users[0]; var newRoutine = new Routine() { Name = data.name, Exercises = new List <(Guid ExerciseId, decimal amountTypeDependent, TimeSpan timeTaken)>(ps), RoutineId = Guid.NewGuid() }; var newFitH = new FitnessHistory() { EventDateTime = DateTime.Now, RoutinePerformed = newRoutine, UserId = user.Id }; db.Users[0].History.Add(newFitH); Database.SaveDatabase(db, _env.ContentRootPath); return(Json(new { id = newRoutine.RoutineId })); }
public FitnessHistoryGraphItem(FitnessHistory fH, List <Exercise> exercises) { decimal cal = 0; Exercises = new List <string>(); foreach (var(ExerciseId, amountTypeDependent, timeTaken) in fH.RoutinePerformed.Exercises) { Exercise e = exercises.Single(ex => ex.ExerciseId == ExerciseId); //get ingredient name Exercises.Add(e.Name); cal += e.CaloriesPerUnit * amountTypeDependent; TimeTaken += timeTaken; } Calories = cal; Date = fH.EventDateTime; Routine = fH.RoutinePerformed; }
public double[] Solve(ObjectiveFunction ObjFunc) { if (Validation()) { PopulationsHistory.Clear(); FitnessHistory.Clear(); #region Initial Population and its Fitness double[][] initilaPopulation = initializePopulation(); PopulationsHistory.Add(initilaPopulation); Fitness initialFitness = new Fitness(GA_Settings.PopulationSize); Parallel.For(0, GA_Settings.PopulationSize, i => { initialFitness.FitnessPopulation[i] = ObjFunc(initilaPopulation[i]); }); initialFitness.SetFitenessData(); initialFitness.BestFeature = (double[])initilaPopulation[initialFitness.MaxFitnessIndex].Clone();; FitnessHistory.Add(initialFitness); OPTHistoryResult.Add(new Result { Parameters = (double[])initialFitness.BestFeature.Clone(), target = new double[2] { initialFitness.MaxFitness, initialFitness.MeanFitness } }); #endregion int gen = 0; #region While Loop for Genereations while (gen < GA_Settings.Generations - 1) { double[][] currentPopulation = PopulationsHistory.Last(); Fitness currentFitness = FitnessHistory.Last(); double[][] newPopulation = new double[GA_Settings.PopulationSize][]; #region GA newPopulation = Selection(currentPopulation, currentFitness.FitnessPopulation); newPopulation[0] = (double[])currentFitness.BestFeature.Clone(); newPopulation = Crossover(newPopulation); newPopulation = Mutation(newPopulation); #endregion Fitness newFitness = new Fitness(GA_Settings.PopulationSize); Parallel.For(0, GA_Settings.PopulationSize, i => { newFitness.FitnessPopulation[i] = ObjFunc(newPopulation[i]); }); newFitness.SetFitenessData(); newFitness.BestFeature = (double[])newPopulation[newFitness.MaxFitnessIndex].Clone(); #region Saving Best fearture NEW Vs CURRENT if (newFitness.MaxFitness <= currentFitness.MaxFitness) { newPopulation[0] = (double[])currentFitness.BestFeature.Clone(); newFitness.BestFeature = (double[])currentFitness.BestFeature.Clone(); newFitness.FitnessPopulation[0] = currentFitness.MaxFitness; newFitness.SetFitenessData(); } #endregion PopulationsHistory.Add(newPopulation); FitnessHistory.Add(newFitness); OPTHistoryResult.Add(new Result { Parameters = (double[])newFitness.BestFeature.Clone(), target = new double[2] { newFitness.MaxFitness, newFitness.MeanFitness } }); gen++; } #endregion OPTResult.Parameters = OPTHistoryResult.Last().Parameters; OPTResult.target = OPTHistoryResult.Last().target; } return(OPTHistoryResult.Last().Parameters); }