Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/08/09 10:15:33 (15 years ago)
Author:
gkronber
Message:

Worked on CEDMA importer. #719

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/CedmaImporter/Importer.cs

    r2259 r2260  
    66using HeuristicLab.Modeling.Database;
    77using HeuristicLab.Modeling.Database.SQLServerCompact;
     8using HeuristicLab.GP;
     9using HeuristicLab.GP.Interfaces;
     10using HeuristicLab.GP.StructureIdentification;
     11using System.Diagnostics;
    812
    913namespace CedmaImporter {
     
    3337      using (StreamReader reader = File.OpenText(fileName)) {
    3438        ReadResultsAndInputVariables(reader);
    35         ImportAllModels(reader, database);
     39        ImportAllModels(dirName, reader, database);
    3640      }
    3741    }
     
    5660    }
    5761
    58     private void ImportAllModels(StreamReader reader, DatabaseService database) {
     62    private void ImportAllModels(string dirName, StreamReader reader, DatabaseService database) {
    5963      while (!reader.EndOfStream) {
    6064        string modelLine = reader.ReadLine();
    6165        string[] modelData = modelLine.Split(';');
    6266        int id = int.Parse(modelData[ID_COLUMN]);
    63         string fileName = modelData[FILENAME_COLUMN];
    64         string targetVariableName = modelData[TARGETVARIABLE_COLUMN];
    65         string algoName = modelData[ALGORITHM_COLUMN];
    66         Variable targetVariable = new Variable(targetVariableName);
     67        string targetVariableName = modelData[TARGETVARIABLE_COLUMN].Trim();
     68        string algoName = modelData[ALGORITHM_COLUMN].Trim();
     69        HeuristicLab.Core.IItem modelItem = ParseModel(dirName, modelData[FILENAME_COLUMN].Trim(), algoName);
     70        HeuristicLab.Modeling.Database.SQLServerCompact.Variable targetVariable = new HeuristicLab.Modeling.Database.SQLServerCompact.Variable(targetVariableName);
    6771        Algorithm algorithm = new Algorithm(algoName);
    6872        Model model = new Model(targetVariable, algorithm);
     
    8387        //foreach (InputVariableResult inputVariableResult in inputVariableResults)
    8488        //  database.Persist(inputVariableResult);
    85        
     89
    8690      }
    8791    }
     
    9195      return from i in Enumerable.Range(0, inputVariables.Count())
    9296             where inputVariables[i] != null && results[i] != null && double.TryParse(modelData[i], out temp)
    93              select new InputVariableResult(new InputVariable(model, new Variable(inputVariables[i])), results[i], double.Parse(modelData[i]));
     97             select new InputVariableResult(new InputVariable(model, new HeuristicLab.Modeling.Database.SQLServerCompact.Variable(inputVariables[i])), results[i], double.Parse(modelData[i]));
    9498    }
    9599
     
    99103             select new ModelResult(model, results[i], double.Parse(modelData[i]));
    100104    }
     105
     106    Dictionary<string, IFunction> knownFunctions = new Dictionary<string, IFunction>() {
     107    {"+", new Addition()},
     108    {"and", new And()},
     109    {"mean", new Average()},
     110    {"cos", new Cosinus()},
     111    {"/", new Division()},
     112    {"equ", new Equal()},
     113    {"exp", new Exponential()},
     114    {">", new GreaterThan()},
     115    {"if", new IfThenElse()},
     116    {"<", new LessThan()},
     117    {"log", new Logarithm()},
     118    {"*", new Multiplication()},
     119    {"not", new Not()},
     120    {"or", new Or()},
     121    {"expt", new Power()},
     122    {"sign", new Signum()},
     123    {"sin",new Sinus()},
     124    {"sqrt", new Sqrt()},
     125    {"-", new Subtraction()},
     126    {"tan", new Tangens()},
     127    {"xor", new Xor()}
     128    };
     129    Constant constant = new Constant();
     130    HeuristicLab.GP.StructureIdentification.Variable variable = new HeuristicLab.GP.StructureIdentification.Variable();
     131    Differential differential = new Differential();
     132
     133    private HeuristicLab.Core.IItem ParseModel(string dirName, string modelFileName, string algoName) {
     134      if (algoName == "SupportVectorRegression") {
     135        HeuristicLab.Data.SVMModel model = new HeuristicLab.Data.SVMModel();
     136        model.Model = SVM.Model.Read(Path.Combine(dirName, modelFileName) + ".svm.model.txt");
     137        model.RangeTransform = SVM.RangeTransform.Read(Path.Combine(dirName, modelFileName) + ".svm.transform.txt");
     138        return model;
     139      } else {
     140        GeneticProgrammingModel model = new GeneticProgrammingModel();
     141        IEnumerable<Token> tokens = GetTokenStream(File.OpenText(Path.Combine(dirName, modelFileName) + ".gp.txt"));
     142        model.FunctionTree = ParseSexp(new Queue<Token>(tokens));
     143        return model;
     144      }
     145    }
     146
     147    private IEnumerable<Token> GetTokenStream(StreamReader reader) {
     148      return from line in GetLineStream(reader)
     149             let strTokens = line.Split(new string[] { " ", "\t", Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).AsEnumerable()
     150             from strToken in strTokens
     151             let t = Token.Parse(strToken)
     152             where t != null
     153             select t;
     154    }
     155
     156    private IEnumerable<string> GetLineStream(StreamReader reader) {
     157      while (!reader.EndOfStream) yield return reader.ReadLine().Replace("(", " ( ").Replace(")", " ) ");
     158      yield break;
     159    }
     160
     161    private HeuristicLab.GP.Interfaces.IFunctionTree ParseSexp(Queue<Token> tokens) {
     162      Expect(Token.LPAR, tokens);
     163
     164      if (tokens.Peek().Symbol == TokenSymbol.SYMB) {
     165        if (tokens.Peek().StringValue.Equals("variable")) {
     166          return ParseVariable(tokens);
     167        } else if (tokens.Peek().StringValue.Equals("differential")) {
     168          return ParseDifferential(tokens);
     169        } else {
     170          Token curToken = tokens.Dequeue();
     171          IFunctionTree tree = CreateTree(curToken);
     172          while (!tokens.Peek().Equals(Token.RPAR)) {
     173            tree.AddSubTree(ParseSexp(tokens));
     174          }
     175          Expect(Token.RPAR, tokens);
     176          return tree;
     177        }
     178      } else if (tokens.Peek().Symbol == TokenSymbol.NUMBER) {
     179        ConstantFunctionTree t = (ConstantFunctionTree)constant.GetTreeNode();
     180        t.Value = tokens.Dequeue().DoubleValue;
     181        return t;
     182      } else {
     183        throw new FormatException("Expected function or constant symbol");
     184      }
     185    }
     186
     187    private IFunctionTree ParseDifferential(Queue<Token> tokens) {
     188      Debug.Assert(tokens.Dequeue().StringValue == "differential");
     189      VariableFunctionTree t = (VariableFunctionTree)differential.GetTreeNode();
     190      t.Weight = tokens.Dequeue().DoubleValue;
     191      t.VariableName = tokens.Dequeue().StringValue;
     192      t.SampleOffset = (int)tokens.Dequeue().DoubleValue;
     193      Expect(Token.RPAR, tokens);
     194      return t;
     195    }
     196
     197    private IFunctionTree ParseVariable(Queue<Token> tokens) {
     198      Debug.Assert(tokens.Dequeue().StringValue == "variable");
     199      VariableFunctionTree t = (VariableFunctionTree)variable.GetTreeNode();
     200      t.Weight = tokens.Dequeue().DoubleValue;
     201      t.VariableName = tokens.Dequeue().StringValue;
     202      t.SampleOffset = (int)tokens.Dequeue().DoubleValue;
     203      Expect(Token.RPAR, tokens);
     204      return t;
     205    }
     206
     207    private IFunctionTree CreateTree(Token token) {
     208      if (token.Symbol != TokenSymbol.SYMB) throw new FormatException("Expected function symbol, but got: " + token.StringValue);
     209      return knownFunctions[token.StringValue].GetTreeNode();
     210    }
     211
     212    private void Expect(Token token, Queue<Token> tokens) {
     213      Token cur = tokens.Dequeue();
     214      if (!token.Equals(cur)) throw new FormatException("Expected: " + token.StringValue + ", but got found: " + cur.StringValue);
     215    }
     216
     217    private enum TokenSymbol { LPAR, RPAR, SYMB, NUMBER };
     218    private class Token {
     219      public static readonly Token LPAR = Token.Parse("(");
     220      public static readonly Token RPAR = Token.Parse(")");
     221
     222      public TokenSymbol Symbol { get; set; }
     223      public string StringValue { get; set; }
     224      public double DoubleValue { get; set; }
     225      public Token() { }
     226
     227      public override bool Equals(object obj) {
     228        Token other = (obj as Token);
     229        if (other == null) return false;
     230        if (other.Symbol != Symbol) return false;
     231        return other.StringValue == this.StringValue;
     232      }
     233
     234      public static Token Parse(string strToken) {
     235        strToken = strToken.Trim();
     236        Token t = new Token();
     237        t.StringValue = strToken.Trim();
     238        double temp;
     239        if (strToken == "") {
     240          t = null;
     241        } else if (strToken == "(") {
     242          t.Symbol = TokenSymbol.LPAR;
     243        } else if (strToken == ")") {
     244          t.Symbol = TokenSymbol.RPAR;
     245        } else if (double.TryParse(strToken, out temp)) {
     246          t.Symbol = TokenSymbol.NUMBER;
     247          t.DoubleValue = double.Parse(strToken);
     248        } else {
     249          t.Symbol = TokenSymbol.SYMB;
     250        }
     251        return t;
     252      }
     253    }
    101254  }
    102255}
Note: See TracChangeset for help on using the changeset viewer.