public void LoadFromXml(XDocument xmlReader) { float minimalSalienceThreshold; var propertiesElement = xmlReader.Root.Element("properties"); normativeMatchDistance = float.Parse(propertiesElement.Element("normative_match_distance").Value.ToString(), CultureInfo.InvariantCulture); distanceConstraint = float.Parse(propertiesElement.Element("distance_constraint").Value.ToString(), CultureInfo.InvariantCulture); comparativeFitAlfa = float.Parse(propertiesElement.Element("comparative_fit_alfa").Value.ToString(), CultureInfo.InvariantCulture); comparativeFitBeta = float.Parse(propertiesElement.Element("comparative_fit_beta").Value.ToString(), CultureInfo.InvariantCulture); minimalSalienceThreshold = float.Parse(propertiesElement.Element("minimal_salience_threshold").Value.ToString(), CultureInfo.InvariantCulture); // agents = new List <Agent>(); foreach (var element in xmlReader.Root.Element("agents").Elements("agent")) { var agent = new Agent(); agent.characteristics = new Dictionary <Characteristic, float>(); agent.knowledgeBase = new List <SocialGroup>(); agent.minimalSalienceThreshold = minimalSalienceThreshold; agent.name = element.Attribute("name").Value; foreach (var subElement in element.Elements("characteristic")) { var characteristic = new Characteristic(); characteristic.name = subElement.Attribute("name").Value; agent.characteristics.Add(characteristic, float.Parse(subElement.Value, CultureInfo.InvariantCulture)); } foreach (var subElement in element.Element("knowledge_base").Elements("social_group")) { var socialGroup = new SocialGroup(); socialGroup.characteristics = new Dictionary <Characteristic, float>(); socialGroup.name = subElement.Attribute("name").Value; foreach (var subEubElement in subElement.Elements("characteristic")) { var characteristic = new Characteristic(); characteristic.name = subEubElement.Attribute("name").Value; socialGroup.characteristics.Add(characteristic, float.Parse(subEubElement.Value, CultureInfo.InvariantCulture)); } socialGroup.CalculateAccessibility(agent); agent.knowledgeBase.Add(socialGroup); } // agent.NormalizeAccessibility(); agents.Add(agent); } }
public void CalculateNormativeFit(Context currentContext, Simulation simulation) { int numDimensions = currentContext.relevantCharacteristcs.Count; for (int c = 0; c < clusterMeans.Count; c++) { var clusterMean = clusterMeans[c]; float bestDistance = float.MaxValue; foreach (var socialGroup in knowledgeBase) { bool validGroup = true; float distance = 0.0f; for (int i = 0; i < numDimensions; i++) { float groupValue = 0.0f; if (!socialGroup.characteristics.TryGetValue(currentContext.relevantCharacteristcs[i], out groupValue)) { validGroup = false; break; } float weight = currentContext.relevantCharacteristcs[i].weight; float difference = (clusterMean.mean[i] - groupValue) * weight; distance += difference * difference; } if (!validGroup) { continue; } distance = (float)Math.Sqrt((double)distance); if (distance < bestDistance) { bestDistance = distance; clusterMean.matchedGroup = socialGroup; } } // ad-hoc group if (bestDistance > simulation.normativeMatchDistance * currentContext.GetMaxDistance()) { numAdhocGroups++; SocialGroup adhocGroup = new SocialGroup(); adhocGroup.isAdHoc = true; adhocGroup.characteristics = new Dictionary <Characteristic, float>(); adhocGroup.name = "Group " + numAdhocGroups; for (int i = 0; i < numDimensions; i++) { adhocGroup.characteristics.Add(currentContext.relevantCharacteristcs[i], clusterMean.mean[i]); } adhocGroup.CalculateAccessibility(this); knowledgeBase.Add(adhocGroup); clusterMean.matchedGroup = adhocGroup; } clusterMeans[c] = clusterMean; } }