private void timerSave_Tick(object sender, EventArgs e) { if (!SavingDataBase) { SavingDataBase = true; MoleculeGraph molecule = null; Molecule saved = null; try { if (ResultQueue.Count > 0) { lock (queueObjectLock) { molecule = ResultQueue.Dequeue(); ShowQueueDataSource(); } if (molecule != null && _moleculeService.GetByIdStructure(molecule.Nomenclature, molecule.IdStructure) == null) { if (!string.IsNullOrEmpty(molecule.IdStructure)) { saved = _moleculeService.Create(molecule); } var emptyMolecule = _moleculeService.GetByIdStructure(molecule.Nomenclature, null); if (emptyMolecule != null && _moleculeService.GetNotEmptyMoleculeCount(molecule.Nomenclature) > 0) { _moleculeService.Delete(emptyMolecule); } } if (saved != null) { SearchCount++; DatabaseCount++; } } DatabaseCount = _moleculeService.GetMoleculeCount(); } catch (Exception) { lock (queueObjectLock) { if (molecule != null && saved == null) { ResultQueue.Enqueue(molecule); } ShowQueueDataSource(); } } finally { SavingDataBase = false; } } }
public void InitializePopulation() { ResultList = new Queue <MoleculeGraph>(); Population = new List <SGChromosome>(_populationSize); Atoms = MoleculeGraph.ExtractAtomsFromNomenclature(Target); if (Atoms.Count > 1) { for (int i = 0; i < _populationSize; i++) { Population.Add(new SGChromosome(new MoleculeGraph(Target, Atoms))); } } }
private void StartSearch() { try { CancelSearch(); ClearCharts(); var populationSize = Int32.Parse(this.txtPopulationSize.Text); var maxGenerations = Int32.Parse(this.txtMaxGenerations.Text); var mutationRate = Double.Parse(this.txtMutationRate.Text); var nomenclature = this.txtNomenclature.Text; StartTimer(); this.searchThread = new Thread(() => { SetStatus("Iniciando população..."); ga = new StructureGenerator(nomenclature, populationSize, maxGenerations, mutationRate); StartWatchers(); SetStatus("Procurando estrutura molecular..."); MoleculeGraph molecule = ga.FindSolution(); SetDataSource(gridResult, molecule.LinkEdges.Select(x => new { From = x.From.ToString(), To = x.To.ToString() }).ToList()); SetStatus("Fim!"); }); searchThread.Start(); new Task(() => { while (searchThread != null && searchThread.IsAlive) { Thread.Sleep(500); } SetEnabled(btnCancel, false); SetEnabled(btnSearch, true); timer1.Enabled = false; }).Start(); } catch (Exception ex) { MessageBox.Show("Erro: " + ex.Message); } }
public Molecule GetCollectionFromGraph(MoleculeGraph molecule) { var collection = new Molecule(); if (molecule.AtomNodes != null && molecule.AtomNodes.Count > 0) { collection.AtomsCount = molecule.AtomNodes.Count; collection.DiferentAtomsCount = molecule.AtomNodes.GroupBy(x => x.Symbol).Count(); collection.SimpleAtoms = string.Join(",", molecule.AtomNodes.Select(x => x.ToString()).ToList()); collection.Atoms = molecule.AtomNodes.Select(x => _atomService.GetCollectionFromNode(x)).ToList(); } if (molecule.LinkEdges != null && molecule.LinkEdges.Count > 0) { collection.SimpleLinks = string.Join(",", molecule.LinkEdges.Select(x => _linkService.GetCollectionFromEdge(x)).Select(x => x.ToString()).ToList()); collection.Links = molecule.LinkEdges.Select(x => _linkService.GetCollectionFromEdge(x)).ToList(); } collection.Nomenclature = molecule.Nomenclature; collection.IdStructure = molecule.IdStructure; collection.Energy = molecule.Energy; collection.FromDataSet = molecule.FromDataSet; return(collection); }
public SGChromosome(MoleculeGraph molecule) { this.Molecule = molecule; this.CalcFitness(); }
public Molecule Create(MoleculeGraph molecule) { return(_moleculeRepository.Create(GetCollectionFromGraph(molecule))); }