<commit_before>#include "Huffman.h"
#include "Node.h"
#include <iostream>
#include <fstream>
#include <list>
#include <algorithm>
#include <unordered_map>

namespace Codecs {

  bool comp(Node* l, Node* r) {
    return l->getFrequency() > r->getFrequency();
  }

  void HuffmanCodec::encode(string& encoded, const string_view& raw) const {
    unsigned char buf = 0;
    size_t count = 0;
    for (size_t i = 0; i < raw.size(); ++i) {
      unsigned char c = raw[i];
      auto it = table.find(c);
      size_t size_of_path = it->second.size();
      size_t j = 0;
      while (j < size_of_path) {
        while (j < size_of_path && count < CHAR_SIZE) {
          buf = buf | ((it->second)[j] << (CHAR_SIZE - count - 1));
          ++j;
          ++count;
        }
        if (CHAR_SIZE == count) {
          count = 0;
          encoded.push_back(buf);
          buf = 0;
        }
      }
    }
    if (count != 0) {
      encoded.push_back(buf);
    }
    encoded.push_back(static_cast<unsigned char>(count));
  }

  void HuffmanCodec::decode(string& raw, const string_view& encoded) const {
    int64_t count = 0;
    Node* cur = root_for_decode;
    unsigned char byte = encoded[0];
    int64_t encoded_size = static_cast<int64_t>(encoded.size()) - 2;
    int64_t i = 0;

    //till almost the last element
    while (i < encoded_size) {
      for (size_t j = 0; j < CHAR_SIZE; ++j) {
        if (byte & (1 << (CHAR_SIZE - j - 1))) {
          cur = cur->right;
        } else {
          cur = cur->left;
        }
        if (cur->left == cur->right) { //is equal to cur->left == nullptr == cur->right
          raw.push_back(cur->getData());
          cur = root_for_decode;
        }
      }
      ++i;
      byte = encoded[i];
    }

    //last element
    while (i != encoded_size + 1) {
      if (byte & (1 << (CHAR_SIZE - count - 1))) {
        cur = cur->right;
      } else {
        cur = cur->left;
      }
      if (cur->left == cur->right) {
        raw.push_back(cur->getData());
        cur = root_for_decode;
      }
      ++count;
      if (count == CHAR_SIZE || encoded[i + 1] == count) {
        count = 0;
        ++i;
        byte = encoded[i];
      }
    }

  }

  string HuffmanCodec::save() const {
    std::ofstream output("config");
    for (auto it = chars.begin(); it != chars.end(); ++it) {
      output << static_cast<uint16_t>(it->first) << " " << it->second << std::endl;
    }
    output.close();
    return string();
  }

  void HuffmanCodec::Build_table(Node* root_for_table, std::vector<bool>& code) {
    if (root_for_table->left != nullptr) {
      code.push_back(false);
      Build_table(root_for_table->left, code);
    }

    if (root_for_table->right != nullptr) {
      code.push_back(true);
      Build_table(root_for_table->right, code);
    }

    if (root_for_table->left == nullptr && root_for_table->left == nullptr) {
      table.insert({root_for_table->getData(), code});
    }

    code.pop_back();
  }

  void HuffmanCodec::load(const string_view& config) {
    std::ifstream input(config.to_string());
    for (size_t i = 0; i < (1 << CHAR_SIZE); ++i) {
      uint32_t sym;
      uint64_t cnt;
      input >> sym >> cnt;
      chars.insert({static_cast<unsigned char>(sym), cnt});
    }

    std::vector<Node*> table_cur;
    for (auto it = chars.begin(); it != chars.end(); ++it) {
      Node *p = new Node(it->first, it->second);
      table_cur.push_back(p);
    }

    while (table_cur.size() != 1) {
      std::stable_sort(table_cur.begin(), table_cur.end(), comp);
      Node *left_son = table_cur.back();
      table_cur.pop_back();
      Node *right_son = table_cur.back();
      table_cur.pop_back();
      Node *parent = new Node(left_son, right_son);
      table_cur.push_back(parent);
    }
    root_for_decode = table_cur.front();
    Node* root_for_table = root_for_decode;

    std::vector<bool> code;
    Build_table(root_for_table, code);
    code.clear();
  }

  size_t HuffmanCodec::sample_size(size_t records_total) const {
    return std::min(static_cast<size_t>(10000), records_total);
  }

  void HuffmanCodec::learn(const StringViewVector& sample) {
    size_t total_count = 0;

    for (int32_t c = 0; c < (1 << CHAR_SIZE); ++c) {
      chars.insert({static_cast<unsigned char>(c), 0});
    }

    for (auto& cur_string : sample) {
      total_count += cur_string.size();
      for (auto& one_char : cur_string) {
        auto iterator = chars.find(one_char);
        if (iterator != chars.end()) {
          ++(iterator->second);
        }
      }
    }
    std::vector<Node*> table_cur;
    for (auto it = chars.begin(); it != chars.end(); ++it) {
      Node *p = new Node(it->first, it->second);
      table_cur.push_back(p);
    }
    auto it = table_cur.begin();
    while (table_cur.size() != 1) {
      std::stable_sort(it, table_cur.end(), comp);
      Node *left_son = table_cur.back();
      table_cur.pop_back();
      Node *right_son = table_cur.back();
      table_cur.pop_back();
      Node *parent = new Node(left_son, right_son);
      table_cur.push_back(parent);
    }
    root_for_encode = table_cur.front();
    Node* root_for_table = root_for_encode;

    std::vector<bool> code;
    Build_table(root_for_table, code);
    code.clear();
  }

  void HuffmanCodec::reset() {
    table.clear();
    chars.clear();
  }
}  // namespace Codecs
<commit_msg>made a better readble code<commit_after>#include "Huffman.h"
#include "Node.h"
#include <iostream>
#include <fstream>
#include <list>
#include <algorithm>
#include <unordered_map>

namespace Codecs {

  bool comp(Node* l, Node* r) {
    return l->getFrequency() > r->getFrequency();
  }

  void HuffmanCodec::encode(string& encoded, const string_view& raw) const {
    unsigned char buf = 0;
    size_t count = 0;
    for (size_t i = 0; i < raw.size(); ++i) {
      unsigned char c = raw[i];
      auto it = table.find(c);
      size_t size_of_path = it->second.size();
      size_t j = 0;
      while (j < size_of_path) {
        while (j < size_of_path && count < CHAR_SIZE) {
          buf = buf | ((it->second)[j] << (CHAR_SIZE - count - 1));
          ++j;
          ++count;
        }
        if (CHAR_SIZE == count) {
          count = 0;
          encoded.push_back(buf);
          buf = 0;
        }
      }
    }
    if (count != 0) {
      encoded.push_back(buf);
    }
    encoded.push_back(static_cast<unsigned char>(count));
  }

  void HuffmanCodec::decode(string& raw, const string_view& encoded) const {
    int64_t count = 0;
    Node* cur = root_for_decode;
    unsigned char byte = encoded[0];
    int64_t encoded_size = static_cast<int64_t>(encoded.size()) - 2;

    //till almost the last element
    for (int64_t i = 0; i < encoded_size; ++i) {
      for (size_t j = 0; j < CHAR_SIZE; ++j) {
        if (byte & (1 << (CHAR_SIZE - j - 1))) {
          cur = cur->right;
        } else {
          cur = cur->left;
        }
        if (cur->left == cur->right) { //is equal to cur->left == nullptr == cur->right
          raw.push_back(cur->getData());
          cur = root_for_decode;
        }
      }
      byte = encoded[i + 1];
    }

    //last element
    while (true) {
      if (byte & (1 << (CHAR_SIZE - count - 1))) {
        cur = cur->right;
      } else {
        cur = cur->left;
      }
      if (cur->left == cur->right) {
        raw.push_back(cur->getData());
        cur = root_for_decode;
      }
      ++count;
      if (count == CHAR_SIZE || encoded[encoded_size + 1] == count) {
        count = 0;
        byte = encoded[encoded_size];
        break;
      }
    }

  }

  string HuffmanCodec::save() const {
    std::ofstream output("config");
    for (auto it = chars.begin(); it != chars.end(); ++it) {
      output << static_cast<uint16_t>(it->first) << " " << it->second << std::endl;
    }
    output.close();
    return string();
  }

  void HuffmanCodec::Build_table(Node* root_for_table, std::vector<bool>& code) {
    if (root_for_table->left != nullptr) {
      code.push_back(false);
      Build_table(root_for_table->left, code);
    }

    if (root_for_table->right != nullptr) {
      code.push_back(true);
      Build_table(root_for_table->right, code);
    }

    if (root_for_table->left == nullptr && root_for_table->left == nullptr) {
      table.insert({root_for_table->getData(), code});
    }

    code.pop_back();
  }

  void HuffmanCodec::load(const string_view& config) {
    std::ifstream input(config.to_string());
    for (size_t i = 0; i < (1 << CHAR_SIZE); ++i) {
      uint32_t sym;
      uint64_t cnt;
      input >> sym >> cnt;
      chars.insert({static_cast<unsigned char>(sym), cnt});
    }

    std::vector<Node*> table_cur;
    for (auto it = chars.begin(); it != chars.end(); ++it) {
      Node *p = new Node(it->first, it->second);
      table_cur.push_back(p);
    }

    while (table_cur.size() != 1) {
      std::stable_sort(table_cur.begin(), table_cur.end(), comp);
      Node *left_son = table_cur.back();
      table_cur.pop_back();
      Node *right_son = table_cur.back();
      table_cur.pop_back();
      Node *parent = new Node(left_son, right_son);
      table_cur.push_back(parent);
    }
    root_for_decode = table_cur.front();
    Node* root_for_table = root_for_decode;

    std::vector<bool> code;
    Build_table(root_for_table, code);
    code.clear();
  }

  size_t HuffmanCodec::sample_size(size_t records_total) const {
    return std::min(static_cast<size_t>(10000), records_total);
  }

  void HuffmanCodec::learn(const StringViewVector& sample) {
    size_t total_count = 0;

    for (int32_t c = 0; c < (1 << CHAR_SIZE); ++c) {
      chars.insert({static_cast<unsigned char>(c), 0});
    }

    for (auto& cur_string : sample) {
      total_count += cur_string.size();
      for (auto& one_char : cur_string) {
        auto iterator = chars.find(one_char);
        if (iterator != chars.end()) {
          ++(iterator->second);
        }
      }
    }
    std::vector<Node*> table_cur;
    for (auto it = chars.begin(); it != chars.end(); ++it) {
      Node *p = new Node(it->first, it->second);
      table_cur.push_back(p);
    }
    auto it = table_cur.begin();
    while (table_cur.size() != 1) {
      std::stable_sort(it, table_cur.end(), comp);
      Node *left_son = table_cur.back();
      table_cur.pop_back();
      Node *right_son = table_cur.back();
      table_cur.pop_back();
      Node *parent = new Node(left_son, right_son);
      table_cur.push_back(parent);
    }
    root_for_encode = table_cur.front();
    Node* root_for_table = root_for_encode;

    std::vector<bool> code;
    Build_table(root_for_table, code);
    code.clear();
  }

  void HuffmanCodec::reset() {
    table.clear();
    chars.clear();
  }
}  // namespace Codecs
<|endoftext|>


<commit_before>//===- FuzzerExtFunctionsDlsym.cpp - Interface to external functions ------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Implementation for operating systems that support dlsym(). We only use it on
// Apple platforms for now. We don't use this approach on Linux because it
// requires that clients of LibFuzzer pass ``--export-dynamic`` to the linker.
// That is a complication we don't wish to expose to clients right now.
//===----------------------------------------------------------------------===//
#include "FuzzerDefs.h"
#if LIBFUZZER_APPLE

#include "FuzzerExtFunctions.h"
#include <dlfcn.h>

using namespace fuzzer;

template <typename T>
static T GetFnPtr(const char *FnName, bool WarnIfMissing) {
  dlerror(); // Clear any previous errors.
  void *Fn = dlsym(RTLD_DEFAULT, FnName);
  if (Fn == nullptr) {
    if (WarnIfMissing) {
      const char *ErrorMsg = dlerror();
      Printf("WARNING: Failed to find function \"%s\".", FnName);
      if (ErrorMsg)
        Printf(" Reason %s.", ErrorMsg);
      Printf("\n");
    }
  }
  return reinterpret_cast<T>(Fn);
}

namespace fuzzer {

ExternalFunctions::ExternalFunctions() {
#define EXT_FUNC(NAME, RETURN_TYPE, FUNC_SIG, WARN)                            \
  this->NAME = GetFnPtr<decltype(ExternalFunctions::NAME)>(#NAME, WARN)

#include "FuzzerExtFunctions.def"

#undef EXT_FUNC
}
} // namespace fuzzer
#endif // LIBFUZZER_APPLE
<commit_msg>[libFuzzer] include FuzzerIO.h and hopefully fix the Mac build. reported by Dejan Mircevski<commit_after>//===- FuzzerExtFunctionsDlsym.cpp - Interface to external functions ------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Implementation for operating systems that support dlsym(). We only use it on
// Apple platforms for now. We don't use this approach on Linux because it
// requires that clients of LibFuzzer pass ``--export-dynamic`` to the linker.
// That is a complication we don't wish to expose to clients right now.
//===----------------------------------------------------------------------===//
#include "FuzzerDefs.h"
#if LIBFUZZER_APPLE

#include "FuzzerExtFunctions.h"
#include "FuzzerIO.h"
#include <dlfcn.h>

using namespace fuzzer;

template <typename T>
static T GetFnPtr(const char *FnName, bool WarnIfMissing) {
  dlerror(); // Clear any previous errors.
  void *Fn = dlsym(RTLD_DEFAULT, FnName);
  if (Fn == nullptr) {
    if (WarnIfMissing) {
      const char *ErrorMsg = dlerror();
      Printf("WARNING: Failed to find function \"%s\".", FnName);
      if (ErrorMsg)
        Printf(" Reason %s.", ErrorMsg);
      Printf("\n");
    }
  }
  return reinterpret_cast<T>(Fn);
}

namespace fuzzer {

ExternalFunctions::ExternalFunctions() {
#define EXT_FUNC(NAME, RETURN_TYPE, FUNC_SIG, WARN)                            \
  this->NAME = GetFnPtr<decltype(ExternalFunctions::NAME)>(#NAME, WARN)

#include "FuzzerExtFunctions.def"

#undef EXT_FUNC
}
} // namespace fuzzer
#endif // LIBFUZZER_APPLE
<|endoftext|>


<commit_before>//------------------------------------------------------------------------------
// CLING - the C++ LLVM-based InterpreterG :)
// author:  Vassil Vassilev <vasil.georgiev.vasilev@cern.ch>
//
// This file is dual-licensed: you can choose to license it under the University
// of Illinois Open Source License or the GNU Lesser General Public License. See
// LICENSE.TXT for details.
//------------------------------------------------------------------------------

#include "cling/Interpreter/ClangInternalState.h"

#include "clang/AST/ASTContext.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Basic/Builtins.h"
#include "clang/Basic/SourceManager.h"

#include "llvm/ADT/SmallString.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/Signals.h"

#include <cstdio>
#include <string>
#include <time.h>

#ifdef WIN32
#define popen _popen
#define pclose _pclose
#endif

using namespace clang;

namespace cling {

  ClangInternalState::ClangInternalState(ASTContext& AC, Preprocessor& PP,
                                         llvm::Module* M, const std::string& name)
    : m_ASTContext(AC), m_Preprocessor(PP), m_Module(M),
      m_DiffCommand("diff -u --text "), m_Name(name) {
    store();
  }

  ClangInternalState::~ClangInternalState() {
    // cleanup the temporary files:
    remove(m_LookupTablesFile.c_str());
    remove(m_IncludedFilesFile.c_str());
    remove(m_ASTFile.c_str());
    remove(m_LLVMModuleFile.c_str());
    remove(m_MacrosFile.c_str());
  }

  void ClangInternalState::store() {
    // Cannot use the stack (private copy ctor)
    llvm::OwningPtr<llvm::raw_fd_ostream> m_LookupTablesOS;
    llvm::OwningPtr<llvm::raw_fd_ostream> m_IncludedFilesOS;
    llvm::OwningPtr<llvm::raw_fd_ostream> m_ASTOS;
    llvm::OwningPtr<llvm::raw_fd_ostream> m_LLVMModuleOS;
    llvm::OwningPtr<llvm::raw_fd_ostream> m_MacrosOS;

    m_LookupTablesOS.reset(createOutputFile("lookup",
                                            &m_LookupTablesFile));
    m_IncludedFilesOS.reset(createOutputFile("included",
                                             &m_IncludedFilesFile));
    m_ASTOS.reset(createOutputFile("ast", &m_ASTFile));
    m_LLVMModuleOS.reset(createOutputFile("module", &m_LLVMModuleFile));
    m_MacrosOS.reset(createOutputFile("macros", &m_MacrosFile));

    printLookupTables(*m_LookupTablesOS.get(), m_ASTContext);
    printIncludedFiles(*m_IncludedFilesOS.get(),
                       m_ASTContext.getSourceManager());
    printAST(*m_ASTOS.get(), m_ASTContext);
    if (m_Module)
      printLLVMModule(*m_LLVMModuleOS.get(), *m_Module);
    printMacroDefinitions(*m_MacrosOS.get(), m_Preprocessor);
  }
  namespace {
    std::string getCurrentTimeAsString() {
      time_t rawtime;
      struct tm * timeinfo;
      char buffer [80];

      time (&rawtime);
      timeinfo = localtime (&rawtime);

      strftime (buffer, 80, "%I_%M_%S", timeinfo);
      return buffer;
    }
  }

  // Copied with modifications from CompilerInstance.cpp
  llvm::raw_fd_ostream*
  ClangInternalState::createOutputFile(llvm::StringRef OutFile,
                                       std::string *TempPathName/*=0*/,
                                       bool RemoveFileOnSignal/*=true*/) {
    llvm::OwningPtr<llvm::raw_fd_ostream> OS;
    std::string OSFile;
    llvm::SmallString<256> OutputPath;
    llvm::sys::path::system_temp_directory(/*erasedOnReboot*/false, OutputPath);

    // Only create the temporary if the parent directory exists (or create
    // missing directories is true) and we can actually write to OutPath,
    // otherwise we want to fail early.
    llvm::SmallString<256> TempPath(OutputPath);
    llvm::sys::fs::make_absolute(TempPath);
    assert(llvm::sys::fs::is_directory(TempPath.str()) && "Must be a folder.");
    // Create a temporary file.
    llvm::sys::path::append(TempPath, OutFile);
    TempPath += "-" + getCurrentTimeAsString();
    TempPath += "-%%%%%%%%";
    int fd;
    if (llvm::sys::fs::createUniqueFile(TempPath.str(), fd, TempPath)
        == llvm::errc::success) {
      OS.reset(new llvm::raw_fd_ostream(fd, /*shouldClose=*/true));
      OSFile = TempPath.str();
    }

    // Make sure the out stream file gets removed if we crash.
    if (RemoveFileOnSignal)
      llvm::sys::RemoveFileOnSignal(OSFile);

    if (TempPathName)
      *TempPathName = OSFile;

    return OS.take();
  }

  void ClangInternalState::compare(ClangInternalState& other) {
    std::string differences = "";
    // Ignore the builtins
    typedef llvm::SmallVector<const char*, 1024> Builtins;
    Builtins builtinNames;
    m_ASTContext.BuiltinInfo.GetBuiltinNames(builtinNames);
    for (Builtins::iterator I = builtinNames.begin();
         I != builtinNames.end();) {
      if (llvm::StringRef(*I).startswith("__builtin"))
        I = builtinNames.erase(I);
      else
        ++I;
    }

    builtinNames.push_back(".*__builtin.*");

    if (differentContent(m_LookupTablesFile, other.m_LookupTablesFile,
                         differences, &builtinNames)) {
      llvm::errs() << "Differences in the lookup tables\n";
      llvm::errs() << differences << "\n";
      differences = "";
    }

    if (differentContent(m_IncludedFilesFile, other.m_IncludedFilesFile,
                         differences)) {
      llvm::errs() << "Differences in the included files\n";
      llvm::errs() << differences << "\n";
      differences = "";
    }
    if (differentContent(m_ASTFile, other.m_ASTFile, differences)) {
      llvm::errs() << "Differences in the AST \n";
      llvm::errs() << differences << "\n";
      differences = "";
    }

    if (differentContent(m_LLVMModuleFile, other.m_LLVMModuleFile, differences)){
      llvm::errs() << "Differences in the llvm Module \n";
      llvm::errs() << differences << "\n";
      differences = "";
    }
    if (differentContent(m_MacrosFile, other.m_MacrosFile, differences)){
      llvm::errs() << "Differences in the Macro Definitions \n";
      llvm::errs() << differences << "\n";
      differences = "";
    }
  }

  bool ClangInternalState::differentContent(const std::string& file1,
                                            const std::string& file2,
                                            std::string& differences,
                const llvm::SmallVectorImpl<const char*>* ignores/*=0*/) const {
    std::string diffCall = m_DiffCommand;
    if (ignores) {
      for (size_t i = 0, e = ignores->size(); i < e; ++i) {
        diffCall += " --ignore-matching-lines=\".*";
        diffCall += (*ignores)[i];
        diffCall += ".*\"";
      }
    }

    FILE* pipe = popen((diffCall + " " + file1 + " " + file2).c_str() , "r");
    assert(pipe && "Error creating the pipe");
    assert(differences.empty() && "Must be empty");

    char buffer[128];
    while(!feof(pipe)) {
      if(fgets(buffer, 128, pipe) != NULL)
        differences += buffer;
    }
    pclose(pipe);

    if (!differences.empty())
      llvm::errs() << diffCall << " " << file1 << " " << file2 << "\n";

    return !differences.empty();
  }

  class DumpLookupTables : public RecursiveASTVisitor<DumpLookupTables> {
  private:
    llvm::raw_ostream& m_OS;
  public:
    DumpLookupTables(llvm::raw_ostream& OS) : m_OS(OS) { }
    bool VisitDecl(Decl* D) {
      if (DeclContext* DC = dyn_cast<DeclContext>(D))
        VisitDeclContext(DC);
      return true;
    }

    bool VisitDeclContext(DeclContext* DC) {
      // If the lookup is pending for building, force its creation.
      if (DC == DC->getPrimaryContext() && !DC->getLookupPtr())
        DC->buildLookup();
      DC->dumpLookups(m_OS);
      return true;
    }
  };

  void ClangInternalState::printLookupTables(llvm::raw_ostream& Out,
                                             ASTContext& C) {
    DumpLookupTables dumper(Out);
    dumper.TraverseDecl(C.getTranslationUnitDecl());
  }

  void ClangInternalState::printIncludedFiles(llvm::raw_ostream& Out,
                                              SourceManager& SM) {
    for (clang::SourceManager::fileinfo_iterator I = SM.fileinfo_begin(),
           E = SM.fileinfo_end(); I != E; ++I) {
      const clang::SrcMgr::ContentCache &C = *I->second;
      const clang::FileEntry *FE = C.OrigEntry;
      // Our error recovery purges the cache of the FileEntry, but keeps
      // the FileEntry's pointer so that if it was used by smb (like the
      // SourceManager) it wouldn't be dangling. In that case we shouldn't
      // print the FileName, because semantically it is not there.
      if (!FE->getSize() && !FE->getModificationTime())
        continue;
      std::string fileName(FE->getName());
      if (!(fileName.compare(0, 5, "/usr/") == 0 &&
            fileName.find("/bits/") != std::string::npos)) {
        Out << fileName << '\n';
      }
    }
  }

  void ClangInternalState::printAST(llvm::raw_ostream& Out, ASTContext& C) {
    TranslationUnitDecl* TU = C.getTranslationUnitDecl();
    unsigned Indentation = 0;
    bool PrintInstantiation = false;
    std::string ErrMsg;
    clang::PrintingPolicy policy = C.getPrintingPolicy();
    TU->print(Out, policy, Indentation, PrintInstantiation);
    // TODO: For future when we relpace the bump allocation with slab.
    //
    //Out << "Allocated memory: " << C.getAllocatedMemory();
    //Out << "Side table allocated memory: " << C.getSideTableAllocatedMemory();
    Out.flush();
  }

  void ClangInternalState::printLLVMModule(llvm::raw_ostream& Out,
                                           llvm::Module& M) {
    M.print(Out, /*AssemblyAnnotationWriter*/ 0);
  }

  void ClangInternalState::printMacroDefinitions(llvm::raw_ostream& Out,
                            clang::Preprocessor& PP) {
    PP.printMacros(Out);
  }
} // end namespace cling
<commit_msg>Prepend cling prefix, which makes the file deletion easier.<commit_after>//------------------------------------------------------------------------------
// CLING - the C++ LLVM-based InterpreterG :)
// author:  Vassil Vassilev <vasil.georgiev.vasilev@cern.ch>
//
// This file is dual-licensed: you can choose to license it under the University
// of Illinois Open Source License or the GNU Lesser General Public License. See
// LICENSE.TXT for details.
//------------------------------------------------------------------------------

#include "cling/Interpreter/ClangInternalState.h"

#include "clang/AST/ASTContext.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Basic/Builtins.h"
#include "clang/Basic/SourceManager.h"

#include "llvm/ADT/SmallString.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/Signals.h"

#include <cstdio>
#include <string>
#include <time.h>

#ifdef WIN32
#define popen _popen
#define pclose _pclose
#endif

using namespace clang;

namespace cling {

  ClangInternalState::ClangInternalState(ASTContext& AC, Preprocessor& PP,
                                         llvm::Module* M, const std::string& name)
    : m_ASTContext(AC), m_Preprocessor(PP), m_Module(M),
      m_DiffCommand("diff -u --text "), m_Name(name) {
    store();
  }

  ClangInternalState::~ClangInternalState() {
    // cleanup the temporary files:
    remove(m_LookupTablesFile.c_str());
    remove(m_IncludedFilesFile.c_str());
    remove(m_ASTFile.c_str());
    remove(m_LLVMModuleFile.c_str());
    remove(m_MacrosFile.c_str());
  }

  void ClangInternalState::store() {
    // Cannot use the stack (private copy ctor)
    llvm::OwningPtr<llvm::raw_fd_ostream> m_LookupTablesOS;
    llvm::OwningPtr<llvm::raw_fd_ostream> m_IncludedFilesOS;
    llvm::OwningPtr<llvm::raw_fd_ostream> m_ASTOS;
    llvm::OwningPtr<llvm::raw_fd_ostream> m_LLVMModuleOS;
    llvm::OwningPtr<llvm::raw_fd_ostream> m_MacrosOS;

    m_LookupTablesOS.reset(createOutputFile("lookup",
                                            &m_LookupTablesFile));
    m_IncludedFilesOS.reset(createOutputFile("included",
                                             &m_IncludedFilesFile));
    m_ASTOS.reset(createOutputFile("ast", &m_ASTFile));
    m_LLVMModuleOS.reset(createOutputFile("module", &m_LLVMModuleFile));
    m_MacrosOS.reset(createOutputFile("macros", &m_MacrosFile));

    printLookupTables(*m_LookupTablesOS.get(), m_ASTContext);
    printIncludedFiles(*m_IncludedFilesOS.get(),
                       m_ASTContext.getSourceManager());
    printAST(*m_ASTOS.get(), m_ASTContext);
    if (m_Module)
      printLLVMModule(*m_LLVMModuleOS.get(), *m_Module);
    printMacroDefinitions(*m_MacrosOS.get(), m_Preprocessor);
  }
  namespace {
    std::string getCurrentTimeAsString() {
      time_t rawtime;
      struct tm * timeinfo;
      char buffer [80];

      time (&rawtime);
      timeinfo = localtime (&rawtime);

      strftime (buffer, 80, "%I_%M_%S", timeinfo);
      return buffer;
    }
  }

  // Copied with modifications from CompilerInstance.cpp
  llvm::raw_fd_ostream*
  ClangInternalState::createOutputFile(llvm::StringRef OutFile,
                                       std::string *TempPathName/*=0*/,
                                       bool RemoveFileOnSignal/*=true*/) {
    llvm::OwningPtr<llvm::raw_fd_ostream> OS;
    std::string OSFile;
    llvm::SmallString<256> OutputPath;
    llvm::sys::path::system_temp_directory(/*erasedOnReboot*/false, OutputPath);

    // Only create the temporary if the parent directory exists (or create
    // missing directories is true) and we can actually write to OutPath,
    // otherwise we want to fail early.
    llvm::SmallString<256> TempPath(OutputPath);
    llvm::sys::fs::make_absolute(TempPath);
    assert(llvm::sys::fs::is_directory(TempPath.str()) && "Must be a folder.");
    // Create a temporary file.
    llvm::sys::path::append(TempPath, "cling-" + OutFile);
    TempPath += "-" + getCurrentTimeAsString();
    TempPath += "-%%%%%%%%";
    int fd;
    if (llvm::sys::fs::createUniqueFile(TempPath.str(), fd, TempPath)
        == llvm::errc::success) {
      OS.reset(new llvm::raw_fd_ostream(fd, /*shouldClose=*/true));
      OSFile = TempPath.str();
    }

    // Make sure the out stream file gets removed if we crash.
    if (RemoveFileOnSignal)
      llvm::sys::RemoveFileOnSignal(OSFile);

    if (TempPathName)
      *TempPathName = OSFile;

    return OS.take();
  }

  void ClangInternalState::compare(ClangInternalState& other) {
    std::string differences = "";
    // Ignore the builtins
    typedef llvm::SmallVector<const char*, 1024> Builtins;
    Builtins builtinNames;
    m_ASTContext.BuiltinInfo.GetBuiltinNames(builtinNames);
    for (Builtins::iterator I = builtinNames.begin();
         I != builtinNames.end();) {
      if (llvm::StringRef(*I).startswith("__builtin"))
        I = builtinNames.erase(I);
      else
        ++I;
    }

    builtinNames.push_back(".*__builtin.*");

    if (differentContent(m_LookupTablesFile, other.m_LookupTablesFile,
                         differences, &builtinNames)) {
      llvm::errs() << "Differences in the lookup tables\n";
      llvm::errs() << differences << "\n";
      differences = "";
    }

    if (differentContent(m_IncludedFilesFile, other.m_IncludedFilesFile,
                         differences)) {
      llvm::errs() << "Differences in the included files\n";
      llvm::errs() << differences << "\n";
      differences = "";
    }
    if (differentContent(m_ASTFile, other.m_ASTFile, differences)) {
      llvm::errs() << "Differences in the AST \n";
      llvm::errs() << differences << "\n";
      differences = "";
    }

    if (differentContent(m_LLVMModuleFile, other.m_LLVMModuleFile, differences)){
      llvm::errs() << "Differences in the llvm Module \n";
      llvm::errs() << differences << "\n";
      differences = "";
    }
    if (differentContent(m_MacrosFile, other.m_MacrosFile, differences)){
      llvm::errs() << "Differences in the Macro Definitions \n";
      llvm::errs() << differences << "\n";
      differences = "";
    }
  }

  bool ClangInternalState::differentContent(const std::string& file1,
                                            const std::string& file2,
                                            std::string& differences,
                const llvm::SmallVectorImpl<const char*>* ignores/*=0*/) const {
    std::string diffCall = m_DiffCommand;
    if (ignores) {
      for (size_t i = 0, e = ignores->size(); i < e; ++i) {
        diffCall += " --ignore-matching-lines=\".*";
        diffCall += (*ignores)[i];
        diffCall += ".*\"";
      }
    }

    FILE* pipe = popen((diffCall + " " + file1 + " " + file2).c_str() , "r");
    assert(pipe && "Error creating the pipe");
    assert(differences.empty() && "Must be empty");

    char buffer[128];
    while(!feof(pipe)) {
      if(fgets(buffer, 128, pipe) != NULL)
        differences += buffer;
    }
    pclose(pipe);

    if (!differences.empty())
      llvm::errs() << diffCall << " " << file1 << " " << file2 << "\n";

    return !differences.empty();
  }

  class DumpLookupTables : public RecursiveASTVisitor<DumpLookupTables> {
  private:
    llvm::raw_ostream& m_OS;
  public:
    DumpLookupTables(llvm::raw_ostream& OS) : m_OS(OS) { }
    bool VisitDecl(Decl* D) {
      if (DeclContext* DC = dyn_cast<DeclContext>(D))
        VisitDeclContext(DC);
      return true;
    }

    bool VisitDeclContext(DeclContext* DC) {
      // If the lookup is pending for building, force its creation.
      if (DC == DC->getPrimaryContext() && !DC->getLookupPtr())
        DC->buildLookup();
      DC->dumpLookups(m_OS);
      return true;
    }
  };

  void ClangInternalState::printLookupTables(llvm::raw_ostream& Out,
                                             ASTContext& C) {
    DumpLookupTables dumper(Out);
    dumper.TraverseDecl(C.getTranslationUnitDecl());
  }

  void ClangInternalState::printIncludedFiles(llvm::raw_ostream& Out,
                                              SourceManager& SM) {
    for (clang::SourceManager::fileinfo_iterator I = SM.fileinfo_begin(),
           E = SM.fileinfo_end(); I != E; ++I) {
      const clang::SrcMgr::ContentCache &C = *I->second;
      const clang::FileEntry *FE = C.OrigEntry;
      // Our error recovery purges the cache of the FileEntry, but keeps
      // the FileEntry's pointer so that if it was used by smb (like the
      // SourceManager) it wouldn't be dangling. In that case we shouldn't
      // print the FileName, because semantically it is not there.
      if (!FE->getSize() && !FE->getModificationTime())
        continue;
      std::string fileName(FE->getName());
      if (!(fileName.compare(0, 5, "/usr/") == 0 &&
            fileName.find("/bits/") != std::string::npos)) {
        Out << fileName << '\n';
      }
    }
  }

  void ClangInternalState::printAST(llvm::raw_ostream& Out, ASTContext& C) {
    TranslationUnitDecl* TU = C.getTranslationUnitDecl();
    unsigned Indentation = 0;
    bool PrintInstantiation = false;
    std::string ErrMsg;
    clang::PrintingPolicy policy = C.getPrintingPolicy();
    TU->print(Out, policy, Indentation, PrintInstantiation);
    // TODO: For future when we relpace the bump allocation with slab.
    //
    //Out << "Allocated memory: " << C.getAllocatedMemory();
    //Out << "Side table allocated memory: " << C.getSideTableAllocatedMemory();
    Out.flush();
  }

  void ClangInternalState::printLLVMModule(llvm::raw_ostream& Out,
                                           llvm::Module& M) {
    M.print(Out, /*AssemblyAnnotationWriter*/ 0);
  }

  void ClangInternalState::printMacroDefinitions(llvm::raw_ostream& Out,
                            clang::Preprocessor& PP) {
    PP.printMacros(Out);
  }
} // end namespace cling
<|endoftext|>


<commit_before>#ifndef CONTROL_HPP
#define CONTROL_HPP

#include "model.hpp"
#include "view.hpp"

namespace Control {
	typedef enum : unsigned char {
		e_tid_view=0, ///< The view task ID, necessarily 0 (master)
		e_tid_model,  ///< The model task ID
		e_tid_total   ///< The total number of enumerated tasks
	} e_tid; ///< The ID of a task

	/// A data structure controlling both model and view
	struct control : public Util::task {
		std::vector<std::string> errors;
		const char *mpath;
		View::view viewer;

		float r_dead = .125f;
		void deadzone(float &x, float &y);

		bool init(std::atomic_bool &alive) override;
		bool poll(std::atomic_bool &alive) override;
		bool run(std::atomic_bool &alive) override;

		/** Constructor; initializes and applies shaders
 		 * @param alive Shared state; false signals shutdown
 		 * @param obj The path to a Wavefront obj model
 		 */
		control(std::atomic_bool &alive, const char *obj);
	};
}

#endif
<commit_msg>Documentation fix<commit_after>#ifndef CONTROL_HPP
#define CONTROL_HPP

#include "model.hpp"
#include "view.hpp"

namespace Control {
	typedef enum : unsigned char {
		e_tid_view=0, ///< The view task ID, necessarily 0 (master)
		e_tid_model,  ///< The model task ID
		e_tid_total   ///< The total number of enumerated tasks
	} e_tid; ///< The ID of a task

	/// A data structure controlling both model and view
	struct control : public Util::task {
		std::vector<std::string> errors;
		const char *mpath;
		View::view viewer;

		float r_dead = .125f;
		/** Joystick deadzone; zeroes inputs below threshold and normalizes
		 * inputs above threshold.
		 * @param x First axis
		 * @param y Second axis
		 */
		void deadzone(float &x, float &y);

		bool init(std::atomic_bool &alive) override;
		bool poll(std::atomic_bool &alive) override;
		bool run(std::atomic_bool &alive) override;

		/** Constructor; initializes and applies shaders
 		 * @param alive Shared state; false signals shutdown
 		 * @param obj The path to a Wavefront obj model
 		 */
		control(std::atomic_bool &alive, const char *obj);
	};
}

#endif
<|endoftext|>


<commit_before>//=======================================================================
// Copyright (c) 2014 Baptiste Wicht
// Distributed under the terms of the MIT License.
// (See accompanying file LICENSE or copy at
//  http://opensource.org/licenses/MIT)
//=======================================================================

#ifndef DBN_RBM_INL
#define DBN_RBM_INL

#include <cmath>
#include <vector>
#include <random>
#include <functional>
#include <ctime>

#include "etl/fast_matrix.hpp"
#include "etl/fast_vector.hpp"
#include "etl/dyn_vector.hpp"
#include "etl/multiplication.hpp"

#include "rbm_base.hpp"      //The base class
#include "stop_watch.hpp"    //Performance counter
#include "assert.hpp"
#include "base_conf.hpp"
#include "math.hpp"
#include "io.hpp"

namespace dll {

template<typename RBM>
struct rbm_trainer;

/*!
 * \brief Standard version of Restricted Boltzmann Machine
 *
 * This follows the definition of a RBM by Geoffrey Hinton.
 */
template<typename Desc>
class rbm : public rbm_base<Desc> {
public:
    typedef double weight;
    typedef double value_t;

    using desc = Desc;

    static constexpr const std::size_t num_visible = desc::num_visible;
    static constexpr const std::size_t num_hidden = desc::num_hidden;

    static constexpr const unit_type visible_unit = desc::visible_unit;
    static constexpr const unit_type hidden_unit = desc::hidden_unit;

    static constexpr const bool DBN = desc::DBN;

    static_assert(visible_unit != unit_type::SOFTMAX && visible_unit != unit_type::EXP,
        "Exponential and softmax Visible units are not support");
    static_assert(hidden_unit != unit_type::GAUSSIAN,
        "Gaussian hidden units are not supported");

    static constexpr const std::size_t num_visible_gra = DBN ? num_visible : 0;
    static constexpr const std::size_t num_hidden_gra = DBN ? num_hidden : 0;

    //Weights and biases
    etl::fast_matrix<weight, num_visible, num_hidden> w;    //!< Weights
    etl::fast_vector<weight, num_hidden> b;                 //!< Hidden biases
    etl::fast_vector<weight, num_visible> c;                //!< Visible biases

    //Reconstruction data
    etl::fast_vector<weight, num_visible> v1; //!< State of the visible units

    etl::fast_vector<weight, num_hidden> h1_a; //!< Activation probabilities of hidden units after first CD-step
    etl::fast_vector<weight, num_hidden> h1_s; //!< Sampled value of hidden units after first CD-step

    etl::fast_vector<weight, num_visible> v2_a; //!< Activation probabilities of visible units after first CD-step
    etl::fast_vector<weight, num_visible> v2_s; //!< Sampled value of visible units after first CD-step

    etl::fast_vector<weight, num_hidden> h2_a; //!< Activation probabilities of hidden units after last CD-step
    etl::fast_vector<weight, num_hidden> h2_s; //!< Sampled value of hidden units after last CD-step

    //Gradients computations for DBN
    etl::fast_matrix<weight, num_visible, num_hidden>& gr_w = w;
    etl::fast_vector<weight, num_hidden>& gr_b = b;

    etl::fast_matrix<weight, num_visible_gra, num_hidden_gra> gr_w_incs;
    etl::fast_vector<weight, num_hidden_gra> gr_b_incs;

    etl::fast_matrix<weight, num_visible_gra, num_hidden_gra> gr_w_best;
    etl::fast_vector<weight, num_hidden_gra> gr_b_best;

    etl::fast_matrix<weight, num_visible_gra, num_hidden_gra> gr_w_best_incs;
    etl::fast_vector<weight, num_hidden_gra> gr_b_best_incs;

    etl::fast_matrix<weight, num_visible_gra, num_hidden_gra> gr_w_df0;
    etl::fast_vector<weight, num_hidden_gra> gr_b_df0;

    etl::fast_matrix<weight, num_visible_gra, num_hidden_gra> gr_w_df3;
    etl::fast_vector<weight, num_hidden_gra> gr_b_df3;

    etl::fast_matrix<weight, num_visible_gra, num_hidden_gra> gr_w_s;
    etl::fast_vector<weight, num_hidden_gra> gr_b_s;

    etl::fast_matrix<weight, num_visible_gra, num_hidden_gra> gr_w_tmp;
    etl::fast_vector<weight, num_hidden_gra> gr_b_tmp;

    std::vector<etl::dyn_vector<weight>> gr_probs_a;
    std::vector<etl::dyn_vector<weight>> gr_probs_s;

public:
    //No copying
    rbm(const rbm& rbm) = delete;
    rbm& operator=(const rbm& rbm) = delete;

    //No moving
    rbm(rbm&& rbm) = delete;
    rbm& operator=(rbm&& rbm) = delete;

    /*!
     * \brief Initialize a RBM with basic weights.
     *
     * The weights are initialized from a normal distribution of
     * zero-mean and 0.1 variance.
     */
    rbm() : b(0.0), c(0.0) {
        //Initialize the weights with a zero-mean and unit variance Gaussian distribution
        static std::default_random_engine rand_engine(std::time(nullptr));
        static std::normal_distribution<weight> distribution(0.0, 1.0);
        static auto generator = std::bind(distribution, rand_engine);

        for(auto& weight : w){
            weight = generator() * 0.1;
        }

        //Better initialization of learning rate
        rbm_base<desc>::learning_rate =
                visible_unit == unit_type::GAUSSIAN && is_relu(hidden_unit) ? 1e-5
            :   visible_unit == unit_type::GAUSSIAN || is_relu(hidden_unit) ? 1e-3
            :   /* Only ReLU and Gaussian Units needs lower rate */           1e-1;
    }

    void store(std::ostream& os) const {
        binary_write_all(os, w);
        binary_write_all(os, b);
        binary_write_all(os, c);
    }

    void load(std::istream& is){
        binary_load_all(is, w);
        binary_load_all(is, b);
        binary_load_all(is, c);
    }

    template<typename Samples>
    double train(const Samples& training_data, std::size_t max_epochs){
        typedef typename std::remove_reference<decltype(*this)>::type this_type;

        dll::rbm_trainer<this_type> trainer;
        return trainer.train(*this, training_data, max_epochs);
    }

    template<typename Samples>
    void init_weights(const Samples& training_data){
        //Initialize the visible biases to log(pi/(1-pi))
        for(size_t i = 0; i < num_visible; ++i){
            auto count = std::count_if(training_data.begin(), training_data.end(),
                [i](auto& a){return a[i] == 1; });

            auto pi = static_cast<weight>(count) / training_data.size();
            pi += 0.0001;
            c(i) = log(pi / (1.0 - pi));

            dll_assert(std::isfinite(c(i)), "NaN verify");
        }
    }

    template<typename H, typename V>
    void activate_hidden(H& h_a, H& h_s, const V& v_a, const V& v_s) const {
        return activate_hidden(h_a, h_s, v_a, v_s, b, w);
    }

    template<bool Temp, typename H, typename V>
    void gr_activate_hidden(H& h_a, H& h_s, const V& v_a, const V& v_s) const {
        return activate_hidden(h_a, h_s, v_a, v_s, Temp ? gr_b_tmp : gr_b, Temp ? gr_w_tmp : gr_w);
    }

    template<typename H, typename V, typename B, typename W>
    static void activate_hidden(H& h_a, H& h_s, const V& v_a, const V&, const B& b, const W& w){
        static std::default_random_engine rand_engine(std::time(nullptr));

        using namespace etl;

        static fast_matrix<weight, 1, num_hidden> t;

        if(hidden_unit == unit_type::BINARY){
            h_a = sigmoid(b + mmul(reshape<1, num_visible>(v_a), w, t));
            h_s = bernoulli(h_a);
        } else if(hidden_unit == unit_type::EXP){
            h_a = exp(b + mmul(reshape<1, num_visible>(v_a), w, t));
            h_s = bernoulli(h_a);
        } else if(hidden_unit == unit_type::RELU){
            h_a = max(b + mmul(reshape<1, num_visible>(v_a), w, t), 0.0);
            h_s = logistic_noise(h_a);
        } else if(hidden_unit == unit_type::RELU6){
            h_a = min(max(b + mmul(reshape<1, num_visible>(v_a), w, t), 0.0), 6.0);
            h_s = ranged_noise(h_a, 6.0);
        } else if(hidden_unit == unit_type::RELU1){
            h_a = min(max(b + mmul(reshape<1, num_visible>(v_a), w, t), 0.0), 1.0);
            h_s = ranged_noise(h_a, 1.0);
        } else if(hidden_unit == unit_type::SOFTMAX){
            weight exp_sum = sum(exp(b + mmul(reshape<1, num_visible>(v_a), w, t)));

            h_a = exp(b + mmul(reshape<1, num_visible>(v_a), w, t)) / exp_sum;

            auto max = std::max_element(h_a.begin(), h_a.end());

            h_s = 0.0;
            h_s(std::distance(h_a.begin(), max)) = 1.0;
        } else {
            dll_unreachable("Invalid path");
        }

        nan_check_deep(h_a);
        nan_check_deep(h_s);
    }

    template<typename H, typename V>
    void activate_visible(const H&, const H& h_s, V& v_a, V& v_s) const {
        static std::default_random_engine rand_engine(std::time(nullptr));

        using namespace etl;

        static fast_matrix<weight, num_visible, 1> t;

        if(visible_unit == unit_type::BINARY){
            v_a = sigmoid(c + mmul(w, reshape<num_hidden, 1>(h_s), t));
            v_s = bernoulli(v_a);
        } else if(visible_unit == unit_type::GAUSSIAN){
            v_a = c + mmul(w, reshape<num_hidden, 1>(h_s), t);
            v_s = noise(v_a);
        } else if(visible_unit == unit_type::RELU){
            v_a = max(c + mmul(w, reshape<num_hidden, 1>(h_s), t), 0.0);
            v_s = noise(v_a);
        } else {
            dll_unreachable("Invalid path");
        }

        nan_check_deep(v_a);
        nan_check_deep(v_s);
    }

    weight free_energy() const {
        weight energy = 0.0;

        for(size_t i = 0; i < num_visible; ++i){
            for(size_t j = 0; j < num_hidden; ++j){
                energy += w(i, j) * b(j) * c(i);
            }
        }

        return -energy;
    }

    template<typename Sample>
    void reconstruct(const Sample& items){
        dll_assert(items.size() == num_visible, "The size of the training sample must match visible units");

        stop_watch<> watch;

        //Set the state of the visible units
        v1 = items;

        activate_hidden(h1_a, h1_s, v1, v1);
        activate_visible(h1_a, h1_s, v2_a, v2_s);
        activate_hidden(h2_a, h2_s, v2_a, v2_s);

        std::cout << "Reconstruction took " << watch.elapsed() << "ms" << std::endl;
    }

    void display() const {
        display_visible_units();
        display_hidden_units();
    }

    void display_visible_units() const {
        std::cout << "Visible  Value" << std::endl;

        for(size_t i = 0; i < num_visible; ++i){
            printf("%-8lu %d\n", i, v2_s(i));
        }
    }

    void display_visible_units(size_t matrix) const {
        for(size_t i = 0; i < matrix; ++i){
            for(size_t j = 0; j < matrix; ++j){
                std::cout << v2_s(i * matrix + j) << " ";
            }
            std::cout << std::endl;
        }
    }

    void display_hidden_units() const {
        std::cout << "Hidden Value" << std::endl;

        for(size_t j = 0; j < num_hidden; ++j){
            printf("%-8lu %d\n", j, h2_s(j));
        }
    }

    void display_weights(){
        for(size_t j = 0; j < num_hidden; ++j){
            for(size_t i = 0; i < num_visible; ++i){
                std::cout << w(i, j) << " ";
            }
            std::cout << std::endl;
        }
    }

    void display_weights(size_t matrix){
        for(size_t j = 0; j < num_hidden; ++j){
            for(size_t i = 0; i < num_visible;){
                for(size_t m = 0; m < matrix; ++m){
                    std::cout << w(i++, j) << " ";
                }
                std::cout << std::endl;
            }
        }
    }
};

} //end of dbn namespace

#endif<commit_msg>Add new fields for gradients<commit_after>//=======================================================================
// Copyright (c) 2014 Baptiste Wicht
// Distributed under the terms of the MIT License.
// (See accompanying file LICENSE or copy at
//  http://opensource.org/licenses/MIT)
//=======================================================================

#ifndef DBN_RBM_INL
#define DBN_RBM_INL

#include <cmath>
#include <vector>
#include <random>
#include <functional>
#include <ctime>

#include "etl/fast_matrix.hpp"
#include "etl/fast_vector.hpp"
#include "etl/dyn_vector.hpp"
#include "etl/multiplication.hpp"

#include "rbm_base.hpp"      //The base class
#include "stop_watch.hpp"    //Performance counter
#include "assert.hpp"
#include "base_conf.hpp"
#include "math.hpp"
#include "io.hpp"

namespace dll {

template<typename RBM>
struct rbm_trainer;

/*!
 * \brief Standard version of Restricted Boltzmann Machine
 *
 * This follows the definition of a RBM by Geoffrey Hinton.
 */
template<typename Desc>
class rbm : public rbm_base<Desc> {
public:
    typedef double weight;
    typedef double value_t;

    using desc = Desc;

    static constexpr const std::size_t num_visible = desc::num_visible;
    static constexpr const std::size_t num_hidden = desc::num_hidden;

    static constexpr const unit_type visible_unit = desc::visible_unit;
    static constexpr const unit_type hidden_unit = desc::hidden_unit;

    static constexpr const bool DBN = desc::DBN;

    static_assert(visible_unit != unit_type::SOFTMAX && visible_unit != unit_type::EXP,
        "Exponential and softmax Visible units are not support");
    static_assert(hidden_unit != unit_type::GAUSSIAN,
        "Gaussian hidden units are not supported");

    static constexpr const std::size_t num_visible_gra = DBN ? num_visible : 0;
    static constexpr const std::size_t num_hidden_gra = DBN ? num_hidden : 0;

    //Weights and biases
    etl::fast_matrix<weight, num_visible, num_hidden> w;    //!< Weights
    etl::fast_vector<weight, num_hidden> b;                 //!< Hidden biases
    etl::fast_vector<weight, num_visible> c;                //!< Visible biases

    //Reconstruction data
    etl::fast_vector<weight, num_visible> v1; //!< State of the visible units

    etl::fast_vector<weight, num_hidden> h1_a; //!< Activation probabilities of hidden units after first CD-step
    etl::fast_vector<weight, num_hidden> h1_s; //!< Sampled value of hidden units after first CD-step

    etl::fast_vector<weight, num_visible> v2_a; //!< Activation probabilities of visible units after first CD-step
    etl::fast_vector<weight, num_visible> v2_s; //!< Sampled value of visible units after first CD-step

    etl::fast_vector<weight, num_hidden> h2_a; //!< Activation probabilities of hidden units after last CD-step
    etl::fast_vector<weight, num_hidden> h2_s; //!< Sampled value of hidden units after last CD-step

    //Gradients computations for DBN
    etl::fast_matrix<weight, num_visible, num_hidden>& gr_w = w;
    etl::fast_vector<weight, num_hidden>& gr_b = b;

    etl::fast_matrix<weight, num_visible_gra, num_hidden_gra> gr_w_incs;
    etl::fast_vector<weight, num_hidden_gra> gr_b_incs;

    etl::fast_matrix<weight, num_visible_gra, num_hidden_gra> gr_w_best;
    etl::fast_vector<weight, num_hidden_gra> gr_b_best;

    etl::fast_matrix<weight, num_visible_gra, num_hidden_gra> gr_w_best_incs;
    etl::fast_vector<weight, num_hidden_gra> gr_b_best_incs;

    etl::fast_matrix<weight, num_visible_gra, num_hidden_gra> gr_w_df0;
    etl::fast_vector<weight, num_hidden_gra> gr_b_df0;

    etl::fast_matrix<weight, num_visible_gra, num_hidden_gra> gr_w_df3;
    etl::fast_vector<weight, num_hidden_gra> gr_b_df3;

    etl::fast_matrix<weight, num_visible_gra, num_hidden_gra> gr_w_s;
    etl::fast_vector<weight, num_hidden_gra> gr_b_s;

    etl::fast_matrix<weight, num_visible_gra, num_hidden_gra> gr_w_tmp;
    etl::fast_vector<weight, num_hidden_gra> gr_b_tmp;

    std::vector<etl::dyn_vector<weight>> gr_probs_a;
    std::vector<etl::dyn_vector<weight>> gr_probs_s;

    //SGD

    etl::fast_matrix<weight, num_visible, num_hidden> w_grad;
    etl::fast_vector<weight, num_hidden> b_grad;
    etl::fast_vector<weight, num_visible> c_grad;

public:
    //No copying
    rbm(const rbm& rbm) = delete;
    rbm& operator=(const rbm& rbm) = delete;

    //No moving
    rbm(rbm&& rbm) = delete;
    rbm& operator=(rbm&& rbm) = delete;

    /*!
     * \brief Initialize a RBM with basic weights.
     *
     * The weights are initialized from a normal distribution of
     * zero-mean and 0.1 variance.
     */
    rbm() : b(0.0), c(0.0) {
        //Initialize the weights with a zero-mean and unit variance Gaussian distribution
        static std::default_random_engine rand_engine(std::time(nullptr));
        static std::normal_distribution<weight> distribution(0.0, 1.0);
        static auto generator = std::bind(distribution, rand_engine);

        for(auto& weight : w){
            weight = generator() * 0.1;
        }

        //Better initialization of learning rate
        rbm_base<desc>::learning_rate =
                visible_unit == unit_type::GAUSSIAN && is_relu(hidden_unit) ? 1e-5
            :   visible_unit == unit_type::GAUSSIAN || is_relu(hidden_unit) ? 1e-3
            :   /* Only ReLU and Gaussian Units needs lower rate */           1e-1;
    }

    void store(std::ostream& os) const {
        binary_write_all(os, w);
        binary_write_all(os, b);
        binary_write_all(os, c);
    }

    void load(std::istream& is){
        binary_load_all(is, w);
        binary_load_all(is, b);
        binary_load_all(is, c);
    }

    template<typename Samples>
    double train(const Samples& training_data, std::size_t max_epochs){
        typedef typename std::remove_reference<decltype(*this)>::type this_type;

        dll::rbm_trainer<this_type> trainer;
        return trainer.train(*this, training_data, max_epochs);
    }

    template<typename Samples>
    void init_weights(const Samples& training_data){
        //Initialize the visible biases to log(pi/(1-pi))
        for(size_t i = 0; i < num_visible; ++i){
            auto count = std::count_if(training_data.begin(), training_data.end(),
                [i](auto& a){return a[i] == 1; });

            auto pi = static_cast<weight>(count) / training_data.size();
            pi += 0.0001;
            c(i) = log(pi / (1.0 - pi));

            dll_assert(std::isfinite(c(i)), "NaN verify");
        }
    }

    template<typename H1, typename H2, typename V>
    void activate_hidden(H1& h_a, H2& h_s, const V& v_a, const V& v_s) const {
        return activate_hidden(h_a, h_s, v_a, v_s, b, w);
    }

    template<bool Temp, typename H1, typename H2, typename V>
    void gr_activate_hidden(H1& h_a, H2& h_s, const V& v_a, const V& v_s) const {
        return activate_hidden(h_a, h_s, v_a, v_s, Temp ? gr_b_tmp : gr_b, Temp ? gr_w_tmp : gr_w);
    }

    template<typename H1, typename H2, typename V, typename B, typename W>
    static void activate_hidden(H1& h_a, H2& h_s, const V& v_a, const V&, const B& b, const W& w){
        static std::default_random_engine rand_engine(std::time(nullptr));

        using namespace etl;

        static fast_matrix<weight, 1, num_hidden> t;

        if(hidden_unit == unit_type::BINARY){
            h_a = sigmoid(b + mmul(reshape<1, num_visible>(v_a), w, t));
            h_s = bernoulli(h_a);
        } else if(hidden_unit == unit_type::EXP){
            h_a = exp(b + mmul(reshape<1, num_visible>(v_a), w, t));
            h_s = bernoulli(h_a);
        } else if(hidden_unit == unit_type::RELU){
            h_a = max(b + mmul(reshape<1, num_visible>(v_a), w, t), 0.0);
            h_s = logistic_noise(h_a);
        } else if(hidden_unit == unit_type::RELU6){
            h_a = min(max(b + mmul(reshape<1, num_visible>(v_a), w, t), 0.0), 6.0);
            h_s = ranged_noise(h_a, 6.0);
        } else if(hidden_unit == unit_type::RELU1){
            h_a = min(max(b + mmul(reshape<1, num_visible>(v_a), w, t), 0.0), 1.0);
            h_s = ranged_noise(h_a, 1.0);
        } else if(hidden_unit == unit_type::SOFTMAX){
            weight exp_sum = sum(exp(b + mmul(reshape<1, num_visible>(v_a), w, t)));

            h_a = exp(b + mmul(reshape<1, num_visible>(v_a), w, t)) / exp_sum;

            auto max = std::max_element(h_a.begin(), h_a.end());

            h_s = 0.0;
            h_s(std::distance(h_a.begin(), max)) = 1.0;
        } else {
            dll_unreachable("Invalid path");
        }

        nan_check_deep(h_a);
        nan_check_deep(h_s);
    }

    template<typename H, typename V>
    void activate_visible(const H&, const H& h_s, V& v_a, V& v_s) const {
        static std::default_random_engine rand_engine(std::time(nullptr));

        using namespace etl;

        static fast_matrix<weight, num_visible, 1> t;

        if(visible_unit == unit_type::BINARY){
            v_a = sigmoid(c + mmul(w, reshape<num_hidden, 1>(h_s), t));
            v_s = bernoulli(v_a);
        } else if(visible_unit == unit_type::GAUSSIAN){
            v_a = c + mmul(w, reshape<num_hidden, 1>(h_s), t);
            v_s = noise(v_a);
        } else if(visible_unit == unit_type::RELU){
            v_a = max(c + mmul(w, reshape<num_hidden, 1>(h_s), t), 0.0);
            v_s = noise(v_a);
        } else {
            dll_unreachable("Invalid path");
        }

        nan_check_deep(v_a);
        nan_check_deep(v_s);
    }

    weight free_energy() const {
        weight energy = 0.0;

        for(size_t i = 0; i < num_visible; ++i){
            for(size_t j = 0; j < num_hidden; ++j){
                energy += w(i, j) * b(j) * c(i);
            }
        }

        return -energy;
    }

    template<typename Sample>
    void reconstruct(const Sample& items){
        dll_assert(items.size() == num_visible, "The size of the training sample must match visible units");

        stop_watch<> watch;

        //Set the state of the visible units
        v1 = items;

        activate_hidden(h1_a, h1_s, v1, v1);
        activate_visible(h1_a, h1_s, v2_a, v2_s);
        activate_hidden(h2_a, h2_s, v2_a, v2_s);

        std::cout << "Reconstruction took " << watch.elapsed() << "ms" << std::endl;
    }

    void display() const {
        display_visible_units();
        display_hidden_units();
    }

    void display_visible_units() const {
        std::cout << "Visible  Value" << std::endl;

        for(size_t i = 0; i < num_visible; ++i){
            printf("%-8lu %d\n", i, v2_s(i));
        }
    }

    void display_visible_units(size_t matrix) const {
        for(size_t i = 0; i < matrix; ++i){
            for(size_t j = 0; j < matrix; ++j){
                std::cout << v2_s(i * matrix + j) << " ";
            }
            std::cout << std::endl;
        }
    }

    void display_hidden_units() const {
        std::cout << "Hidden Value" << std::endl;

        for(size_t j = 0; j < num_hidden; ++j){
            printf("%-8lu %d\n", j, h2_s(j));
        }
    }

    void display_weights(){
        for(size_t j = 0; j < num_hidden; ++j){
            for(size_t i = 0; i < num_visible; ++i){
                std::cout << w(i, j) << " ";
            }
            std::cout << std::endl;
        }
    }

    void display_weights(size_t matrix){
        for(size_t j = 0; j < num_hidden; ++j){
            for(size_t i = 0; i < num_visible;){
                for(size_t m = 0; m < matrix; ++m){
                    std::cout << w(i++, j) << " ";
                }
                std::cout << std::endl;
            }
        }
    }
};

} //end of dbn namespace

#endif<|endoftext|>


<commit_before>#ifndef FORMAST_HPP_INCLUDED
#define FORMAST_HPP_INCLUDED

#ifdef FORMAST_STATIC
// formast static library
#  define FORMAST_API
#  define FORMAST_HIDDEN
#else
// formast shared library
#  if defined _WIN32 || defined __CYGWIN__
// Windows
#    ifdef formastlib_EXPORTS
// formast is being compiled: export API to dll
#      define FORMAST_API __declspec(dllexport)
#    else
// other code is being compiled, using formast.dll: import API from dll
#      define FORMAST_API __declspec(dllimport)
#    endif
#    define FORMAST_HIDDEN
#  elif __GNUC__ >= 4
// GNU Linux: set visibility of symbols
#    define FORMAST_API    __attribute__ ((visibility ("default")))
#    define FORMAST_HIDDEN __attribute__ ((visibility ("hidden")))
#  else
// unknown
#    define FORMAST_API
#    define FORMAST_HIDDEN
#  endif
#endif

#include <boost/cstdint.hpp>
#include <boost/optional.hpp>
#include <boost/shared_ptr.hpp>
#include <string>
#include <vector>

namespace formast
{

class Module
{
public:
    FORMAST_API Module();
private:
    // pimpl idiom
    FORMAST_HIDDEN class Impl;
    boost::shared_ptr<Impl> _impl;
    friend class Visitor;
    friend class Parser;
    friend class XmlParser;
};

class Stats
{
public:
    FORMAST_API Stats();
private:
    // pimpl idiom
    FORMAST_HIDDEN class Impl;
    boost::shared_ptr<Impl> _impl;
    friend class Visitor;
    friend class Parser;
    friend class XmlParser;
};

class Expr
{
public:
    FORMAST_API Expr();
    FORMAST_API Expr(Expr const & e);
    FORMAST_API Expr & operator=(Expr const & e);
private:
    // pimpl idiom
    FORMAST_HIDDEN class Impl;
    boost::shared_ptr<Impl> _impl;
    friend class Visitor;
    friend class Parser;
    friend class XmlParser;
};

typedef std::string Doc;

class Field
{
public:
    std::string type_;
    std::string name;
    boost::optional<Doc> doc;
    boost::optional<Expr> arr1;
    boost::optional<Expr> arr2;
};

class Class
{
public:
    std::string name;
    boost::optional<std::string> base_name;
    boost::optional<Doc> doc;
    boost::optional<Stats> stats;
};

class If
{
public:
    Expr expr;
    Stats then;
    boost::optional<Stats> else_;
};

class Parser
{
public:
    FORMAST_API Parser();
    FORMAST_API virtual ~Parser();
    FORMAST_API virtual void parse_stream(std::istream & is, Module & module) = 0;
    FORMAST_API void parse_string(std::string const & s, Module & module);
private:
    // pimpl idiom
    FORMAST_HIDDEN class Impl;
    boost::shared_ptr<Impl> _impl;
    friend class XmlParser;
};

class XmlParser : public Parser
{
public:
    FORMAST_API XmlParser();
    FORMAST_API virtual void parse_stream(std::istream & is, Module & module);
};

class Visitor
{
public:
    FORMAST_API Visitor();
    FORMAST_API virtual ~Visitor();

    FORMAST_API virtual void module(Module const & module);
    FORMAST_API virtual void module_class(Class const & class_);

    FORMAST_API virtual void stats(Stats const & stats);
    FORMAST_API virtual void stats_field(Field const & field);
    FORMAST_API virtual void stats_if(If const & if_);

    FORMAST_API virtual void expr(Expr const & e);
    FORMAST_API virtual void expr_uint(boost::uint64_t const & n);
    FORMAST_API virtual void expr_id(std::string const & i);
    FORMAST_API virtual void expr_pos(Expr const & right);
    FORMAST_API virtual void expr_neg(Expr const & right);
    FORMAST_API virtual void expr_add(Expr const & left, Expr const & right);
    FORMAST_API virtual void expr_sub(Expr const & left, Expr const & right);
    FORMAST_API virtual void expr_mul(Expr const & left, Expr const & right);
    FORMAST_API virtual void expr_div(Expr const & left, Expr const & right);
    FORMAST_API virtual void expr_mod(Expr const & left, Expr const & right);
    FORMAST_API virtual void expr_pow(Expr const & left, Expr const & right);
    FORMAST_API virtual void expr_logical_and(Expr const & left, Expr const & right);
    FORMAST_API virtual void expr_logical_or(Expr const & left, Expr const & right);
    FORMAST_API virtual void expr_logical_not(Expr const & right);
    FORMAST_API virtual void expr_bitwise_and(Expr const & left, Expr const & right);
    FORMAST_API virtual void expr_bitwise_or(Expr const & left, Expr const & right);
    FORMAST_API virtual void expr_bitwise_xor(Expr const & left, Expr const & right);
    FORMAST_API virtual void expr_compare_eq(Expr const & left, Expr const & right);
    FORMAST_API virtual void expr_compare_ne(Expr const & left, Expr const & right);
    FORMAST_API virtual void expr_compare_gt(Expr const & left, Expr const & right);
    FORMAST_API virtual void expr_compare_lt(Expr const & left, Expr const & right);
    FORMAST_API virtual void expr_compare_ge(Expr const & left, Expr const & right);
    FORMAST_API virtual void expr_compare_le(Expr const & left, Expr const & right);
    FORMAST_API virtual void expr_shift_left(Expr const & left, Expr const & right);
    FORMAST_API virtual void expr_shift_right(Expr const & left, Expr const & right);
private:
    // pimpl idiom
    FORMAST_HIDDEN class ExprVisitor;
    boost::shared_ptr<ExprVisitor> _expr_visitor;
    FORMAST_HIDDEN class ModuleVisitor;
    boost::shared_ptr<ModuleVisitor> _module_visitor;
    FORMAST_HIDDEN class StatsVisitor;
    boost::shared_ptr<StatsVisitor> _stats_visitor;
};

} // namespace formast

#endif
<commit_msg>Expose enum to public API.<commit_after>#ifndef FORMAST_HPP_INCLUDED
#define FORMAST_HPP_INCLUDED

#ifdef FORMAST_STATIC
// formast static library
#  define FORMAST_API
#  define FORMAST_HIDDEN
#else
// formast shared library
#  if defined _WIN32 || defined __CYGWIN__
// Windows
#    ifdef formastlib_EXPORTS
// formast is being compiled: export API to dll
#      define FORMAST_API __declspec(dllexport)
#    else
// other code is being compiled, using formast.dll: import API from dll
#      define FORMAST_API __declspec(dllimport)
#    endif
#    define FORMAST_HIDDEN
#  elif __GNUC__ >= 4
// GNU Linux: set visibility of symbols
#    define FORMAST_API    __attribute__ ((visibility ("default")))
#    define FORMAST_HIDDEN __attribute__ ((visibility ("hidden")))
#  else
// unknown
#    define FORMAST_API
#    define FORMAST_HIDDEN
#  endif
#endif

#include <boost/cstdint.hpp>
#include <boost/optional.hpp>
#include <boost/shared_ptr.hpp>
#include <string>
#include <vector>

namespace formast
{

class Module
{
public:
    FORMAST_API Module();
private:
    // pimpl idiom
    FORMAST_HIDDEN class Impl;
    boost::shared_ptr<Impl> _impl;
    friend class Visitor;
    friend class Parser;
    friend class XmlParser;
};

class Stats
{
public:
    FORMAST_API Stats();
private:
    // pimpl idiom
    FORMAST_HIDDEN class Impl;
    boost::shared_ptr<Impl> _impl;
    friend class Visitor;
    friend class Parser;
    friend class XmlParser;
};

class Expr
{
public:
    FORMAST_API Expr();
    FORMAST_API Expr(Expr const & e);
    FORMAST_API Expr & operator=(Expr const & e);
private:
    // pimpl idiom
    FORMAST_HIDDEN class Impl;
    boost::shared_ptr<Impl> _impl;
    friend class Visitor;
    friend class Parser;
    friend class XmlParser;
};

typedef std::string Doc;

class Field
{
public:
    std::string type_;
    std::string name;
    boost::optional<Doc> doc;
    boost::optional<Expr> arr1;
    boost::optional<Expr> arr2;
};

class Class
{
public:
    std::string name;
    boost::optional<std::string> base_name;
    boost::optional<Doc> doc;
    boost::optional<Stats> stats;
};

class If
{
public:
    Expr expr;
    Stats then;
    boost::optional<Stats> else_;
};

class EnumConst
{
public:
    std::string name;
    boost::uint64_t value;
    boost::optional<Doc> doc;
};

class EnumStats
{
public:
    FORMAST_API EnumStats();
private:
    // pimpl idiom
    FORMAST_HIDDEN class Impl;
    boost::shared_ptr<Impl> _impl;
    friend class Visitor;
    friend class Parser;
    friend class XmlParser;
};

class Enum
{
public:
    std::string name;
    std::string base_name;
    boost::optional<EnumStats> stats;
};

class Parser
{
public:
    FORMAST_API Parser();
    FORMAST_API virtual ~Parser();
    FORMAST_API virtual void parse_stream(std::istream & is, Module & module) = 0;
    FORMAST_API void parse_string(std::string const & s, Module & module);
private:
    // pimpl idiom
    FORMAST_HIDDEN class Impl;
    boost::shared_ptr<Impl> _impl;
    friend class XmlParser;
};

class XmlParser : public Parser
{
public:
    FORMAST_API XmlParser();
    FORMAST_API virtual void parse_stream(std::istream & is, Module & module);
};

class Visitor
{
public:
    FORMAST_API Visitor();
    FORMAST_API virtual ~Visitor();

    FORMAST_API virtual void module(Module const & module);
    FORMAST_API virtual void module_class(Class const & class_);
    FORMAST_API virtual void module_enum(Enum const & enum_);

    FORMAST_API virtual void stats(Stats const & stats);
    FORMAST_API virtual void stats_field(Field const & field);
    FORMAST_API virtual void stats_if(If const & if_);

    FORMAST_API virtual void enum_stats(EnumStats const & stats);
    FORMAST_API virtual void enum_stats_const(EnumConst const & const_);

    FORMAST_API virtual void expr(Expr const & e);
    FORMAST_API virtual void expr_uint(boost::uint64_t const & n);
    FORMAST_API virtual void expr_id(std::string const & i);
    FORMAST_API virtual void expr_pos(Expr const & right);
    FORMAST_API virtual void expr_neg(Expr const & right);
    FORMAST_API virtual void expr_add(Expr const & left, Expr const & right);
    FORMAST_API virtual void expr_sub(Expr const & left, Expr const & right);
    FORMAST_API virtual void expr_mul(Expr const & left, Expr const & right);
    FORMAST_API virtual void expr_div(Expr const & left, Expr const & right);
    FORMAST_API virtual void expr_mod(Expr const & left, Expr const & right);
    FORMAST_API virtual void expr_pow(Expr const & left, Expr const & right);
    FORMAST_API virtual void expr_logical_and(Expr const & left, Expr const & right);
    FORMAST_API virtual void expr_logical_or(Expr const & left, Expr const & right);
    FORMAST_API virtual void expr_logical_not(Expr const & right);
    FORMAST_API virtual void expr_bitwise_and(Expr const & left, Expr const & right);
    FORMAST_API virtual void expr_bitwise_or(Expr const & left, Expr const & right);
    FORMAST_API virtual void expr_bitwise_xor(Expr const & left, Expr const & right);
    FORMAST_API virtual void expr_compare_eq(Expr const & left, Expr const & right);
    FORMAST_API virtual void expr_compare_ne(Expr const & left, Expr const & right);
    FORMAST_API virtual void expr_compare_gt(Expr const & left, Expr const & right);
    FORMAST_API virtual void expr_compare_lt(Expr const & left, Expr const & right);
    FORMAST_API virtual void expr_compare_ge(Expr const & left, Expr const & right);
    FORMAST_API virtual void expr_compare_le(Expr const & left, Expr const & right);
    FORMAST_API virtual void expr_shift_left(Expr const & left, Expr const & right);
    FORMAST_API virtual void expr_shift_right(Expr const & left, Expr const & right);
private:
    // pimpl idiom
    FORMAST_HIDDEN class ExprVisitor;
    boost::shared_ptr<ExprVisitor> _expr_visitor;
    FORMAST_HIDDEN class ModuleVisitor;
    boost::shared_ptr<ModuleVisitor> _module_visitor;
    FORMAST_HIDDEN class StatsVisitor;
    boost::shared_ptr<StatsVisitor> _stats_visitor;
};

} // namespace formast

#endif
<|endoftext|>


<commit_before>#include <ros/ros.h>
#include <ros/package.h>
#include <std_msgs/String.h>
#include <gtest/gtest.h>
#include <sstream>
#include "RAstar_ros.h"

void spinThread(bool *cont) {
    ros::Rate loop_rate(10);

    while (ros::ok() && *cont) {
        ros::spinOnce();
        loop_rate.sleep();
    }
}

TEST(TestPlannerl, testIsCellInsideMap) {

    RAstar_planner::RAstarPlannerROS plan;
    float x = 1.0;
    float y = 1.0;
    plan.width = 10;
    plan.height = 10;
    plan.resolution = 5;

    EXPECT_EQ(true, plan.isCellInsideMap(x,y));

}

TEST(TestPlannerl, testgetCoord) {

    RAstar_planner::RAstarPlannerROS plan;

    float x = 5.0;
    float y = 7.0;
    plan.originX = 2.0;
    plan.originY = 3.0;
    plan.getCorrdinate(x,y);

    EXPECT_EQ(3.0, x);
    EXPECT_EQ(4.0,y);
}

TEST(TestPlannerl, testConvertToCellIndex) {

    RAstar_planner::RAstarPlannerROS plan;

    float x = 20.0;
    float y = 30.0;
    plan.resolution = 10.0;
    plan.width = 1.0;

    EXPECT_EQ(5, plan.convertToCellIndex(x,y));

}

TEST(TestPlannerl, testConvertToCoord) {

    RAstar_planner::RAstarPlannerROS plan;

    float x = 10.0;
    float y = 10.0;
    plan.resolution = 5.0;
    int index = 10;
    plan.originX = 2.0;
    plan.originY = 3.0;
    plan.width = 5.0;
    plan.convertToCoordinate(index,x,y);
    EXPECT_EQ(2, x);
    EXPECT_EQ(13,y);

}

TEST(TestPlannerl, testFindFreeNeighborCell) {

    RAstar_planner::RAstarPlannerROS plan;
    plan.width = 3.0;
    plan.height = 3.0;
    int id = 4;
    int mapSize = plan.width*plan.height;
    plan.OGM = new bool [mapSize];
    for (unsigned int iy = 0; iy < plan.height; iy++)
    {
      for (unsigned int ix = 0; ix < plan.width; ix++)
      {
       int cost = 0;

        if (cost == 0) {
          plan.OGM[iy*plan.width+ix]=true;
	 }
       }
    }

    int ints[] = {0,1,2,3,5,6,7,8};
    vector <int> test (ints, ints+sizeof(ints)/sizeof(int));
    vector <int> nay;
    nay =  plan.findFreeNeighborCell(id);
    EXPECT_EQ(test, nay);

}

TEST(TestPlannerl, testFindPath) {

    RAstar_planner::RAstarPlannerROS plan;
    plan.width = 5.0;
    plan.height = 5.0;
    //plan.width2 = 10.0;
    //plan.height2 = 10.0;
    float infinity = std::numeric_limits< float >::infinity();
    int startCell = 1;
    int endCell = 23;
    int mapSize = plan.width*plan.height;
    //plan.OGM;
    plan.OGM = new bool [mapSize];
    for (unsigned int iy = 0; iy < plan.height; iy++)
    {
      for (unsigned int ix = 0; ix < plan.width; ix++)
      {
       int cost = 0;

        if (cost == 0) {
          plan.OGM[iy*plan.width+ix]=true;
	  //ROS_INFO_STREAM("OGM["<<iy*plan.width+ix<<"] =" << plan.OGM[iy*plan.width+ix]);
	}
       }
    }
    //ROS_INFO_STREAM("OGM[0] = " << plan.OGM[0]);
    float *g_score = new float[mapSize];
    for (uint i=0; i<mapSize; i++)
	g_score[i]=infinity;

    int ints[] = {1,6,11,17,23};
    vector <int> test (ints, ints+sizeof(ints)/sizeof(int));
    vector<int> bpath;
    //ROS_INFO("bpath1");
    bpath =  plan.findPath(startCell,endCell,g_score);
    //ROS_INFO("bpath2");
    EXPECT_EQ(test, bpath);
}

TEST(TestPlannerl, testCalcH) {

    RAstar_planner::RAstarPlannerROS plan;
    int cell = 10;
    int goal = 30;
    float dist = 4.0;

    int cell2 = 10;
    int goal2 = 33;
    float dist2 = 7.0;
    plan.width = 5.0;
    float H = plan.calculateHCost(cell,goal);
    float H2 = plan.calculateHCost(cell2,goal2);
    EXPECT_EQ(dist, H);
    EXPECT_EQ(dist2, H2);
}

TEST(TestPlannerl, testAddNToCellOpenList) {

    RAstar_planner::RAstarPlannerROS plan;
    int neighborCell = 1;
    int goalCell = 3;
    plan.width = 5.0;
    plan.height = 5.0;
    int mapSize = plan.width*plan.height;
    float infinity = std::numeric_limits< float >::infinity();
    float *g_score = new float[mapSize];
    for (uint i=0; i<mapSize; i++)
	g_score[i]=5;
    multiset<cells> OPL;

    EXPECT_EQ(true, OPL.empty());
    plan.addNeighborCellToOpenList(OPL,neighborCell,goalCell,g_score);
    EXPECT_EQ(false, OPL.empty());
}

TEST(TestPlannerl, testIsStartGoalValid) {

    RAstar_planner::RAstarPlannerROS plan;
    int start1 = 10;
    int goal1 = 10;

    int start2 = 90;
    int goal2 = 91;

    int start3 = 90;
    int goal3 = 10;

    int start4 = 10;
    int goal4 = 90;

    int start5 = 10;
    int goal5 = 11;

    plan.width = 5;
    plan.height = 5;
    int mapSize = plan.width*plan.height;
    plan.OGM = new bool [mapSize];
    for (unsigned int iy = 0; iy < plan.height; iy++)
    {
      for (unsigned int ix = 0; ix < plan.width; ix++)
      {
       int cost = 0;

        if (cost == 0) {
          plan.OGM[iy*plan.width+ix]=true;
	}
       }
    }

    EXPECT_EQ(false,plan.isStartAndGoalCellsValid(start1,goal1) );
    EXPECT_EQ(false,plan.isStartAndGoalCellsValid(start2,goal2) );
    EXPECT_EQ(false,plan.isStartAndGoalCellsValid(start3,goal3) );
    EXPECT_EQ(false,plan.isStartAndGoalCellsValid(start4,goal4) );
    EXPECT_EQ(true ,plan.isStartAndGoalCellsValid(start5,goal5) );

}

TEST(TestPlannerl, testGetMoveCost) {

    RAstar_planner::RAstarPlannerROS plan;
    int cell1 = 1;
    int cell2 = 2;
    int cell3 = 5;
    plan.width = 5.0;
    float d1 = 1;
    float d2 = 1.4;
    EXPECT_EQ(d1, plan.getMoveCost(cell1,cell2));
    EXPECT_EQ(0, d2 - plan.getMoveCost(cell1,cell3));
    }

TEST(TestPlannerl, testIsFree) {

    RAstar_planner::RAstarPlannerROS plan;
    int id = 1;
    int i = 2;
    int j = 5;
    plan.width = 5.0;
    plan.height = 5.0;
    int mapSize = plan.width*plan.height;
    plan.OGM = new bool [mapSize];
    for (unsigned int iy = 0; iy < plan.height; iy++)
    {
      for (unsigned int ix = 0; ix < plan.width; ix++)
      {
       int cost = 0;

        if (cost == 0) {
          plan.OGM[iy*plan.width+ix]=true;
	}
       }
    }
    plan.OGM[id] = false;
    EXPECT_EQ(true, plan.isFree(i,j));
    EXPECT_EQ(false,plan.isFree(id));
    }

int main(int argc, char** argv) {
    ::testing::InitGoogleTest(&argc, argv);
    ros::init(argc, argv, "testAstar");
    ros::NodeHandle nh;

    bool cont = true;

    // spawn another thread
    boost::thread th(spinThread, &cont);

    int ret = RUN_ALL_TESTS();

    cont = false;

    // wait the second thread to finish
    th.join();

    return ret;
}
<commit_msg>Task 3.5.1 Doxygen Update<commit_after>#include <ros/ros.h>
#include <ros/package.h>
#include <std_msgs/String.h>
#include <gtest/gtest.h>
#include <sstream>
#include "RAstar_ros.h"

void spinThread(bool *cont) {
    ros::Rate loop_rate(10);

    while (ros::ok() && *cont) {
        ros::spinOnce();
        loop_rate.sleep();
    }
}

/**
 *   @brief  Test cells inside map
 *
 *   @param  none
 *   @return none
*/
TEST(TestPlannerl, testIsCellInsideMap) {

    RAstar_planner::RAstarPlannerROS plan;
    float x = 1.0;
    float y = 1.0;
    plan.width = 10;
    plan.height = 10;
    plan.resolution = 5;

    EXPECT_EQ(true, plan.isCellInsideMap(x,y));
}

/**
 *   @brief  Test getting coordinates
 *
 *   @param  none
 *   @return none
*/
TEST(TestPlannerl, testgetCoord) {

    RAstar_planner::RAstarPlannerROS plan;

    float x = 5.0;
    float y = 7.0;
    plan.originX = 2.0;
    plan.originY = 3.0;
    plan.getCorrdinate(x,y);

    EXPECT_EQ(3.0, x);
    EXPECT_EQ(4.0,y);
}

/**
 *   @brief  Test conversion to cell index
 *
 *   @param  none
 *   @return none
*/
TEST(TestPlannerl, testConvertToCellIndex) {

    RAstar_planner::RAstarPlannerROS plan;

    float x = 20.0;
    float y = 30.0;
    plan.resolution = 10.0;
    plan.width = 1.0;

    EXPECT_EQ(5, plan.convertToCellIndex(x,y));
}

/**
 *   @brief  Test conversion to coordinates
 *
 *   @param  none
 *   @return none
*/
TEST(TestPlannerl, testConvertToCoord) {

    RAstar_planner::RAstarPlannerROS plan;

    float x = 10.0;
    float y = 10.0;
    plan.resolution = 5.0;
    int index = 10;
    plan.originX = 2.0;
    plan.originY = 3.0;
    plan.width = 5.0;
    plan.convertToCoordinate(index,x,y);
    EXPECT_EQ(2, x);
    EXPECT_EQ(13,y);
}

/**
 *   @brief  Test finding free neighbors
 *
 *   @param  none
 *   @return none
*/
TEST(TestPlannerl, testFindFreeNeighborCell) {

    RAstar_planner::RAstarPlannerROS plan;
    plan.width = 3.0;
    plan.height = 3.0;
    int id = 4;
    int mapSize = plan.width*plan.height;
    plan.OGM = new bool [mapSize];
    for (unsigned int iy = 0; iy < plan.height; iy++)
    {
      for (unsigned int ix = 0; ix < plan.width; ix++)
      {
       int cost = 0;

        if (cost == 0) {
          plan.OGM[iy*plan.width+ix]=true;
	 }
       }
    }

    int ints[] = {0,1,2,3,5,6,7,8};
    vector <int> test (ints, ints+sizeof(ints)/sizeof(int));
    vector <int> nay;
    nay =  plan.findFreeNeighborCell(id);
    EXPECT_EQ(test, nay);
}

/**
 *   @brief  Test finding path
 *
 *   @param  none
 *   @return none
*/
TEST(TestPlannerl, testFindPath) {

    RAstar_planner::RAstarPlannerROS plan;
    plan.width = 5.0;
    plan.height = 5.0;
    //plan.width2 = 10.0;
    //plan.height2 = 10.0;
    float infinity = std::numeric_limits< float >::infinity();
    int startCell = 1;
    int endCell = 23;
    int mapSize = plan.width*plan.height;
    //plan.OGM;
    plan.OGM = new bool [mapSize];
    for (unsigned int iy = 0; iy < plan.height; iy++)
    {
      for (unsigned int ix = 0; ix < plan.width; ix++)
      {
       int cost = 0;

        if (cost == 0) {
          plan.OGM[iy*plan.width+ix]=true;
	  //ROS_INFO_STREAM("OGM["<<iy*plan.width+ix<<"] =" << plan.OGM[iy*plan.width+ix]);
	}
       }
    }
    //ROS_INFO_STREAM("OGM[0] = " << plan.OGM[0]);
    float *g_score = new float[mapSize];
    for (uint i=0; i<mapSize; i++)
	g_score[i]=infinity;

    int ints[] = {1,6,11,17,23};
    vector <int> test (ints, ints+sizeof(ints)/sizeof(int));
    vector<int> bpath;
    //ROS_INFO("bpath1");
    bpath =  plan.findPath(startCell,endCell,g_score);
    //ROS_INFO("bpath2");
    EXPECT_EQ(test, bpath);
}

/**
 *   @brief  Test caluclating Heuristic
 *
 *   @param  none
 *   @return none
*/
TEST(TestPlannerl, testCalcH) {

    RAstar_planner::RAstarPlannerROS plan;
    int cell = 10;
    int goal = 30;
    float dist = 4.0;

    int cell2 = 10;
    int goal2 = 33;
    float dist2 = 7.0;
    plan.width = 5.0;
    float H = plan.calculateHCost(cell,goal);
    float H2 = plan.calculateHCost(cell2,goal2);
    EXPECT_EQ(dist, H);
    EXPECT_EQ(dist2, H2);
}

/**
 *   @brief  Test adding cell to open list
 *
 *   @param  none
 *   @return none
*/
TEST(TestPlannerl, testAddNToCellOpenList) {

    RAstar_planner::RAstarPlannerROS plan;
    int neighborCell = 1;
    int goalCell = 3;
    plan.width = 5.0;
    plan.height = 5.0;
    int mapSize = plan.width*plan.height;
    float infinity = std::numeric_limits< float >::infinity();
    float *g_score = new float[mapSize];
    for (uint i=0; i<mapSize; i++)
	g_score[i]=5;
    multiset<cells> OPL;

    EXPECT_EQ(true, OPL.empty());
    plan.addNeighborCellToOpenList(OPL,neighborCell,goalCell,g_score);
    EXPECT_EQ(false, OPL.empty());
}

/**
 *   @brief  Test start/goal locations being valid
 *
 *   @param  none
 *   @return none
*/
TEST(TestPlannerl, testIsStartGoalValid) {

    RAstar_planner::RAstarPlannerROS plan;
    int start1 = 10;
    int goal1 = 10;

    int start2 = 90;
    int goal2 = 91;

    int start3 = 90;
    int goal3 = 10;

    int start4 = 10;
    int goal4 = 90;

    int start5 = 10;
    int goal5 = 11;

    plan.width = 5;
    plan.height = 5;
    int mapSize = plan.width*plan.height;
    plan.OGM = new bool [mapSize];
    for (unsigned int iy = 0; iy < plan.height; iy++)
    {
      for (unsigned int ix = 0; ix < plan.width; ix++)
      {
       int cost = 0;

        if (cost == 0) {
          plan.OGM[iy*plan.width+ix]=true;
	}
       }
    }

    EXPECT_EQ(false,plan.isStartAndGoalCellsValid(start1,goal1) );
    EXPECT_EQ(false,plan.isStartAndGoalCellsValid(start2,goal2) );
    EXPECT_EQ(false,plan.isStartAndGoalCellsValid(start3,goal3) );
    EXPECT_EQ(false,plan.isStartAndGoalCellsValid(start4,goal4) );
    EXPECT_EQ(true ,plan.isStartAndGoalCellsValid(start5,goal5) );

}

/**
 *   @brief  Test getting move costs
 *
 *   @param  none
 *   @return none
*/
TEST(TestPlannerl, testGetMoveCost) {

    RAstar_planner::RAstarPlannerROS plan;
    int cell1 = 1;
    int cell2 = 2;
    int cell3 = 5;
    plan.width = 5.0;
    float d1 = 1;
    float d2 = 1.4;
    EXPECT_EQ(d1, plan.getMoveCost(cell1,cell2));
    EXPECT_EQ(0, d2 - plan.getMoveCost(cell1,cell3));
    }

/**
 *   @brief  Test check if neighbor free
 *
 *   @param  none
 *   @return none
*/
TEST(TestPlannerl, testIsFree) {

    RAstar_planner::RAstarPlannerROS plan;
    int id = 1;
    int i = 2;
    int j = 5;
    plan.width = 5.0;
    plan.height = 5.0;
    int mapSize = plan.width*plan.height;
    plan.OGM = new bool [mapSize];
    for (unsigned int iy = 0; iy < plan.height; iy++)
    {
      for (unsigned int ix = 0; ix < plan.width; ix++)
      {
       int cost = 0;

        if (cost == 0) {
          plan.OGM[iy*plan.width+ix]=true;
	}
       }
    }
    plan.OGM[id] = false;
    EXPECT_EQ(true, plan.isFree(i,j));
    EXPECT_EQ(false,plan.isFree(id));
    }

/*
 *   @brief  unit test entrypoint
 *
 *   @param  number of arguments
 *   @param  argument character array
 *   @return integer 0 upon exit success
*/
int main(int argc, char** argv) {
    ::testing::InitGoogleTest(&argc, argv);
    ros::init(argc, argv, "testAstar");
    ros::NodeHandle nh;

    bool cont = true;

    // spawn another thread
    boost::thread th(spinThread, &cont);

    int ret = RUN_ALL_TESTS();

    cont = false;

    // wait the second thread to finish
    th.join();

    return ret;
}
<|endoftext|>


<commit_before>#include "ride/projectsettingsdlg.h"

#include "ride/generated/ui.h"
#include "ride/project.h"
#include "ride/mainwindow.h"
#include <wx/fontenum.h>
#include "ride/wxutils.h"
#include "ride/enabledisable.h"
#include "ride/form.h"
#include "ride/cargo.h"
#include "wx/editlbox.h"

class ProjectSettingsDlg : public ui::ProjectSettings
{
public:
  ProjectSettingsDlg(wxWindow* parent, MainWindow* mainwindow, Project* project);

protected:
  void OnApply(wxCommandEvent& event);
  void OnCancel(wxCommandEvent& event);
  void OnOk(wxCommandEvent& event);

protected:
  void AllToGui(bool togui);
  void CargoToGui(bool togui);

protected:
  Cargo cargo_;

private:
  MainWindow* main_window_;
  Project* project_;
};

void DoProjectSettingsDlg(wxWindow* parent, MainWindow* mainwindow, Project* project)
{
  ProjectSettingsDlg dlg(parent, mainwindow, project);
  dlg.ShowModal();
}

bool LoadCargoFile(const wxString& cargo_file, Cargo* cargo, wxStaticText* error_display) {
  if (cargo_file.IsEmpty()) {
    error_display->SetLabelText("No project loaded");
    return false;
  }
  const auto result = cargo->Load(cargo_file);
  if (result.IsOk())
  {
    error_display->SetLabelText("");
    return true;
  }

  error_display->SetLabelText(result.message());
  return false;
}

ProjectSettingsDlg::ProjectSettingsDlg(wxWindow* parent, MainWindow* mainwindow, Project* project) :
::ui::ProjectSettings(parent, wxID_ANY), main_window_(mainwindow), project_(project)
{
  // uiCargoAuthors = new wxEditableListBox(uiCargoAuthorsContainer, wxID_ANY, "", wxDefaultPosition, wxDefaultPosition, wxEL_DEFAULT_STYLE);

  if (false == LoadCargoFile(project_->GetCargoFile(), &cargo_, uiCargoLoadError)) {
    EnableDisable(false)
      (uiCargoName)
      (uiCargoVersion)
      (uiCargoAuthors)
      ;
  }
  AllToGui(true);
}

void ProjectSettingsDlg::OnApply( wxCommandEvent& event )
{
  AllToGui(false);
}

void ProjectSettingsDlg::OnCancel( wxCommandEvent& event )
{
  EndModal(wxCANCEL);
}

void ProjectSettingsDlg::OnOk( wxCommandEvent& event )
{
  AllToGui(false);
  EndModal(wxOK);
}

void ProjectSettingsDlg::AllToGui(bool togui) {
  CargoToGui(togui);
}

//////////////////////////////////////////////////////////////////////////

void ProjectSettingsDlg::CargoToGui(bool togui) {
  DIALOG_DATA(cargo_, name, uiCargoName, _Str);
  DIALOG_DATA(cargo_, version, uiCargoVersion, _Str);
  DIALOG_DATA(cargo_, authors, uiCargoAuthors,);
}<commit_msg>can't save toml files, redisigning gui<commit_after>#include "ride/projectsettingsdlg.h"

#include "ride/generated/ui.h"
#include "ride/project.h"
#include "ride/mainwindow.h"
#include <wx/fontenum.h>
#include "ride/wxutils.h"
#include "ride/enabledisable.h"
#include "ride/form.h"
#include "ride/cargo.h"
#include "wx/editlbox.h"

class ProjectSettingsDlg : public ui::ProjectSettings
{
public:
  ProjectSettingsDlg(wxWindow* parent, MainWindow* mainwindow, Project* project);

protected:
  void OnApply(wxCommandEvent& event);
  void OnCancel(wxCommandEvent& event);
  void OnOk(wxCommandEvent& event);

protected:
  bool Apply();
  void AllToGui(bool togui);
  void CargoToGui(bool togui);

protected:
  Cargo cargo_;

private:
  MainWindow* main_window_;
  Project* project_;
};

void DoProjectSettingsDlg(wxWindow* parent, MainWindow* mainwindow, Project* project)
{
  ProjectSettingsDlg dlg(parent, mainwindow, project);
  dlg.ShowModal();
}

bool LoadCargoFile(const wxString& cargo_file, Cargo* cargo, wxStaticText* error_display) {
  if (cargo_file.IsEmpty()) {
    error_display->SetLabelText("No project loaded");
    return false;
  }
  const auto result = cargo->Load(cargo_file);
  if (result.IsOk())
  {
    error_display->SetLabelText("");
    return true;
  }

  error_display->SetLabelText(result.message());
  return false;
}

ProjectSettingsDlg::ProjectSettingsDlg(wxWindow* parent, MainWindow* mainwindow, Project* project) :
::ui::ProjectSettings(parent, wxID_ANY), main_window_(mainwindow), project_(project)
{
  LoadCargoFile(project_->GetCargoFile(), &cargo_, uiCargoLoadError);

  AllToGui(true);

  // disable all cargo related gui as we can't save toml files yet
  EnableDisable(false)
    (uiCargoName)
    (uiCargoVersion)
    (uiCargoAuthors)
    ;
}

void ProjectSettingsDlg::OnApply( wxCommandEvent& event )
{
  Apply();
}

void ProjectSettingsDlg::OnCancel( wxCommandEvent& event )
{
  EndModal(wxCANCEL);
}

void ProjectSettingsDlg::OnOk( wxCommandEvent& event )
{
  if (Apply()) {
    EndModal(wxOK);
  }
}

void ProjectSettingsDlg::AllToGui(bool togui) {
  CargoToGui(togui);
}

//////////////////////////////////////////////////////////////////////////

bool ProjectSettingsDlg::Apply() {
  AllToGui(false);

  return true;
}

//////////////////////////////////////////////////////////////////////////

void ProjectSettingsDlg::CargoToGui(bool togui) {
  DIALOG_DATA(cargo_, name, uiCargoName, _Str);
  DIALOG_DATA(cargo_, version, uiCargoVersion, _Str);
  DIALOG_DATA(cargo_, authors, uiCargoAuthors,);
}<|endoftext|>