public void NewAlgorithmModel() { NewModel(); SetupModel setupModel = _model.NewSetupModel(); setupModel.Initialize(); ParticipantsModel participantsModel = _model.NewParticipantsModel(); participantsModel.Initialize(); PreferencesModel preferencesModel = _model.NewPreferencesModel(); preferencesModel.Initialize(); AlgorithmModel algorithmModel = _model.NewAlgorithmModel(); algorithmModel.Initialize(); _context.AlgorithmsChanged = false; algorithmModel.CreateGaleShapleyAlgorithm(); Assert.AreEqual(1, _context.Algorithms.Count()); AlgorithmVisitor _visitor = new AlgorithmVisitor(null, () => Assert.Fail()); _visitor.Visit(_context.Algorithms[0].Element); Assert.IsTrue(_context.AlgorithmsChanged); }
public AlgorithmModel Get( ) { AlgorithmModel model = new AlgorithmModel(); model.get(Uow); return(model); }
public void LoadData() { SaveData data; NewModel(); SetupModel setupModel = new SetupModel(_context); setupModel.Initialize(); ParticipantsModel participantsModel = new ParticipantsModel(_context); participantsModel.Initialize(); PreferencesModel preferencesModel = new PreferencesModel(_context); preferencesModel.Initialize(); AlgorithmModel algorithmModel = new AlgorithmModel(_context); algorithmModel.Initialize(); _model.SaveAsData(""); data = _persistence.MockData; NewModel(); _persistence.MockData = data; _model.LoadData(""); Assert.AreEqual(data, _persistence.Data); }
public ViewResult GenerateToken(string jsontext, string key, string algorithm) { try { jsontext = jsontext.Replace("\r", String.Empty); jsontext = jsontext.Replace("\n", String.Empty); var json = JsonConvert.DeserializeObject(jsontext); //KEY (your - 256 - bit - secret ) var verificarion = Encoding.UTF8.GetBytes(key); var sKey = new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(Encoding.UTF8.GetBytes(key)); var signingcredential = new Microsoft.IdentityModel.Tokens.SigningCredentials(sKey, algorithm); //HEADER var header = new JwtHeader(signingcredential); //PAYLOAD var payload = new JwtPayload(); payload = JwtPayload.Deserialize(jsontext); var secToken = new JwtSecurityToken(header, payload); var handler = new JwtSecurityTokenHandler(); var tokenString = handler.WriteToken(secToken); AlgorithmModel model = new AlgorithmModel(); model.token = tokenString; return(View(model)); } catch { ViewBag.Message = "Please enter a valid key"; return(View(new AlgorithmModel())); } }
public void BeginAlgorithm(string error) { int people; float margin; using (AlgorithmDialog dialog = new AlgorithmDialog()) { dialog.OpenDialog(error); if (dialog.Result.Count > 0) { people = (int)dialog.Result["People"]; margin = dialog.Result["Margin"] / 100; // Margin is given in centimeters (so we don't have inconsistensies between system languages } // e.g in Dutch you use a comma to denote a floating point, while in English you would use a period). else { people = 0; margin = 0; } } ComboBox algorithmComboBox = (ComboBox)view.Get(ProductGrid.PropertyEnum.AlgorithmComboBox); AlgorithmModel selectedAlgorithm = (AlgorithmModel)algorithmComboBox.SelectedItem; // Example chair and table ProductModel chair = ProductFactory.CreateProduct("Ahrend", 1, 1, "Stoelen", new CategoryModel(5, "Stoel", 0, "White")); ProductModel table = ProductFactory.CreateProduct("TableCompany", 2, 1, "Tafels", new CategoryModel(8, "Tafel", 2, "White")); StartAlgorithm(selectedAlgorithm, chair, table, people, margin); }
public ActionResult RunAlgorithm() { var model = new AlgorithmModel { Area = CurrentUser.Area ?? Area.North }; return(View(model)); }
public void AddToComboBox(AlgorithmModel algorithm) { comboBoxAlgorithms.Add(algorithm); ComboBox comboBox = (ComboBox)view.Get(ProductGrid.PropertyEnum.AlgorithmComboBox); comboBox.DataSource = comboBoxAlgorithms; comboBox.DisplayMember = "Name"; comboBox.ValueMember = "Value"; comboBox.DropDownStyle = ComboBoxStyle.DropDownList; }
public AlgorithmModel post(AlgorithmModel model) { if (model.LankymoData.HasValue && string.Format("{0:yyyy-MM-dd}", model.LankymoData.Value) == "2010-01-01") { model.ResetData(Uow, model.LankymoData.Value); } model.get(Uow); return(model); }
public AlgorithmViewModel(AlgorithmModel model, IContext context) { _model = model; _context = context; DeleteAlgorithmCommand = new DelegateCommand(param => OnDeleteAlgorithmCommand(Convert.ToInt32(param))); NewGaleShapleyAlgorithmCommand = new DelegateCommand(param => OnNewGaleShapleyAlgorithmCommand()); NewGeneticAlgorithmCommand = new DelegateCommand(param => OnNewGeneticAlgorithmCommand()); ToPreferencesCommand = new DelegateCommand(param => OnToPreferencesCommand()); ToRunCommand = new DelegateCommand(param => OnToRunCommand()); AlgorithmElements = new List <IAlgorithmOptionElement>(); AlgorithmOptions = new ObservableCollection <AlgorithmOptionBase>(); }
private void NewViewModel() { _context = new ModelContext(); _model = new AlgorithmModel(_context); _viewModel = new AlgorithmViewModel(_model, _context); SetupModel setupModel = new SetupModel(_context); setupModel.Initialize(); ParticipantsModel participantsModel = new ParticipantsModel(_context); participantsModel.Initialize(); PreferencesModel preferencesModel = new PreferencesModel(_context); preferencesModel.Initialize(); _viewModel.RefreshPage(); }
public void NewRunModel() { StablePairsEvaluation stablePairsEvaluation = new StablePairsEvaluation(); GroupHappinessEvaluation groupHappinessEvaluation = new GroupHappinessEvaluation(); EgalitarianHappinessEvaluation egalitarianHappinessEvaluation = new EgalitarianHappinessEvaluation(); NewModel(); SetupModel setupModel = _model.NewSetupModel(); setupModel.Initialize(); ParticipantsModel participantsModel = _model.NewParticipantsModel(); participantsModel.Initialize(); PreferencesModel preferencesModel = _model.NewPreferencesModel(); preferencesModel.Initialize(); AlgorithmModel algorithmModel = _model.NewAlgorithmModel(); algorithmModel.Initialize(); RunModel runModel = _model.NewRunModel(); runModel.Initialize(); int receivedEvents = 0; Task task; NewModel(); runModel.AlgorithmStarted += (object sender, AlgorithmEventArgs e) => receivedEvents++; runModel.AlgorithmFinished += (object sender, AlgorithmEventArgs e) => { receivedEvents++; Assert.AreEqual(_context.Algorithms[e.Index].Algorithm.Evaluate(stablePairsEvaluation), e.StablePairs); Assert.AreEqual(_context.Algorithms[e.Index].Algorithm.Evaluate(groupHappinessEvaluation), e.GroupHappiness); Assert.AreEqual(_context.Algorithms[e.Index].Algorithm.Evaluate(egalitarianHappinessEvaluation), e.EgalitarianHappiness); }; task = Task.Run(async() => { await runModel.RunSingleAlgorithm(0); Assert.AreEqual(2, receivedEvents); }); }
private void NewModel() { _context = new ModelContext(); SetupModel setupModel = new SetupModel(_context); setupModel.Initialize(); ParticipantsModel participantsModel = new ParticipantsModel(_context); participantsModel.Initialize(); PreferencesModel preferencesModel = new PreferencesModel(_context); preferencesModel.Initialize(); AlgorithmModel algorithmModel = new AlgorithmModel(_context); algorithmModel.Initialize(); algorithmModel.CreateGaleShapleyAlgorithm(); algorithmModel.CreateGeneticAlgorithm(); _model = new RunModel(_context); _model.Initialize(); _context.AlgorithmsChanged = false; }
/// <summary> /// Starts the selected algorithm. /// </summary> /// <param name="algorithm"></param> /// <param name="model1"></param> /// <param name="model2"></param> /// <param name="people"></param> /// <param name="margin"></param> public void StartAlgorithm(AlgorithmModel algorithm, ProductModel model1, ProductModel model2, int people, float margin) { // clear current placed products placedProducts.RemoveAll(x => x.Product.Name != "Muur" || x.Product.Name != "Raam" || x.Product.Name != "Deur" || x.Product.Name != "Stopcontact"); Type selectedType = algorithm.Value; IDesignAlgorithm algoInstance = (IDesignAlgorithm)Activator.CreateInstance(selectedType); List <ProductModel> result = algoInstance.Design(model1, model2, people, meterWidth, meterHeight, margin); foreach (ProductModel model in result) { // flip width and height, because the algorithm flips those because products are turned sideways int width = model.Width; model.Width = model.Height; model.Height = width; AddNewProduct(model, model.Location.X, model.Location.Y, model.Width, model.Height, true, 0); } }
public ActionResult RunAlgorithm(Area area) { var model = new AlgorithmModel { Area = area }; var status = _tutorTraineeService.ResetRecommended(model.Area); var trainees = _traineeService.GetUnMatchedAlg(model.Area); if (trainees.Success) { model.TraineeList = trainees.Data; } else { return(Json(trainees)); } var tutors = _tutorService.GetUnMatchedAlg(model.Area); if (tutors.Success) { model.TutorList = tutors.Data; } else { return(Json(tutors)); } if (status.Success) { status = _tutorTraineeService.RunAlgorithm(model); if (status.Success) { var result = _tutorTraineeService.IsUnRecommended(model.Area); return(Json(result)); } return(Json(status)); } return(Json(status)); }
public void SaveData() { NewModel(); SetupModel setupModel = new SetupModel(_context); setupModel.Initialize(); ParticipantsModel participantsModel = new ParticipantsModel(_context); participantsModel.Initialize(); PreferencesModel preferencesModel = new PreferencesModel(_context); preferencesModel.Initialize(); AlgorithmModel algorithmModel = new AlgorithmModel(_context); algorithmModel.Initialize(); _model.SaveAsData(""); Assert.IsNotNull(_persistence.MockData); Assert.IsNotNull(_persistence.MockData.Group1Name); Assert.IsNotNull(_persistence.MockData.Group2Name); Assert.IsNotNull(_persistence.MockData.Participants); Assert.IsNotNull(_persistence.MockData.Preferences); Assert.IsNotNull(_persistence.MockData.Algorithms); }
private void NewModel() { List <Participant> participants = new List <Participant>(); for (int i = 0; i < 5; i++) { participants.Add(new Participant(i, "test" + i, MarriageGroup.Group1)); } for (int i = 5; i < 10; i++) { participants.Add(new Participant(i, "test" + i, MarriageGroup.Group2)); } _context = new ModelContext { TotalSize = 10, Participants = participants, SetupChanged = true, ParticipantsChanged = true, PreferencesChanged = true, }; _context.Priorities = new Priorities(new Dictionary <int, UnitSet>()); foreach (Participant participant in _context.Group1Participants) { _context.Priorities[participant.ID] = new UnitSet(_context.Group2Participants.Select(x => x.ID).ToList()); } foreach (Participant participant in _context.Group2Participants) { _context.Priorities[participant.ID] = new UnitSet(_context.Group1Participants.Select(x => x.ID).ToList()); } _model = new AlgorithmModel(_context); _model.Initialize(); _context.AlgorithmsChanged = false; }
public IActionResult Index(AlgorithmModel algorithmModel) { if (ModelState.IsValid) { decimal result = 0; ReturnParameter param = _calculateAlgotithm.CalAlgorithm(algorithmModel.a, algorithmModel.b, algorithmModel.FunctionType); if (param.Flag == true) { result = Convert.ToDecimal(param.Var); _log.LogData(algorithmModel.FunctionType, algorithmModel.a, algorithmModel.b, result.ToString()); result = Math.Round(result, 2); return(RedirectToAction("Result", "Home", new { @result = param.Var })); } else { _log.LogData(algorithmModel.FunctionType, algorithmModel.a, algorithmModel.b, param.Message); return(RedirectToAction("Result", "Home", new { @result = param.Message })); } } return(View()); }
private void NewViewModel() { _context = new ModelContext(); _model = new RunModel(_context); _viewModel = new RunViewModel(_model, _context); SetupModel setupModel = new SetupModel(_context); setupModel.Initialize(); ParticipantsModel participantsModel = new ParticipantsModel(_context); participantsModel.Initialize(); PreferencesModel preferencesModel = new PreferencesModel(_context); preferencesModel.Initialize(); AlgorithmModel algorithmModel = new AlgorithmModel(_context); algorithmModel.Initialize(); algorithmModel.CreateGaleShapleyAlgorithm(); algorithmModel.CreateGeneticAlgorithm(); _viewModel.RefreshPage(); }
public Solution FindSolution(AlgorithmModel model = null) { var algorithmList = new List <AlgorithmModel>(); DateTime startTime = DateTime.Now; Random rnd = new Random(); int iterations_count = 0; List <int> T = new List <int>() { 15, 11, 12, 100, 14, 15 }; Solution S = new Solution(); S.assignments = S.GenerateSolution(); //Console.WriteLine("Score ne fillim : {0}",S.GetScore()); Solution H = new Solution(); H = H.Copy(S.assignments); Solution Best = new Solution(); Best = Best.Copy(S.assignments); var R = new Solution(); algorithmList.Add(new AlgorithmModel() { ChangeTimeSlot = 0, HillCliming = 0, ImprovementPercent = 0, Iteration = iterations_count + 1, IterationNoResult = 0, NewHomeBase = 0, Swaps = 0, Score = S.GetScore() }); Stopwatch s = new Stopwatch(); s.Start(); while (iterations_count < iterations && s.Elapsed < TimeSpan.FromSeconds(seconds)) { int climb_iterations = 0; int time = T[rnd.Next(T.Count)]; while (iterations_count < iterations && climb_iterations < time && s.Elapsed < TimeSpan.FromSeconds(seconds)) { if (model != null && model.HillCliming == climb_iterations) { break; } R = R.Copy(S.assignments); R.Tweak(); var random = rnd.Next(1, 70); if (R.GetScore() < S.GetScore()) { S = S.Copy(R.assignments); } climb_iterations++; } int noResult = 1; double improvementPercent = 0; if (S.GetScore() < Best.GetScore()) { double decrease = Best.GetScore() - S.GetScore(); improvementPercent = decrease / Best.GetScore() * 100; noResult = 0; Best = Best.Copy(S.assignments); } int newHomeBase = 0, swaps = 0, changeTimeSlots = 0; H = H.Copy(NewHomeBase(H, S, out newHomeBase, model?.NewHomeBase).assignments); S = S.Copy(H.assignments); S.Perturb(out swaps, out changeTimeSlots, model?.Swaps, model?.ChangeTimeSlot); iterations_count++; algorithmList.Add(new AlgorithmModel() { ChangeTimeSlot = changeTimeSlots, HillCliming = climb_iterations, ImprovementPercent = improvementPercent, Iteration = iterations_count + 1, IterationNoResult = noResult, NewHomeBase = newHomeBase, Swaps = swaps, Score = Best.GetScore() }); // Console.WriteLine(Best.GetScore()); if (Best.GetScore() == 0) { break; } } s.Stop(); Best.AlgorithmModels = algorithmList; return(Best); }
// This method is here to populate the algorithms until // someone wants to update that process public void PopulateAlgorithms() { // Standard test setup algorithm AddToComboBox(AlgorithmModel.CreateAlgorithm("Toets lokaal", typeof(TestSetupDesign))); }
public StatusModel RunAlgorithm(AlgorithmModel model) { var status = new StatusModel(false, String.Empty); int numTrainees = model.TraineeList.Count; int numTutors = model.TutorList.Count; int[,] utilityMat = new int[numTrainees, numTutors]; int maxUtil = 0; for (int traineeIdx = 0; traineeIdx < numTrainees; traineeIdx++) { for (int tutorIdx = 0; tutorIdx < numTutors; tutorIdx++) { TraineeModel trainee = model.TraineeList[traineeIdx]; TutorModel tutor = model.TutorList[tutorIdx]; int libaWeight = 200; int majorWeight = 2000; int minorWeight = 1000; int clusterWeight = 20; int institutionWeight = 50; bool isMechina = (trainee.EnglishLevel > 0 || trainee.MathLevel > 0 || trainee.PhysicsLevel > 0); if (isMechina) { majorWeight = majorWeight / 5; institutionWeight = institutionWeight / 4; minorWeight = minorWeight / 5; clusterWeight = clusterWeight / 4; } //Gender must match if (trainee.Gender != tutor.Gender) { utilityMat[traineeIdx, tutorIdx] = -1; continue; } //TODO: Check with or if required AcademicYear instead of SemesterNumber //tutor has senority over trainee if (trainee.SemesterNumber > tutor.SemesterNumber) { utilityMat[traineeIdx, tutorIdx] = -1; continue; } //Liba matching if ((int)trainee.MathLevel > (int)tutor.MathLevel || (int)trainee.EnglishLevel > (int)tutor.EnglishLevel || (int)trainee.PhysicsLevel > (int)tutor.PhysicsLevel) { utilityMat[traineeIdx, tutorIdx] = -1; continue; } else if (isMechina) { utilityMat[traineeIdx, tutorIdx] += libaWeight; } //Trainee Major match int calc1 = 0; if (trainee.AcademicMajor.Id == tutor.AcademicMajor.Id) { calc1 += majorWeight; } else if (tutor.AcademicMajor1 != null && trainee.AcademicMajor.Id == tutor.AcademicMajor1.Id) { calc1 += majorWeight - (int)(0.1 * majorWeight); } //Trainee minor match int calc2 = 0; if (trainee.AcademicMajor1 != null) { if (tutor.AcademicMajor1 != null && trainee.AcademicMajor1.Id == tutor.AcademicMajor1.Id) { calc2 += minorWeight; } else if (trainee.AcademicMajor1.Id == tutor.AcademicMajor.Id) { calc2 += minorWeight - (int)(0.1 * minorWeight); } } else { calc1 = (int)(1.5 * calc1); } //Trainee needed help exsists if (trainee.AcademicMajor2 != null) { if (trainee.AcademicMajor2.Id == tutor.AcademicMajor.Id) { calc1 += (int)(majorWeight * 0.5); } else if (tutor.AcademicMajor1 != null && trainee.AcademicMajor2.Id == tutor.AcademicMajor1.Id) { calc1 += (int)(majorWeight * 0.5); } } else { if (calc1 > 0) { calc1 += (int)(majorWeight * 0.5); } if (calc2 > 0) { calc2 += (int)(minorWeight * 0.5); } } //adding calc to matrix utilityMat[traineeIdx, tutorIdx] += (calc1 + calc2); //Academic Clusters //Trainee major cluster calc1 = 0; if (trainee.AcademicMajor.AcademicCluster == tutor.AcademicMajor.AcademicCluster) { calc1 += clusterWeight; } else if (tutor.AcademicMajor1 != null && trainee.AcademicMajor.AcademicCluster == tutor.AcademicMajor1.AcademicCluster) { calc1 += clusterWeight - (int)(0.1 * clusterWeight); } //Trainee minor cluster calc2 = 0; if (trainee.AcademicMajor1 != null) { if (tutor.AcademicMajor1 != null && trainee.AcademicMajor1.AcademicCluster == tutor.AcademicMajor1.AcademicCluster) { calc2 += clusterWeight; } else if (trainee.AcademicMajor1.AcademicCluster == tutor.AcademicMajor.AcademicCluster) { calc2 += clusterWeight - (int)(0.1 * clusterWeight); } } else { calc1 = (int)(1.5 * calc1); } //Trainee needed help exsists - consider cluster if (trainee.AcademicMajor2 != null) { if (trainee.AcademicMajor2.AcademicCluster == tutor.AcademicMajor.AcademicCluster) { calc1 += (int)(clusterWeight * 0.5); } else if (tutor.AcademicMajor1 != null && trainee.AcademicMajor2.AcademicCluster == tutor.AcademicMajor1.AcademicCluster) { calc1 += (int)(clusterWeight * 0.35); } } else { if (calc1 > 0) { calc1 += (int)(clusterWeight * 0.5); } if (calc2 > 0) { calc2 += (int)(clusterWeight * 0.5); } } //adding calc to matrix utilityMat[traineeIdx, tutorIdx] += (calc1 + calc2); //AcademicInstitution needs to be alike.. or similar //Academic institution does not disqualify matches if (trainee.AcademicInstitution.InstitutionType == tutor.AcademicInstitution.InstitutionType) { utilityMat[traineeIdx, tutorIdx] += institutionWeight; } else if (trainee.AcademicInstitution.InstitutionType < tutor.AcademicInstitution.InstitutionType) { utilityMat[traineeIdx, tutorIdx] += institutionWeight / 2; } //If they have the same Academic Institution if (trainee.AcademicInstitution.Name == tutor.AcademicInstitution.Name) { utilityMat[traineeIdx, tutorIdx] += institutionWeight; } //check what is max util if (utilityMat[traineeIdx, tutorIdx] > maxUtil) { maxUtil = utilityMat[traineeIdx, tutorIdx]; } } } //Generate cost matrix //Create a balanced matrix for the algorithm NXN int matSize = 0; matSize = numTrainees >= numTutors ? numTrainees : numTutors; int[,] costMatrix = new int[matSize, matSize]; int bigM = maxUtil * 1000; for (int traineeIdx = 0; traineeIdx < numTrainees; traineeIdx++) { for (int tutorIdx = 0; tutorIdx < numTutors; tutorIdx++) { //replace -1 (forbidden) with M if (utilityMat[traineeIdx, tutorIdx] == -1) { costMatrix[traineeIdx, tutorIdx] = bigM; } //Convert utility to cost else { costMatrix[traineeIdx, tutorIdx] = maxUtil - utilityMat[traineeIdx, tutorIdx]; } } } // Send Matrix to Algorithm MatchingAlgorithm alg = new MatchingAlgorithm(costMatrix); int[,] finalmatch = alg.Run(); //Translate Matrix to actual matches var unmatchedTrainees = new List <int>(); var isMatchedTrainees = new int[numTrainees]; var unmatchedTutors = new List <int>(); var isMatchedTutors = new int[numTutors]; try { for (int traineeIdx = 0; traineeIdx < numTrainees; traineeIdx++) { for (int tutorIdx = 0; tutorIdx < numTutors; tutorIdx++) { if (finalmatch[traineeIdx, tutorIdx] == 5) { if (utilityMat[traineeIdx, tutorIdx] > 100) { //Create Match var result = TutorTraineeAdd(model.TutorList[tutorIdx], model.TraineeList[traineeIdx]); isMatchedTutors[tutorIdx] = 1; isMatchedTrainees[traineeIdx] = 1; if (!result.Success) { throw new Exception(result.Message); } } } } } for (int i = 0; i < numTrainees; i++) { if (isMatchedTrainees[i] == 0) { unmatchedTrainees.Add(i); } } for (int i = 0; i < numTutors; i++) { if (isMatchedTutors[i] == 0) { unmatchedTutors.Add(i); } } if (unmatchedTutors.Count < unmatchedTrainees.Count) { foreach (var tutor in unmatchedTutors) { var maxValue = -1; int loc = -1; foreach (var trainee in unmatchedTrainees) { if (utilityMat[trainee, tutor] > maxValue) { maxValue = utilityMat[trainee, tutor]; loc = trainee; } } if (maxValue > 100 && loc > -1) { TutorTraineeAdd(model.TutorList[tutor], model.TraineeList[loc]); unmatchedTrainees.Remove(loc); } } } else { foreach (var trainee in unmatchedTrainees) { var maxValue = -1; int loc = -1; foreach (var tutor in unmatchedTutors) { if (utilityMat[trainee, tutor] > maxValue) { maxValue = utilityMat[trainee, tutor]; loc = tutor; } } if (maxValue > 100 && loc > -1) { TutorTraineeAdd(model.TutorList[loc], model.TraineeList[trainee]); unmatchedTrainees.Remove(loc); } } } status.Success = true; } catch (Exception ex) { status.Message = String.Format("שגיאה במהלך הרצת האלגוריתם"); LogService.Logger.Error(status.Message, ex); } return(status); }