Changeset 2165
- Timestamp:
- 07/16/09 11:34:22 (15 years ago)
- Location:
- trunk/sources
- Files:
-
- 15 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.GP.Boolean/3.3/FunctionLibraryInjector.cs
r1529 r2165 32 32 public class FunctionLibraryInjector : OperatorBase { 33 33 private const string TARGETVARIABLE = "TargetVariable"; 34 private const string ALLOWEDFEATURES = "AllowedFeatures";35 34 private const string OPERATORLIBRARY = "FunctionLibrary"; 36 35 … … 45 44 : base() { 46 45 AddVariableInfo(new VariableInfo(TARGETVARIABLE, "The target variable", typeof(IntData), VariableKind.In)); 47 AddVariableInfo(new VariableInfo(ALLOWEDFEATURES, "List of indexes of allowed features", typeof(ItemList<IntData>), VariableKind.In));48 46 AddVariableInfo(new VariableInfo(OPERATORLIBRARY, "Preconfigured default operator library", typeof(GPOperatorLibrary), VariableKind.New)); 49 47 } 50 48 51 49 public override IOperation Apply(IScope scope) { 52 ItemList<IntData> allowedFeatures = GetVariableValue<ItemList<IntData>>(ALLOWEDFEATURES, scope, true);53 50 int targetVariable = GetVariableValue<IntData>(TARGETVARIABLE, scope, true).Data; 54 51 55 // remove the target-variable in case it occures in allowed features56 List<IntData> ts = allowedFeatures.FindAll(d => d.Data == targetVariable);57 foreach (IntData t in ts) allowedFeatures.Remove(t);58 59 52 InitDefaultOperatorLibrary(); 60 61 int[] allowedIndexes = new int[allowedFeatures.Count];62 for (int i = 0; i < allowedIndexes.Length; i++) {63 allowedIndexes[i] = allowedFeatures[i].Data;64 }65 66 variable.SetConstraints(allowedIndexes);67 53 68 54 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName(OPERATORLIBRARY), operatorLibrary)); -
trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Evaluators/VariableEvaluationImpactCalculator.cs
r2043 r2165 41 41 42 42 43 protected override double[] GetOutputs(IScope scope, Dataset dataset, int targetVariable, ItemList<IntData> allowedFeatures,int start, int end) {43 protected override double[] GetOutputs(IScope scope, Dataset dataset, int targetVariable, int start, int end) { 44 44 ITreeEvaluator evaluator = GetVariableValue<ITreeEvaluator>("TreeEvaluator", scope, true); 45 45 IFunctionTree tree = GetVariableValue<IFunctionTree>("FunctionTree", scope, true); -
trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Evaluators/VariableQualityImpactCalculator.cs
r2136 r2165 40 40 } 41 41 42 protected override double CalculateQuality(IScope scope, Dataset dataset, int targetVariable, ItemList<IntData> allowedFeatures,int start, int end) {42 protected override double CalculateQuality(IScope scope, Dataset dataset, int targetVariable, int start, int end) { 43 43 ITreeEvaluator evaluator = GetVariableValue<ITreeEvaluator>("TreeEvaluator", scope, true); 44 44 IFunctionTree tree = GetVariableValue<IFunctionTree>("FunctionTree", scope, true); -
trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/FunctionLibraryInjector.cs
r2130 r2165 32 32 namespace HeuristicLab.GP.StructureIdentification { 33 33 public class FunctionLibraryInjector : OperatorBase { 34 private const string NUMBEROFINPUTVARIABLES = "NumberOfInputVariables"; 34 35 private const string FUNCTIONLIBRARY = "FunctionLibrary"; 35 36 private const string TARGETVARIABLE = "TargetVariable"; 36 private const string ALLOWEDFEATURES = "AllowedFeatures";37 37 private const string MINTIMEOFFSET = "MinTimeOffset"; 38 38 private const string MAXTIMEOFFSET = "MaxTimeOffset"; … … 70 70 public FunctionLibraryInjector() 71 71 : base() { 72 AddVariableInfo(new VariableInfo(NUMBEROFINPUTVARIABLES, "The number of available input variables", typeof(IntData), VariableKind.In)); 72 73 AddVariableInfo(new VariableInfo(TARGETVARIABLE, "The target variable", typeof(IntData), VariableKind.In)); 73 AddVariableInfo(new VariableInfo(ALLOWEDFEATURES, "List of indexes of allowed features", typeof(ItemList<IntData>), VariableKind.In)); 74 AddVariableInfo(new Core.VariableInfo(MINTIMEOFFSET, "Minimal time offset for all features", typeof(IntData), Core.VariableKind.In)); 75 AddVariableInfo(new Core.VariableInfo(MAXTIMEOFFSET, "Maximal time offset for all feature", typeof(IntData), Core.VariableKind.In)); 74 AddVariableInfo(new VariableInfo(MINTIMEOFFSET, "Minimal time offset for all features", typeof(IntData), VariableKind.In)); 75 AddVariableInfo(new VariableInfo(MAXTIMEOFFSET, "Maximal time offset for all feature", typeof(IntData), VariableKind.In)); 76 76 AddVariableInfo(new VariableInfo(FUNCTIONLIBRARY, "Preconfigured default operator library", typeof(GPOperatorLibrary), VariableKind.New)); 77 77 … … 111 111 StructId.Variable variable; 112 112 GPOperatorLibrary operatorLibrary; 113 114 ItemList<IntData> allowedFeatures = GetVariableValue<ItemList<IntData>>(ALLOWEDFEATURES, scope, true); 113 int nInputVariables = GetVariableValue<IntData>(NUMBEROFINPUTVARIABLES, scope, true).Data; 115 114 int targetVariable = GetVariableValue<IntData>(TARGETVARIABLE, scope, true).Data; 116 115 … … 226 225 ConditionalAddOperator(XOR_ALLOWED, operatorLibrary, xor); 227 226 228 int[] allowedIndexes = new int[allowedFeatures.Count]; 229 for (int i = 0; i < allowedIndexes.Length; i++) { 230 allowedIndexes[i] = allowedFeatures[i].Data; 231 } 232 233 variable.SetConstraints(allowedIndexes, minTimeOffset, maxTimeOffset); 234 differential.SetConstraints(allowedIndexes, minTimeOffset, maxTimeOffset); 227 variable.SetConstraints(1, nInputVariables, minTimeOffset, maxTimeOffset); 228 differential.SetConstraints(1, nInputVariables, minTimeOffset, maxTimeOffset); 235 229 236 230 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName(FUNCTIONLIBRARY), operatorLibrary)); -
trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Variable.cs
r1618 r2165 154 154 } 155 155 156 public void SetConstraints(int [] allowedIndexes, int minSampleOffset, int maxSampleOffset) {156 public void SetConstraints(int minInputIndex, int maxInputIndex, int minSampleOffset, int maxSampleOffset) { 157 157 ConstrainedIntData offset = GetVariableValue<ConstrainedIntData>(OFFSET, null, false); 158 IntBoundedConstraint rangeConstraint = new IntBoundedConstraint();158 IntBoundedConstraint offsetConstraint = new IntBoundedConstraint(); 159 159 this.minOffset = minSampleOffset; 160 160 this.maxOffset = maxSampleOffset; 161 rangeConstraint.LowerBound = minSampleOffset;162 rangeConstraint.LowerBoundEnabled = true;163 rangeConstraint.LowerBoundIncluded = true;164 rangeConstraint.UpperBound = maxSampleOffset;165 rangeConstraint.UpperBoundEnabled = true;166 rangeConstraint.UpperBoundIncluded = true;167 offset.AddConstraint( rangeConstraint);161 offsetConstraint.LowerBound = minSampleOffset; 162 offsetConstraint.LowerBoundEnabled = true; 163 offsetConstraint.LowerBoundIncluded = true; 164 offsetConstraint.UpperBound = maxSampleOffset; 165 offsetConstraint.UpperBoundEnabled = true; 166 offsetConstraint.UpperBoundIncluded = true; 167 offset.AddConstraint(offsetConstraint); 168 168 169 169 ConstrainedIntData index = GetVariableValue<ConstrainedIntData>(INDEX, null, false); 170 Array.Sort(allowedIndexes); 171 minIndex = allowedIndexes[0]; maxIndex = allowedIndexes[allowedIndexes.Length - 1]; 172 List<IConstraint> constraints = new List<IConstraint>(); 173 int start = allowedIndexes[0]; 174 int prev = start; 175 for(int i = 1; i < allowedIndexes.Length; i++) { 176 if(allowedIndexes[i] != prev + 1) { 177 IntBoundedConstraint lastRange = new IntBoundedConstraint(); 178 lastRange.LowerBound = start; 179 lastRange.LowerBoundEnabled = true; 180 lastRange.LowerBoundIncluded = true; 181 lastRange.UpperBound = prev; 182 lastRange.UpperBoundEnabled = true; 183 lastRange.UpperBoundIncluded = true; 184 constraints.Add(lastRange); 185 start = allowedIndexes[i]; 186 prev = start; 187 } 188 prev = allowedIndexes[i]; 189 } 190 IntBoundedConstraint range = new IntBoundedConstraint(); 191 range.LowerBound = start; 192 range.LowerBoundEnabled = true; 193 range.LowerBoundIncluded = true; 194 range.UpperBound = prev; 195 range.UpperBoundEnabled = true; 196 range.UpperBoundIncluded = true; 197 constraints.Add(range); 198 if(constraints.Count > 1) { 199 OrConstraint or = new OrConstraint(); 200 foreach(IConstraint c in constraints) or.Clauses.Add(c); 201 index.AddConstraint(or); 202 } else { 203 index.AddConstraint(constraints[0]); 204 } 170 IntBoundedConstraint indexConstraint = new IntBoundedConstraint(); 171 minIndex = minInputIndex; 172 maxIndex = maxInputIndex; 173 indexConstraint.LowerBound = minInputIndex; 174 indexConstraint.LowerBoundEnabled = true; 175 indexConstraint.LowerBoundIncluded = true; 176 indexConstraint.UpperBound = maxInputIndex; 177 indexConstraint.UpperBoundEnabled = true; 178 indexConstraint.UpperBoundIncluded = true; 179 index.AddConstraint(indexConstraint); 205 180 206 181 SetupInitialization(); -
trunk/sources/HeuristicLab.LinearRegression/3.2/LinearRegressionOperator.cs
r2154 r2165 38 38 AddVariableInfo(new VariableInfo("SamplesStart", "Start index of samples in dataset to evaluate", typeof(IntData), VariableKind.In)); 39 39 AddVariableInfo(new VariableInfo("SamplesEnd", "End index of samples in dataset to evaluate", typeof(IntData), VariableKind.In)); 40 AddVariableInfo(new VariableInfo("AllowedFeatures", "List of indexes of allowed features", typeof(ItemList<IntData>), VariableKind.In));41 40 AddVariableInfo(new VariableInfo("LinearRegressionModel", "Formula that was calculated by linear regression", typeof(IFunctionTree), VariableKind.Out | VariableKind.New)); 42 41 AddVariableInfo(new VariableInfo("TreeSize", "The size (number of nodes) of the tree", typeof(IntData), VariableKind.New | VariableKind.Out)); … … 49 48 int start = GetVariableValue<IntData>("SamplesStart", scope, true).Data; 50 49 int end = GetVariableValue<IntData>("SamplesEnd", scope, true).Data; 51 ItemList<IntData> allowedFeatures = GetVariableValue<ItemList<IntData>>("AllowedFeatures", scope, true);52 List<int> allowed Rows = CalculateAllowedRows(dataset, allowedFeatures, targetVariable, start, end);50 List<int> allowedRows = CalculateAllowedRows(dataset, targetVariable, start, end); 51 List<int> allowedColumns = CalculateAllowedColumns(dataset, targetVariable, start, end); 53 52 54 List<IntData> disallowedFeatures = new List<IntData>(); 55 foreach (IntData allowedFeature in allowedFeatures) { 56 if (IsAlmost(dataset.GetMinimum(allowedFeature.Data, start, end), 0.0) && 57 IsAlmost(dataset.GetMaximum(allowedFeature.Data, start, end), 0.0)) 58 disallowedFeatures.Add(allowedFeature); 59 } 60 foreach (IntData disallowedFeature in disallowedFeatures) 61 allowedFeatures.Remove(disallowedFeature); 62 63 double[,] inputMatrix = PrepareInputMatrix(dataset, allowedFeatures, allowedRows); 53 double[,] inputMatrix = PrepareInputMatrix(dataset, allowedColumns, allowedRows); 64 54 double[] targetVector = PrepareTargetVector(dataset, targetVariable, allowedRows); 65 55 double[] coefficients = CalculateCoefficients(inputMatrix, targetVector); 66 IFunctionTree tree = CreateModel(coefficients, allowed Features);56 IFunctionTree tree = CreateModel(coefficients, allowedColumns); 67 57 68 58 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("LinearRegressionModel"), tree)); … … 76 66 } 77 67 78 private IFunctionTree CreateModel(double[] coefficients, ItemList<IntData> allowedFeatures) {68 private IFunctionTree CreateModel(double[] coefficients, List<int> allowedColumns) { 79 69 IFunctionTree root = new Addition().GetTreeNode(); 80 70 IFunctionTree actNode = root; … … 84 74 for (int i = 0; i < coefficients.Length - 1; i++) { 85 75 v = new GP.StructureIdentification.Variable(); 86 v.GetVariable(GP.StructureIdentification.Variable.INDEX).Value = new ConstrainedIntData(allowed Features[i].Data);76 v.GetVariable(GP.StructureIdentification.Variable.INDEX).Value = new ConstrainedIntData(allowedColumns[i]); 87 77 v.GetVariable(GP.StructureIdentification.Variable.WEIGHT).Value = new ConstrainedDoubleData(coefficients[i]); 88 78 v.GetVariable(GP.StructureIdentification.Variable.OFFSET).Value = new ConstrainedIntData(0); … … 90 80 } 91 81 GP.StructureIdentification.Constant c = new Constant(); 92 c.GetVariable(GP.StructureIdentification.Constant.VALUE).Value = new ConstrainedDoubleData(coefficients[coefficients.Length - 1] * 1.0);82 c.GetVariable(GP.StructureIdentification.Constant.VALUE).Value = new ConstrainedDoubleData(coefficients[coefficients.Length - 1]); 93 83 nodes.Enqueue(c.GetTreeNode()); 94 84 … … 107 97 double[] weights = new double[targetVector.Length]; 108 98 double[] coefficients = new double[inputMatrix.GetLength(1)]; 109 for (int i=0;i<weights.Length;i++) weights[i] = 1.0;99 for (int i = 0; i < weights.Length; i++) weights[i] = 1.0; 110 100 // call external ALGLIB solver 111 101 leastsquares.buildgeneralleastsquares(ref targetVector, ref weights, ref inputMatrix, inputMatrix.GetLength(0), inputMatrix.GetLength(1), ref coefficients); … … 115 105 116 106 //returns list of valid row indexes (rows without NaN values) 117 private List<int> CalculateAllowedRows(Dataset dataset, ItemList<IntData> allowedFeatures,int targetVariable, int start, int end) {107 private List<int> CalculateAllowedRows(Dataset dataset, int targetVariable, int start, int end) { 118 108 List<int> allowedRows = new List<int>(); 119 109 bool add; 120 110 for (int row = start; row < end; row++) { 121 111 add = true; 122 for (int col = 0; col < allowedFeatures.Count&& add == true; col++) {123 if (double.IsNaN(dataset.GetValue(row, allowedFeatures[col].Data)) ||112 for (int col = 0; col < dataset.Columns && add == true; col++) { 113 if (double.IsNaN(dataset.GetValue(row, col)) || 124 114 double.IsNaN(dataset.GetValue(row, targetVariable))) 125 115 add = false; … … 132 122 } 133 123 134 private double[,] PrepareInputMatrix(Dataset dataset, ItemList<IntData> allowedFeatures, List<int> allowedRows) { 124 //returns list of valid column indexes (columns which contain at least one non-zero value) 125 private List<int> CalculateAllowedColumns(Dataset dataset, int targetVariable, int start, int end) { 126 List<int> allowedColumns = new List<int>(); 127 for (int i = 0; i < dataset.Columns; i++) { 128 if (i == targetVariable) continue; 129 if (!IsAlmost(dataset.GetMinimum(i, start, end), 0.0) || 130 !IsAlmost(dataset.GetMaximum(i, start, end), 0.0)) 131 allowedColumns.Add(i); 132 } 133 return allowedColumns; 134 } 135 136 private double[,] PrepareInputMatrix(Dataset dataset, List<int> allowedColumns, List<int> allowedRows) { 135 137 int rowCount = allowedRows.Count; 136 double[,] matrix = new double[rowCount, allowed Features.Count + 1];137 for (int col = 0; col < allowed Features.Count; col++) {138 double[,] matrix = new double[rowCount, allowedColumns.Count + 1]; 139 for (int col = 0; col < allowedColumns.Count; col++) { 138 140 for (int row = 0; row < allowedRows.Count; row++) 139 matrix[row, col] = dataset.GetValue(allowedRows[row], allowed Features[col].Data);141 matrix[row, col] = dataset.GetValue(allowedRows[row], allowedColumns[col]); 140 142 } 141 143 //add constant 1.0 in last column 142 144 for (int i = 0; i < rowCount; i++) 143 matrix[i, allowed Features.Count] = constant;145 matrix[i, allowedColumns.Count] = constant; 144 146 return matrix; 145 147 } -
trunk/sources/HeuristicLab.Modeling/3.2/ProblemInjector.cs
r2162 r2165 77 77 78 78 AddVariableInfo(new VariableInfo("MaxNumberOfTrainingSamples", "Maximal number of training samples to use (optional)", typeof(IntData), VariableKind.In)); 79 AddVariableInfo(new VariableInfo("NumberOfInputVariables", "The number of available input variables", typeof(IntData), VariableKind.New)); 79 80 } 80 81 … … 97 98 Dataset scopeDataset = CreateNewDataset(operatorDataset, targetVariable, operatorAllowedFeatures); 98 99 99 ItemList<IntData> allowedFeatures = new ItemList<IntData>();100 allowedFeatures.AddRange(Enumerable.Range(1, scopeDataset.Columns -1 ).Select(x=>new IntData(x)));101 102 100 scope.AddVariable(new Variable("Dataset", scopeDataset)); 103 scope.AddVariable(new Variable("AllowedFeatures", allowedFeatures));104 101 scope.AddVariable(new Variable("TargetVariable", new IntData(0))); 102 scope.AddVariable(new Variable("NumberOfInputVariables", new IntData(scopeDataset.Columns - 1))); 105 103 106 104 int trainingStart = GetVariableValue<IntData>("TrainingSamplesStart", scope, true).Data; -
trunk/sources/HeuristicLab.Modeling/3.2/VariableEvaluationImpactCalculator.cs
r2136 r2165 58 58 } 59 59 60 protected override double[] CalculateValue(IScope scope, Dataset dataset, int targetVariable, ItemList<IntData> allowedFeatures,int start, int end) {61 return GetOutputs(scope, dataset, targetVariable, allowedFeatures,start, end);60 protected override double[] CalculateValue(IScope scope, Dataset dataset, int targetVariable, int start, int end) { 61 return GetOutputs(scope, dataset, targetVariable, start, end); 62 62 } 63 63 … … 75 75 } 76 76 77 protected abstract double[] GetOutputs(IScope scope, Dataset dataset, int targetVariable, ItemList<IntData> allowedFeatures,int start, int end);77 protected abstract double[] GetOutputs(IScope scope, Dataset dataset, int targetVariable, int start, int end); 78 78 } 79 79 } -
trunk/sources/HeuristicLab.Modeling/3.2/VariableImpactCalculatorBase.cs
r2043 r2165 34 34 35 35 public override string Description { 36 get { return @"Calculates the impact of all allowedinput variables on the model."; }36 get { return @"Calculates the impact of all input variables on the model."; } 37 37 } 38 38 … … 47 47 AddVariableInfo(new VariableInfo("Dataset", "Dataset", typeof(Dataset), VariableKind.In)); 48 48 AddVariableInfo(new VariableInfo("TargetVariable", "TargetVariable", typeof(IntData), VariableKind.In)); 49 AddVariableInfo(new VariableInfo("AllowedFeatures", "Indexes of allowed input variables", typeof(ItemList<IntData>), VariableKind.In));50 49 AddVariableInfo(new VariableInfo("TrainingSamplesStart", "TrainingSamplesStart", typeof(IntData), VariableKind.In)); 51 50 AddVariableInfo(new VariableInfo("TrainingSamplesEnd", "TrainingSamplesEnd", typeof(IntData), VariableKind.In)); … … 54 53 55 54 public override IOperation Apply(IScope scope) { 56 ItemList<IntData> allowedFeatures = GetVariableValue<ItemList<IntData>>("AllowedFeatures", scope, true);57 55 int targetVariable = GetVariableValue<IntData>("TargetVariable", scope, true).Data; 58 56 Dataset dataset = GetVariableValue<Dataset>("Dataset", scope, true); … … 61 59 int end = GetVariableValue<IntData>("TrainingSamplesEnd", scope, true).Data; 62 60 63 T referenceValue = CalculateValue(scope, dataset, targetVariable, allowedFeatures,start, end);64 double[] impacts = new double[ allowedFeatures.Count];61 T referenceValue = CalculateValue(scope, dataset, targetVariable, start, end); 62 double[] impacts = new double[dataset.Columns]; 65 63 66 for (int i = 0; i < allowedFeatures.Count && !abortRequested; i++) { 67 int currentVariable = allowedFeatures[i].Data; 68 var oldValues = ReplaceVariableValues(dirtyDataset, currentVariable, CalculateNewValues(dirtyDataset, currentVariable, start, end), start, end); 69 T newValue = CalculateValue(scope, dirtyDataset, targetVariable, allowedFeatures, start, end); 70 impacts[i] = CalculateImpact(referenceValue, newValue); 71 ReplaceVariableValues(dirtyDataset, currentVariable, oldValues, start, end); 64 for (int i = 0; i < impacts.Length && !abortRequested; i++) { 65 int currentVariable = i; 66 if (currentVariable != targetVariable) { 67 var oldValues = ReplaceVariableValues(dirtyDataset, currentVariable, CalculateNewValues(dirtyDataset, currentVariable, start, end), start, end); 68 T newValue = CalculateValue(scope, dirtyDataset, targetVariable, start, end); 69 impacts[i] = CalculateImpact(referenceValue, newValue); 70 ReplaceVariableValues(dirtyDataset, currentVariable, oldValues, start, end); 71 } 72 72 } 73 73 … … 76 76 77 77 ItemList variableImpacts = new ItemList(); 78 for (int i = 0; i < allowedFeatures.Count; i++) { 79 int currentVariable = allowedFeatures[i].Data; 80 ItemList row = new ItemList(); 81 row.Add(new StringData(dataset.GetVariableName(currentVariable))); 82 row.Add(new DoubleData(impacts[i])); 83 variableImpacts.Add(row); 78 for (int i = 0; i < impacts.Length; i++) { 79 int currentVariable = i; 80 if (currentVariable != targetVariable) { 81 ItemList row = new ItemList(); 82 row.Add(new StringData(dataset.GetVariableName(currentVariable))); 83 row.Add(new DoubleData(impacts[i])); 84 variableImpacts.Add(row); 85 } 84 86 } 85 87 … … 91 93 } 92 94 93 protected abstract T CalculateValue(IScope scope, Dataset dataset, int targetVariable, ItemList<IntData> allowedFeatures,int start, int end);95 protected abstract T CalculateValue(IScope scope, Dataset dataset, int targetVariable, int start, int end); 94 96 95 97 protected abstract double CalculateImpact(T referenceValue, T newValue); -
trunk/sources/HeuristicLab.Modeling/3.2/VariableQualityImpactCalculator.cs
r2043 r2165 43 43 } 44 44 45 protected override double CalculateValue(IScope scope, Dataset dataset, int targetVariable, ItemList<IntData> allowedFeatures,int start, int end) {46 return CalculateQuality(scope, dataset, targetVariable, allowedFeatures,start, end);45 protected override double CalculateValue(IScope scope, Dataset dataset, int targetVariable, int start, int end) { 46 return CalculateQuality(scope, dataset, targetVariable, start, end); 47 47 } 48 48 49 protected abstract double CalculateQuality(IScope scope, Dataset dataset, int targetVariable, ItemList<IntData> allowedFeatures,int start, int end);49 protected abstract double CalculateQuality(IScope scope, Dataset dataset, int targetVariable, int start, int end); 50 50 } 51 51 } -
trunk/sources/HeuristicLab.SupportVectorMachines/3.2/SVMHelper.cs
r2148 r2165 9 9 namespace HeuristicLab.SupportVectorMachines { 10 10 public class SVMHelper { 11 public static SVM.Problem CreateSVMProblem(Dataset dataset, ItemList<IntData> allowedFeatures,int targetVariable, int start, int end) {11 public static SVM.Problem CreateSVMProblem(Dataset dataset, int targetVariable, int start, int end) { 12 12 int rowCount = end - start; 13 double[] samples = dataset.Samples; 13 List<int> skippedFeatures = new List<int>(); 14 for (int i = 0; i < dataset.Columns; i++) { 15 if (i != targetVariable) { 16 if (dataset.GetRange(i, start, end) == 0) 17 skippedFeatures.Add(i); 18 } 19 } 14 20 15 List<int> skippedFeatures = new List<int>(); 16 for (int i = 0; i < allowedFeatures.Count; i++) { 17 if (dataset.GetRange(allowedFeatures[i].Data, start, end) == 0) 18 skippedFeatures.Add(i); 19 } 21 int maxColumns = dataset.Columns - skippedFeatures.Count(); 20 22 21 23 double[] targetVector = new double[rowCount]; 22 24 for (int i = 0; i < rowCount; i++) { 23 double value = samples[(start + i) * dataset.Columns + targetVariable];25 double value = dataset.GetValue(start + i, targetVariable); 24 26 targetVector[i] = value; 25 27 } … … 31 33 for (int row = 0; row < rowCount; row++) { 32 34 tempRow = new List<SVM.Node>(); 33 for (int col = 0; col < allowedFeatures.Count; col++) {34 if (!skippedFeatures.Contains(col) ) {35 double value = samples[(start + row) * dataset.Columns + allowedFeatures[col].Data];35 for (int col = 0; col < dataset.Columns; col++) { 36 if (!skippedFeatures.Contains(col) && col!=targetVariable) { 37 double value = dataset.GetValue(start + row, col); 36 38 if (!double.IsNaN(value)) 37 tempRow.Add(new SVM.Node( allowedFeatures[col].Data, value));39 tempRow.Add(new SVM.Node(col, value)); 38 40 } 39 41 } 40 if (!double.IsNaN( samples[(start + row) * dataset.Columns + targetVariable])) {42 if (!double.IsNaN(dataset.GetValue(start + row, targetVariable))) { 41 43 nodes[addedRows] = tempRow.ToArray(); 42 44 addedRows++; … … 44 46 } 45 47 46 return new SVM.Problem(targetVector.Length, targetVector, nodes, allowedFeatures.Max(x => x.Data));48 return new SVM.Problem(targetVector.Length, targetVector, nodes, maxColumns); 47 49 } 48 50 } -
trunk/sources/HeuristicLab.SupportVectorMachines/3.2/SupportVectorCreator.cs
r2163 r2165 39 39 //Dataset infos 40 40 AddVariableInfo(new VariableInfo("Dataset", "Dataset with all samples on which to apply the function", typeof(Dataset), VariableKind.In)); 41 AddVariableInfo(new VariableInfo("AllowedFeatures", "List of indexes of allowed features", typeof(ItemList<IntData>), VariableKind.In));42 41 AddVariableInfo(new VariableInfo("TargetVariable", "Index of the column of the dataset that holds the target variable", typeof(IntData), VariableKind.In)); 43 42 AddVariableInfo(new VariableInfo("SamplesStart", "Start index of samples in dataset to evaluate", typeof(IntData), VariableKind.In)); … … 67 66 abortRequested = false; 68 67 Dataset dataset = GetVariableValue<Dataset>("Dataset", scope, true); 69 ItemList<IntData> allowedFeatures = GetVariableValue<ItemList<IntData>>("AllowedFeatures", scope, true);70 68 int targetVariable = GetVariableValue<IntData>("TargetVariable", scope, true).Data; 71 69 int start = GetVariableValue<IntData>("SamplesStart", scope, true).Data; … … 83 81 parameter.Gamma = GetVariableValue<DoubleData>("SVMGamma", scope, true).Data; 84 82 85 SVM.Problem problem = SVMHelper.CreateSVMProblem(dataset, allowedFeatures,targetVariable, start, end);83 SVM.Problem problem = SVMHelper.CreateSVMProblem(dataset, targetVariable, start, end); 86 84 SVM.RangeTransform rangeTransform = SVM.Scaling.DetermineRange(problem); 87 85 SVM.Problem scaledProblem = SVM.Scaling.Scale(problem, rangeTransform); -
trunk/sources/HeuristicLab.SupportVectorMachines/3.2/SupportVectorEvaluator.cs
r2148 r2165 35 35 //Dataset infos 36 36 AddVariableInfo(new VariableInfo("Dataset", "Dataset with all samples on which to apply the function", typeof(Dataset), VariableKind.In)); 37 AddVariableInfo(new VariableInfo("AllowedFeatures", "List of indexes of allowed features", typeof(ItemList<IntData>), VariableKind.In));38 37 AddVariableInfo(new VariableInfo("TargetVariable", "Index of the column of the dataset that holds the target variable", typeof(IntData), VariableKind.In)); 39 38 AddVariableInfo(new VariableInfo("SamplesStart", "Start index of samples in dataset to evaluate", typeof(IntData), VariableKind.In)); … … 47 46 public override IOperation Apply(IScope scope) { 48 47 Dataset dataset = GetVariableValue<Dataset>("Dataset", scope, true); 49 ItemList<IntData> allowedFeatures = GetVariableValue<ItemList<IntData>>("AllowedFeatures", scope, true);50 48 int targetVariable = GetVariableValue<IntData>("TargetVariable", scope, true).Data; 51 49 int start = GetVariableValue<IntData>("SamplesStart", scope, true).Data; … … 53 51 54 52 SVMModel modelData = GetVariableValue<SVMModel>("SVMModel", scope, true); 55 SVM.Problem problem = SVMHelper.CreateSVMProblem(dataset, allowedFeatures,targetVariable, start, end);53 SVM.Problem problem = SVMHelper.CreateSVMProblem(dataset, targetVariable, start, end); 56 54 SVM.Problem scaledProblem = SVM.Scaling.Scale(problem, modelData.RangeTransform); 57 55 -
trunk/sources/HeuristicLab.SupportVectorMachines/3.2/VariableEvaluationImpactCalculator.cs
r2043 r2165 38 38 39 39 40 protected override double[] GetOutputs(IScope scope, Dataset dataset, int targetVariable, ItemList<IntData> allowedFeatures,int start, int end) {40 protected override double[] GetOutputs(IScope scope, Dataset dataset, int targetVariable, int start, int end) { 41 41 SVMModel model = GetVariableValue<SVMModel>("SVMModel", scope, true); 42 SVM.Problem problem = SVMHelper.CreateSVMProblem(dataset, allowedFeatures,targetVariable, start, end);42 SVM.Problem problem = SVMHelper.CreateSVMProblem(dataset, targetVariable, start, end); 43 43 SVM.Problem scaledProblem = SVM.Scaling.Scale(problem, model.RangeTransform); 44 44 -
trunk/sources/HeuristicLab.SupportVectorMachines/3.2/VariableQualityImpactCalculator.cs
r2136 r2165 37 37 } 38 38 39 protected override double CalculateQuality(IScope scope, Dataset dataset, int targetVariable, ItemList<IntData> allowedFeatures,int start, int end) {39 protected override double CalculateQuality(IScope scope, Dataset dataset, int targetVariable, int start, int end) { 40 40 SVMModel model = GetVariableValue<SVMModel>("SVMModel", scope, true); 41 SVM.Problem problem = SVMHelper.CreateSVMProblem(dataset, allowedFeatures,targetVariable, start, end);41 SVM.Problem problem = SVMHelper.CreateSVMProblem(dataset, targetVariable, start, end); 42 42 SVM.Problem scaledProblem = SVM.Scaling.Scale(problem, model.RangeTransform); 43 43
Note: See TracChangeset
for help on using the changeset viewer.