public double CalculateDistance(Data.Instance instance1, Data.Instance instance2) { double distance = 0; int attributeCount = instance1.Values.Length;; for (int attributeIndex = 0; attributeIndex < instance1.Values.Length; attributeIndex++) { if (instance1.Metadata.Attributes[attributeIndex] is NominalAttribute) { distance += (instance1[attributeIndex] == instance2[attributeIndex]) ? 0 : 1; } else { distance += Math.Pow(instance1[attributeIndex] - instance2[attributeIndex], this._order); } } distance = Math.Pow(distance, 1.0 / (double)this._order); //double maxDistance = Math.Pow(attributeCount, 1.0 / (double)this._order); //double similarity = (maxDistance - distance) / distance; //return similarity; return(distance); }
public double CalculateDistance(Data.Instance instance1, Data.Instance instance2, double[][] weights) { double distance = 0; int classIndex = instance2.Label; int attributeCount = instance1.Values.Length;; for (int attributeIndex = 0; attributeIndex < attributeCount; attributeIndex++) { if (instance1.Metadata.Attributes[attributeIndex] is NominalAttribute) { distance += (instance1[attributeIndex] == instance2[attributeIndex]) ? 0 : weights[classIndex][attributeIndex]; } else { distance += Math.Pow((instance1[attributeIndex] - instance2[attributeIndex]), this._order) * weights[classIndex][attributeIndex]; } } distance = Math.Pow(distance, 1.0 / (double)this._order); //double maxDistance = Math.Pow(attributeCount, 1.0 / (double)this._order); //double similarity = (maxDistance - distance) / maxDistance; //return similarity; //distance = Math.Pow(distance, 1.0 / (double)this._order) / attributeCount; //double similarity = 1.0 / (1.0 + distance); //return similarity; return(distance); }
private static Data.Instance ParseinstanceLine(int instanceIndex, string line, Data.Dataset dataset) { string[] parts = line.Split(','); List <double> values = new List <double>(); int label = dataset.Metadata.Target.GetIndex(parts[parts.Length - 1]); for (int index = 0; index < parts.Length - 1; index++) { if (dataset.Metadata.Attributes[index] is Data.NominalAttribute) { Data.NominalAttribute attribute = dataset.Metadata.Attributes[index] as Data.NominalAttribute; string value = parts[index]; values.Add(attribute.GetIndex(value)); } else { double value = double.NaN; if (parts[index] != "?") { value = double.Parse(parts[index]); } values.Add(value); } } Data.Instance instance = new Data.Instance(dataset.Metadata, instanceIndex, values.ToArray(), label); return(instance); }
public override Model.Prediction Classify(Data.Instance instance) { double[] classCounts = new double[this._database.Metadata.Target.Values.Length]; double[] distances = new double[this._database.Size]; int[] instanceIndexes = new int[this._database.Size]; for (int instanceIndex = 0; instanceIndex < this._database.Size; instanceIndex++) { if (this._database[instanceIndex] == instance) { continue; } instanceIndexes[instanceIndex] = instanceIndex; double distance = this._distanceMeasure.CalculateDistance(instance, this._database[instanceIndex], this._classBasedWeights); if (distance == 0) { distances[instanceIndex] = distance; } distances[instanceIndex] = distance; } Array.Sort(distances, instanceIndexes); int max = 0; for (int counter = 0; counter < this._k; counter++) { int instanceIndex = instanceIndexes[counter]; int predicted = this._database[instanceIndex].Label; if (this._useWeightedVote) { classCounts[predicted] += (1.0 / 1.0 + distances[instanceIndex]); } else { classCounts[predicted] += 1; } if (classCounts[predicted] > classCounts[max]) { max = predicted; } } double sum = classCounts.Sum(); for (int i = 0; i < classCounts.Length; i++) { classCounts[i] /= sum; } Prediction prediction = new Prediction(max, classCounts); return(prediction); }
public override Model.Prediction Classify(Data.Instance instance) { int[] classCount = new int[this._database.Metadata.Attributes.Length]; double [] classSimilarity = new double[this._database.Metadata.Attributes.Length]; for (int instanceIndex = 0; instanceIndex < this._database.Size; instanceIndex++) { if (this._database[instanceIndex] == instance) { continue; } Instance instancer = this._database[instanceIndex]; double distance = this._distanceMeasure.CalculateDistance(instance, instancer, this._classBasedWeights); double similartiy = double.PositiveInfinity; if (distance > 0) { similartiy = 1 / distance; } if (similartiy > this.SimilarityThreshold) { classCount[instancer.Label] += 1; classSimilarity[instancer.Label] += similartiy; } } int max = 0; for (int i = 0; i < classCount.Length; i++) { //if (classCount[i] != 0) // classSimilarity[i] /= classCount[i]; if (classSimilarity[i] > classSimilarity[max]) { max = i; } } double sum = classSimilarity.Sum(); for (int i = 0; i < classSimilarity.Length; i++) { classSimilarity[i] /= sum; } Prediction prediction = new Prediction(max, classSimilarity); return(prediction); }
public override Prediction Classify(Data.Instance instance) { List <int> prediceted = new List <int>(); Queue <Node> candidates = new Queue <Node>(); foreach (string child in this._hierarchy.Root.Children) { candidates.Enqueue(this._hierarchy[child]); } while (candidates.Count != 0) { Node current = candidates.Dequeue(); IClassifier classifier = this[current.Name]; Prediction prediction = classifier.Classify(instance); if (prediction.Label == 0) { prediceted.Add(current.ValueIndex); if (current.Children != null) { foreach (string child in current.Children) { candidates.Enqueue(this._hierarchy[child]); } } } } double[] probabilities = new double[instance.Metadata.Target.Values.Length]; foreach (int index in prediceted) { probabilities[index] = 1; } Prediction final = new Prediction(-1, probabilities); return(final); }
private static Data.Instance ParseHierarchicalinstanceLine(int instanceIndex, string line, Data.Dataset dataset, bool skipFirstAttribute) { string[] parts = line.Split(','); List <double> values = new List <double>(); List <int> label = new List <int>(); string[] labelParts = parts[parts.Length - 1].Split(';'); foreach (string target in labelParts) { if (!string.IsNullOrWhiteSpace(target)) { label.Add(dataset.Metadata.Target.GetIndex(target)); } } for (int index = 0; index < parts.Length - 1; index++) { if (dataset.Metadata.Attributes[index] is Data.NominalAttribute) { Data.NominalAttribute attribute = dataset.Metadata.Attributes[index] as Data.NominalAttribute; string value = parts[index]; values.Add(attribute.GetIndex(value)); } else { double value = double.NaN; if (parts[index] != "?") { value = double.Parse(parts[index]); } values.Add(value); } } Data.Instance instance = new Data.Instance(dataset.Metadata, instanceIndex, values.ToArray(), label); return(instance); }
public static Data.Dataset LoadHierarchicalDatasetFromTxt(string filePath, bool skipfirstAttribute) { StreamReader reader = new StreamReader(filePath); string datasetName = null; Data.Metadata metadata = null; Data.Dataset dataset = null; List <Data.Attribute> attributes = new List <Data.Attribute>(); List <Data.Instance> instances = new List <Data.Instance>(); List <Data.Node> nodes = new List <Data.Node>(); int attributeIndex = 0; int instanceIndex = 0; string mode = "start"; while (!reader.EndOfStream) { string line = reader.ReadLine(); if (!string.IsNullOrEmpty(line)) { if (line.Contains("%")) { continue; } if (line.Contains("@relation")) { datasetName = line.Substring(line.IndexOf(' ')).Trim(); datasetName = datasetName.Contains("-weka") ? datasetName.Substring(0, datasetName.IndexOf("-weka")) : datasetName; } else if (line.Contains("@attribute")) { Data.Attribute attribute = ParseAttributeLine(line, attributeIndex); if (attribute != null) { attributeIndex++; attributes.Add(attribute); } } else if (line.Contains("@ontology")) { mode = "ontolog"; } else if (line.Contains("@data")) { List <string> classValues = new List <string>(); int counter = 0; for (int i = 1; i < nodes.Count; i++) { Data.Node node = nodes[i]; node.ValueIndex = counter; classValues.Add(node.Name); counter++; } Data.ClassHierarchy classHierarchy = new Data.ClassHierarchy(nodes.ToArray()); Data.HierarchicalAttribute target = new Data.HierarchicalAttribute("class", attributes.Count, classValues.ToArray(), classHierarchy); metadata = new Data.Metadata(datasetName, attributes.ToArray(), target, true); dataset = new Data.Dataset(metadata); mode = "data"; } else { if (mode == "ontolog") { Data.Node node = ParseOntologyLine(line); if (!nodes.Exists(n => n.Name == node.Name)) { nodes.Add(node); } } else { Data.Instance instance = ParseHierarchicalinstanceLine(instanceIndex, line, dataset, skipfirstAttribute); instanceIndex++; instances.Add(instance); } } } } dataset.SetInstances(instances.ToArray()); return(dataset); }
public static Data.Dataset LoadDatasetFromArff(string filePath) { StreamReader reader = new StreamReader(filePath); string datasetName = null; Data.Metadata metadata = null; Data.Dataset dataset = null; List <Data.Attribute> attributes = new List <Data.Attribute>(); List <Data.Instance> instances = new List <Data.Instance>(); int attributeIndex = 0; int instanceIndex = 0; List <string> instanceLines = new List <string>(); while (!reader.EndOfStream) { string line = reader.ReadLine(); if (!string.IsNullOrEmpty(line)) { if (line.Contains("%")) { continue; } if (line.Contains("@relation")) { datasetName = line.Substring(line.IndexOf(' ')).Trim(); datasetName = datasetName.Contains("-weka") ? datasetName.Substring(0, datasetName.IndexOf("-weka")) : datasetName; } else if (line.Contains("@attribute")) { Data.Attribute attribute = ParseAttributeLine(line, attributeIndex); if (attribute != null) { attributeIndex++; attributes.Add(attribute); } } else if (line.Contains("@data")) { int targetIndex = attributes.FindIndex(m => string.Compare(m.Name, "Class", true) == 0); Data.NominalAttribute target = attributes[targetIndex] as Data.NominalAttribute; attributes.RemoveAt(target.Index); metadata = new Data.Metadata(datasetName, attributes.ToArray(), target, false); dataset = new Data.Dataset(metadata); } else { if (instanceLines.Contains(line)) { continue; } Data.Instance instance = ParseinstanceLine(instanceIndex, line, dataset); instanceIndex++; instances.Add(instance); instanceLines.Add(line); } } } dataset.SetInstances(instances.ToArray()); return(dataset); }
private void InitNewPlayer() { CriticalChance = 0.1; CurrentLevel = new Data.Instance(); PlayerLevel = 1; Id = 1; IsGood = true; MainShieldId = 1; MainWeaponId = 1; Strenght = 5; Gold = 0; Accuracy = 5; SecondaryWeaponId = 1; TilesRef = "playerChar"; Defence = 5; HealingCooldown = 0; UnspentSkillPoints = 0; Exp = new Exp { Current = 0, Max = 100 }; Abilities = CreateAbilities(); Shields = new List<Shield>(); Weapons = CreateBasicWeapons(); EquipedWeapon = Weapons.First(); switch (CharacterClass) { case CharacterClass.Wizard: CurrentHP = 120; MaxHp = 120; Healing = 10; break; case CharacterClass.Hunter: CurrentHP = 180; MaxHp = 180; Healing = 5; Accuracy = 10; break; case CharacterClass.Warrior: break; default: break; } }
public abstract Prediction Classify(Data.Instance instance);