public BionicModel() { Attributes = new NamedModelEntityCollection <Parameter>(); FitnessCriterion = null; FunctionalConstraints = new NamedModelEntityCollection <Constraint>(); CurrentPopulation = new Population(); CurrentGeneration = 0; }
/// <summary> /// Copies <see cref="NamedModelEntity"/> instances or instances of the derived classes from /// <paramref name="source"/> collection to the <paramref name="destination"/> collection /// </summary> /// <typeparam name="TSource">Type of the source items</typeparam> /// <typeparam name="TDestination">Type of the destination items</typeparam> /// <param name="source">Source collection (copy from)</param> /// <param name="destination">Destination collection (copy to)</param> private static void CopyModelEntites <TSource, TDestination>(NamedModelEntityCollection <TSource> source, NamedModelEntityCollection <TDestination> destination) where TSource : TDestination where TDestination : NamedModelEntity, ICloneable { foreach (TSource item in source.Values) { destination.Add((TDestination)item.Clone()); } }
/// <summary> /// Shows addForm and update DataGrid /// </summary> /// <typeparam name="T">inheritors of NamedModelEntity</typeparam> /// <param name="addForm">form for adding entity</param> /// <param name="entities">collection of model which will be shown in the DataGrid</param> protected void AddEntity <T>(Form addForm, NamedModelEntityCollection <T> entities) where T : NamedModelEntity { if (addForm.ShowDialog() == DialogResult.OK) { this.UpdateParametersDataGrid(entities); } addForm.Dispose(); }
/// <summary> /// Shows editForm and update DataGrid /// </summary> /// <typeparam name="T">inheritors of NamedModelEntity</typeparam> /// <param name="editForm">form for editing entity</param> /// <param name="entities">collection of model which will be shown in the DataGrid</param> /// <param name="index">index of the row to be edited <seealso cref="GetSelectedEntityId()"/></param> protected void EditEntity <T>(Form editForm, NamedModelEntityCollection <T> entities, int index) where T : NamedModelEntity { if (editForm.ShowDialog() == DialogResult.OK) { this.UpdateParametersDataGrid(entities); SelectDataGridViewRow(index); } editForm.Dispose(); }
private static void CopyParameters(NamedModelEntityCollection <PromotableConstant> source, NamedModelEntityCollection <Parameter> destination) { if (source == null) { throw new ArgumentNullException("source"); } IEnumerable <Parameter> parameters = source.Values.Where(promotableConstant => promotableConstant.IsPromoted) .Select(promotableConstant => promotableConstant.GetParameter()); CopyEntities(parameters, destination); }
private static void CopyConstraints(NamedModelEntityCollection <PromotableCriterion> source, NamedModelEntityCollection <Constraint> destination) { if (source == null) { throw new ArgumentNullException("source"); } IEnumerable <Constraint> constraints = source.Values.Where(promotableCriterion => promotableCriterion.IsPromoted) .Select(promotableCriterion => promotableCriterion.GetConstraint()); CopyEntities(constraints, destination); }
private void GenerateAttributeValues(Individual targetIndividual, NamedModelEntityCollection <Parameter> attributes) { foreach (Parameter attribute in attributes.Values) { if (attribute == null) { throw new ArgumentException("One of the attributes in null"); } double attributeValue = attribute.MinValue + randomizer.NextDouble() * (attribute.MaxValue - attribute.MinValue); targetIndividual.AttributeValues.Add(attribute.Id, attributeValue); } }
protected override void UpdateParametersDataGrid <T>(NamedModelEntityCollection <T> entities) { base.UpdateParametersDataGrid <T>(entities); this.dgvModelEntities.SuspendLayout(); int ind = 0; foreach (KeyValuePair <TId, AdequacyCriterion> criterion in ModelStorage.Instance.Model.Criteria) { this.dgvModelEntities[3, ind].Value = AdequacyCriterionTypeManager.GetFriendlyName(criterion.Value.AdequacyType); ind++; } this.dgvModelEntities.ResumeLayout(); }
public IEnumerable <Individual> Breed( Population parentPopulation, NamedModelEntityCollection <Parameter> attributes, uint attributeDeviationPercent, uint maxIndividualDescendants, uint generationNumber) { if (parentPopulation == null) { throw new ArgumentNullException("parentPopulation"); } if (attributes == null) { throw new ArgumentNullException("attributes"); } if (parentPopulation.Count == 0) { throw new ArgumentException("Parent population is empty, nothing to breed", "parentPopulation"); } List <Individual> activeParents = parentPopulation.Where(ind => ind.Value.IsActive).Select(ind => ind.Value).ToList(); if (activeParents.Count == 0) { throw new ArgumentException("No active individuals in parent population, nothing to breed", "parentPopulation"); } List <Individual> descendants = new List <Individual>(); // Need descendant counter for unique ID generation - BAD HACK int descendantsCount = 0; foreach (Individual parent in activeParents) { for (int i = 0; i < maxIndividualDescendants; i++) { // TODO: Find a better way to assign new IDs to the descendants. // Current 'descendantsCount' reveals int nature of the ID, which is bad Individual descendant = CreateDescendant(parent, parentPopulation.GetFreeConsequentId() + descendantsCount, generationNumber, attributes, attributeDeviationPercent); descendants.Add(descendant); descendantsCount++; } } return(descendants); }
protected override void UpdateParametersDataGrid <T>(NamedModelEntityCollection <T> entities) { base.UpdateParametersDataGrid <T>(entities); this.dgvModelEntities.SuspendLayout(); int ind = 0; foreach (KeyValuePair <TId, Parameter> parameter in ModelStorage.Instance.Model.IdentificationParameters) { this.dgvModelEntities[3, ind].Value = parameter.Value.MinValue.ToString(SettingsManager.Instance.DoubleStringFormat); this.dgvModelEntities[4, ind].Value = parameter.Value.MaxValue.ToString(SettingsManager.Instance.DoubleStringFormat); ind++; } this.dgvModelEntities.ResumeLayout(); }
private Individual CreateDescendant( Individual parent, TId descendantId, uint generationNumber, NamedModelEntityCollection <Parameter> attributes, uint attributeDeviationPercent) { Individual descendant = new Individual(descendantId, generationNumber); foreach (Parameter attribute in attributes.Values) { descendant.AttributeValues.Add(attribute.Id, CreateAttributeValue(attribute, parent.AttributeValues[attribute.Id], attributeDeviationPercent)); } return(descendant); }
/// <summary> /// TODO: Still doesn't work ( /// </summary> /// <typeparam name="T">inheritors of NamedModelEntity</typeparam> /// <param name="entities">collection of model which will be shown in the DataGrid</param> protected virtual void UpdateParametersDataGrid <T>(NamedModelEntityCollection <T> entities) where T : NamedModelEntity { this.dgvModelEntities.SuspendLayout(); this.dgvModelEntities.Rows.Clear(); foreach (NamedModelEntity entity in entities.Values) { int ind = this.dgvModelEntities.Rows.Add(); this.dgvModelEntities[0, ind].Value = entity.Id; this.dgvModelEntities[1, ind].Value = entity.Name; this.dgvModelEntities[2, ind].Value = entity.VariableIdentifier; } this.dgvModelEntities.ResumeLayout(); }
/// <summary> /// Reads a collection of <see cref="AdequacyCriterion"/> from XML /// </summary> /// <param name="criteriaCollection">A collection to be read from XML</param> /// <param name="criteriaCollectionElement"><see cref="XElement"/> to read a collection from</param> private static void ReadCriteria(NamedModelEntityCollection <AdequacyCriterion> criteriaCollection, XElement criteriaCollectionElement) { IEnumerable <XElement> criterionElements = criteriaCollectionElement.Descendants(Elements.Criterion); foreach (XElement criterionElement in criterionElements) { TId id = TId.Parse(criterionElement.Attribute(Attributes.Id).Value); string name = criterionElement.Attribute(Attributes.Name).Value; string variableIdentifier = criterionElement.Attribute(Attributes.VariableIdentifier).Value; AdequacyCriterionType criterionType = EnumEx.Parse <AdequacyCriterionType>(criterionElement.Attribute(Attributes.Type).Value); AdequacyCriterion criterion = new AdequacyCriterion(id, name, variableIdentifier, criterionType); ReadPropertyCollection(criterion.Properties, ReadCollectionElement(Elements.Properties, criterionElement, false)); criteriaCollection.Add(criterion); } }
protected override void UpdateParametersDataGrid <T>(NamedModelEntityCollection <T> entities) { base.UpdateParametersDataGrid <T>(entities); this.dgvModelEntities.SuspendLayout(); int ind = 0; foreach (KeyValuePair <TId, opt.DataModel.Constraint> constraint in ModelStorage.Instance.Model.FunctionalConstraints) { this.dgvModelEntities[3, ind].Value = RelationManager. GetRelationName(constraint.Value.ConstraintRelation); this.dgvModelEntities[4, ind].Value = constraint.Value.Value; ind++; } this.dgvModelEntities.ResumeLayout(); }
public Population GenerateInitialPopulation(uint size, NamedModelEntityCollection <Parameter> attributes) { if (attributes == null) { throw new ArgumentNullException("attributes"); } Population result = new Population(); for (uint i = 0; i < size; i++) { Individual individual = new Individual(result.GetFreeConsequentId(), 0); GenerateAttributeValues(individual, attributes); result.Add(individual); } return(result); }
private static void CopyEntities <TEntity>(IEnumerable <TEntity> source, NamedModelEntityCollection <TEntity> destination) where TEntity : NamedModelEntity { if (source == null) { throw new ArgumentNullException("source"); } if (destination == null) { throw new ArgumentNullException("destination"); } destination.Clear(); foreach (TEntity entity in source) { destination.Add(entity); } }
/// <summary> /// Reads a collection of <see cref="Parameter"/> from XML /// </summary> /// <param name="parametersCollection">A collection to be read from XML</param> /// <param name="parametersCollectionElement"><see cref="XElement"/> to read a collection from</param> /// <param name="parameterElementName">Name of the XML element that represents <see cref="Parameter"/></param> private static void ReadParameters(NamedModelEntityCollection <Parameter> parametersCollection, XElement parametersCollectionElement, XName parameterElementName) { IEnumerable <XElement> parameterElements = parametersCollectionElement.Descendants(parameterElementName); foreach (XElement parameterElement in parameterElements) { TId id = TId.Parse(parameterElement.Attribute(Attributes.Id).Value); string name = parameterElement.Attribute(Attributes.Name).Value; string variableIdentifier = parameterElement.Attribute(Attributes.VariableIdentifier).Value; double minValue = ConvertEx.ToDoubleInvariant(parameterElement.Attribute(Attributes.MinValue).Value); double maxValue = ConvertEx.ToDoubleInvariant(parameterElement.Attribute(Attributes.MaxValue).Value); Parameter parameter = new Parameter(id, name, variableIdentifier, minValue, maxValue); ReadPropertyCollection(parameter.Properties, ReadCollectionElement(Elements.Properties, parameterElement, false)); parametersCollection.Add(parameter); } }
/// <summary> /// Reads a collection of <see cref="Constraint"/> from XML /// </summary> /// <param name="constraintsCollection">A collection to be read from XML</param> /// <param name="constraintsCollectionElement"><see cref="XElement"/> to read a collection from</param> private static void ReadConstraints(NamedModelEntityCollection <Constraint> constraintsCollection, XElement constraintsCollectionElement) { IEnumerable <XElement> constraintElements = constraintsCollectionElement.Descendants(Elements.FunctionalConstraint); foreach (XElement constraintElement in constraintElements) { TId id = TId.Parse(constraintElement.Attribute(Attributes.Id).Value); string name = constraintElement.Attribute(Attributes.Name).Value; string variableIdentifier = constraintElement.Attribute(Attributes.VariableIdentifier).Value; Relation constraintRelation = EnumEx.Parse <Relation>(constraintElement.Attribute(Attributes.Relation).Value); double value = ConvertEx.ToDoubleInvariant(constraintElement.Attribute(Attributes.Value).Value); string expression = constraintElement.Attribute(Attributes.Expression).Value; Constraint constraint = new Constraint(id, name, variableIdentifier, constraintRelation, value, expression); ReadPropertyCollection(constraint.Properties, ReadCollectionElement(Elements.Properties, constraintElement, false)); constraintsCollection.Add(constraint); } }
/// <summary> /// Writes a collection of <see cref="AdequacyCriterion"/> to XML /// </summary> /// <param name="criterionCollection">A collection to be written to XML</param> /// <param name="modelDocumentRoot"><see cref="XElement"/> instance that represents root of model XML document</param> private static void WriteCriteria(NamedModelEntityCollection <AdequacyCriterion> criterionCollection, XElement modelDocumentRoot) { XElement criteriaCollectionElement = new XElement(Elements.Criteria); foreach (AdequacyCriterion criterion in criterionCollection.Values) { XElement criterionElement = new XElement(Elements.Criterion, new XAttribute(Attributes.Id, criterion.Id.ToString()), new XAttribute(Attributes.Name, criterion.Name), new XAttribute(Attributes.VariableIdentifier, criterion.VariableIdentifier), new XAttribute(Attributes.Type, criterion.AdequacyType.ToString()) ); WritePropertyCollection(criterion.Properties, criterionElement); criteriaCollectionElement.Add(criterionElement); } modelDocumentRoot.Add(criteriaCollectionElement); }
/// <summary> /// Writes a collection of <see cref="Parameter"/> to XML /// </summary> /// <param name="parameterCollection">A collection to be written to XML</param> /// <param name="modelDocumentRoot"><see cref="XElement"/> instance that represents root of model XML document</param> /// <param name="collectionName">Name of the parameter collection</param> /// <param name="elementName">Name of the parameter element</param> private static void WriteParameters(NamedModelEntityCollection <Parameter> parameterCollection, XElement modelDocumentRoot, XName collectionName, XName elementName) { XElement parametersCollectionElement = new XElement(collectionName); foreach (Parameter parameter in parameterCollection.Values) { XElement parameterElement = new XElement(elementName, new XAttribute(Attributes.Id, parameter.Id.ToString()), new XAttribute(Attributes.Name, parameter.Name), new XAttribute(Attributes.VariableIdentifier, parameter.VariableIdentifier), new XAttribute(Attributes.MinValue, parameter.MinValue.ToStringInvariant()), new XAttribute(Attributes.MaxValue, parameter.MaxValue.ToStringInvariant()) ); WritePropertyCollection(parameter.Properties, parameterElement); parametersCollectionElement.Add(parameterElement); } modelDocumentRoot.Add(parametersCollectionElement); }
/// <summary> /// Copies references to <see cref="Parameter"/> instances from <paramref name="source"/> collection to the /// <paramref name="destination"/> collection, optionally - with IDs shift /// </summary> /// <param name="source">Source collection (copy from)</param> /// <param name="destination">Destination collection (copy to)</param> /// <param name="shiftIds">If True, method will create a deep copy of each <see cref="Parameter"/> instance /// and assign it new free ID from <paramref name="destination"/> collection</param> /// <returns>If <paramref name="shiftIds"/> is True, method will return a map of IDs: key is an ID of /// <see cref="Parameter"/> instance in <paramref name="source"/> and value is this instance's ID in the /// <paramref name="destination"/></returns> private static Dictionary <TId, TId> CopyParameters(NamedModelEntityCollection <Parameter> source, NamedModelEntityCollection <Parameter> destination, bool shiftIds = false) { Dictionary <TId, TId> idMap = new Dictionary <TId, TId>(source.Count); foreach (Parameter parameter in source.Values) { Parameter toAdd = parameter; if (shiftIds) { TId newId = destination.GetFreeConsequentId(); // Here we are leaving Custom Properties behind, but that should be fine for now toAdd = new Parameter(newId, parameter.Name, parameter.VariableIdentifier, parameter.MinValue, parameter.MaxValue); idMap.Add(parameter.Id, newId); } destination.Add(toAdd); } return(idMap); }
/// <summary> /// Deletes selected row /// </summary> /// <typeparam name="T">inheritors of NamedModelEntity</typeparam> /// <param name="entities">collection of model which will be shown in the DataGrid</param> protected void DeleteEntities <T>(NamedModelEntityCollection <T> entities) where T : NamedModelEntity { if (this.dgvModelEntities.SelectedRows.Count == 0) { MessageBoxHelper.ShowExclamation("Выберите в списке хотя бы один элемент для удаления"); return; } DialogResult result = MessageBox.Show("Удалить выбранные элементы?", Program.ApplicationSettings.ApplicationName, MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.Yes) { foreach (DataGridViewRow selRow in this.dgvModelEntities.SelectedRows) { TId entityId = TId.Parse((string)selRow.Cells[0].Value); entities.Remove(entityId); } this.UpdateParametersDataGrid(entities); } }
/// <summary> /// Writes a collection of <see cref="Constraint"/> to XML /// </summary> /// <param name="constraintCollection">A collection to be written to XML</param> /// <param name="modelDocumentRoot"><see cref="XElement"/> instance that represents root of model XML document</param> private static void WriteFunctionalConstraints(NamedModelEntityCollection <Constraint> constraintCollection, XElement modelDocumentRoot) { XElement constraintsCollectionElement = new XElement(Elements.FunctionalConstraints); foreach (Constraint constraint in constraintCollection.Values) { XElement constraintElement = new XElement(Elements.FunctionalConstraint, new XAttribute(Attributes.Id, constraint.Id.ToString()), new XAttribute(Attributes.Name, constraint.Name), new XAttribute(Attributes.VariableIdentifier, constraint.VariableIdentifier), new XAttribute(Attributes.Relation, constraint.ConstraintRelation.ToString()), new XAttribute(Attributes.Value, constraint.Value.ToStringInvariant()), new XAttribute(Attributes.Expression, constraint.Expression) ); WritePropertyCollection(constraint.Properties, constraintElement); constraintsCollectionElement.Add(constraintElement); } modelDocumentRoot.Add(constraintsCollectionElement); }
/// <summary> /// Initializes new instance of <see cref="ModelDraft"/> /// </summary> public ModelDraft() { PromotableConstants = new NamedModelEntityCollection <PromotableConstant>(); PromotableCriteria = new NamedModelEntityCollection <PromotableCriterion>(); }