#region License Information
/* HeuristicLab
* Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
using static HeuristicLab.Problems.DataAnalysis.Symbolic.TreeToAutoDiffTermConverter;
namespace HeuristicLab.Problems.DataAnalysis.Symbolic.ConstantsOptimization {
public static class Util {
public static double[,] ExtractData(IDataset dataset, IEnumerable variables, IEnumerable rows) {
var x = new double[rows.Count(), variables.Count()];
int row = 0;
foreach (var r in rows) {
int col = 0;
foreach (var variable in variables) {
if (dataset.VariableHasType(variable.variableName)) {
x[row, col] = dataset.GetDoubleValue(variable.variableName, r+variable.lag);
} else if (dataset.VariableHasType(variable.variableName)) {
x[row, col] = dataset.GetStringValue(variable.variableName, r) == variable.variableValue ? 1 : 0;
} else throw new InvalidProgramException("found a variable of unknown type");
col++;
}
row++;
}
return x;
}
public static Dictionary ExtractParameters(IDataset dataset) {
var parameters = new Dictionary();
foreach(var doubleVariable in dataset.DoubleVariables) {
var data = new DataForVariable(doubleVariable, string.Empty, 0);
var param = new AutoDiff.Variable();
parameters.Add(data,param);
}
foreach(var stringVariable in dataset.StringVariables) {
foreach(var stringValue in dataset.GetStringValues(stringVariable).Distinct()) {
var data = new DataForVariable(stringVariable, stringVariable, 0);
var param = new AutoDiff.Variable();
parameters.Add(data, param);
}
}
return parameters;
}
public static void UpdateConstants(ISymbolicExpressionTree tree, double[] constants) {
int i = 0;
foreach (var node in tree.Root.IterateNodesPrefix().OfType()) {
ConstantTreeNode constantTreeNode = node as ConstantTreeNode;
VariableTreeNodeBase variableTreeNodeBase = node as VariableTreeNodeBase;
FactorVariableTreeNode factorVarTreeNode = node as FactorVariableTreeNode;
if (constantTreeNode != null)
constantTreeNode.Value = constants[i++];
else if (variableTreeNodeBase != null)
variableTreeNodeBase.Weight = constants[i++];
else if (factorVarTreeNode != null) {
for (int j = 0; j < factorVarTreeNode.Weights.Length; j++)
factorVarTreeNode.Weights[j] = constants[i++];
}
}
}
}
}