text:
/*
 *            Copyright 2009-2020 The VOTCA Development Team
 *                       (http://www.votca.org)
 *
 *      Licensed under the Apache License, Version 2.0 (the "License")
 *
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *              http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

// Third party includes
#include <boost/format.hpp>

// VOTCA includes
#include <votca/tools/elements.h>

// Local VOTCA includes
#include "votca/xtp/atom.h"
#include "votca/xtp/forces.h"
#include "votca/xtp/statetracker.h"

namespace votca {
namespace xtp {

using std::flush;
void Forces::Initialize(tools::Property& options) {

  std::vector<std::string> choices = {"forward", "central"};
  _force_method =
      options.ifExistsAndinListReturnElseThrowRuntimeError<std::string>(
          ".method", choices);

  _displacement = options.ifExistsReturnElseReturnDefault<double>(
      ".displacement", 0.001);  // Angstrom
  _displacement *= tools::conv::ang2bohr;

  _remove_total_force = options.ifExistsReturnElseReturnDefault<bool>(
      ".CoMforce_removal", _remove_total_force);
  return;
}

void Forces::Calculate(const Orbitals& orbitals) {

  Index natoms = orbitals.QMAtoms().size();
  _forces = Eigen::MatrixX3d::Zero(natoms, 3);

  for (Index atom_index = 0; atom_index < natoms; atom_index++) {

    XTP_LOG(Log::debug, *_pLog)
        << "FORCES--DEBUG working on atom " << atom_index << flush;
    Eigen::Vector3d atom_force = Eigen::Vector3d::Zero();
    // Calculate Force on this atom
    if (_force_method == "forward") {
      atom_force = NumForceForward(orbitals, atom_index);
    }
    if (_force_method == "central") {
      atom_force = NumForceCentral(orbitals, atom_index);
    }
    _forces.row(atom_index) = atom_force.transpose();
  }
  if (_remove_total_force) {
    RemoveTotalForce();
  }
  return;
}

void Forces::Report() const {

  XTP_LOG(Log::error, *_pLog)
      << (boost::format(" ---- FORCES (Hartree/Bohr)   ")).str() << flush;
  XTP_LOG(Log::error, *_pLog)
      << (boost::format("      %1$s differences   ") % _force_method).str()
      << flush;
  XTP_LOG(Log::error, *_pLog)
      << (boost::format("      displacement %1$1.4f Angstrom   ") %
          (_displacement * tools::conv::bohr2ang))
             .str()
      << flush;
  XTP_LOG(Log::error, *_pLog)
      << (boost::format(" Atom\t x\t  y\t  z ")).str() << flush;

  for (Index i = 0; i < _forces.rows(); i++) {
    XTP_LOG(Log::error, *_pLog)
        << (boost::format("%1$4d    %2$+1.4f  %3$+1.4f  %4$+1.4f") % i %
            _forces(i, 0) % _forces(i, 1) % _forces(i, 2))
               .str()
        << flush;
  }
  return;
}

Eigen::Vector3d Forces::NumForceForward(Orbitals orbitals, Index atom_index) {
  Eigen::Vector3d force = Eigen::Vector3d::Zero();
  // get this atoms's current coordinates
  double energy_center =
      orbitals.getTotalStateEnergy(_tracker.CalcState(orbitals));
  const Eigen::Vector3d current_pos = orbitals.QMAtoms()[atom_index].getPos();
  for (Index i_cart = 0; i_cart < 3; i_cart++) {
    Eigen::Vector3d displacement_vec = Eigen::Vector3d::Zero();
    displacement_vec[i_cart] = _displacement;
    // update the coordinate
    Eigen::Vector3d pos_displaced = current_pos + displacement_vec;
    orbitals.QMAtoms()[atom_index].setPos(pos_displaced);
    _gwbse_engine.ExcitationEnergies(orbitals);
    double energy_displaced =
        orbitals.getTotalStateEnergy(_tracker.CalcState(orbitals));
    force(i_cart) = (energy_center - energy_displaced) / _displacement;
    orbitals.QMAtoms()[atom_index].setPos(
        current_pos);  // restore original coordinate into segment
  }                    // Cartesian directions
  return force;
}

Eigen::Vector3d Forces::NumForceCentral(Orbitals orbitals, Index atom_index) {
  Eigen::Vector3d force = Eigen::Vector3d::Zero();
  const Eigen::Vector3d current_pos = orbitals.QMAtoms()[atom_index].getPos();
  for (Index i_cart = 0; i_cart < 3; i_cart++) {
    XTP_LOG(Log::debug, *_pLog)
        << "FORCES--DEBUG           Cartesian component " << i_cart << flush;
    Eigen::Vector3d displacement_vec = Eigen::Vector3d::Zero();
    displacement_vec[i_cart] = _displacement;
    // update the coordinate
    Eigen::Vector3d pos_displaced = current_pos + displacement_vec;
    orbitals.QMAtoms()[atom_index].setPos(pos_displaced);
    _gwbse_engine.ExcitationEnergies(orbitals);
    double energy_displaced_plus =
        orbitals.getTotalStateEnergy(_tracker.CalcState(orbitals));
    // update the coordinate
    pos_displaced = current_pos - displacement_vec;
    orbitals.QMAtoms()[atom_index].setPos(pos_displaced);
    _gwbse_engine.ExcitationEnergies(orbitals);
    double energy_displaced_minus =
        orbitals.getTotalStateEnergy(_tracker.CalcState(orbitals));
    force(i_cart) =
        0.5 * (energy_displaced_minus - energy_displaced_plus) / _displacement;
    orbitals.QMAtoms()[atom_index].setPos(
        current_pos);  // restore original coordinate into orbital
  }
  return force;
}

void Forces::RemoveTotalForce() {
  Eigen::Vector3d avgtotal_force =
      _forces.colwise().sum() / double(_forces.rows());
  for (Index i_atom = 0; i_atom < _forces.rows(); i_atom++) {
    _forces.row(i_atom) -= avgtotal_force;
  }
  return;
}

}  // namespace xtp
}  // namespace votca

max_stars_repo_name: fossabot/xtp

max_issues_repo_name: fossabot/xtp

max_forks_repo_name: fossabot/xtp


text:
#define BOOST_TEST_MODULE TestSolvers
#include <boost/test/unit_test.hpp>
#include <amgcl/backend/blaze.hpp>

#include "test_solver.hpp"

BOOST_AUTO_TEST_SUITE( test_solvers )

BOOST_AUTO_TEST_CASE(test_blaze_backend)
{
    test_backend< amgcl::backend::blaze<double> >();
}

BOOST_AUTO_TEST_SUITE_END()

ext: cpp

max_stars_repo_name: tenglongcong/amgcl

max_issues_repo_name: tenglongcong/amgcl

max_forks_repo_name: tenglongcong/amgcl


text:
#ifndef __UTILITY_FILE_H__
#define __UTILITY_FILE_H__

#include <iostream>
#include <fstream>
#include <map>
#include <ctype.h>

#include <strips_prob.hxx>
#include <fluent.hxx>
#include <action.hxx>
#include <cond_eff.hxx>
#include <strips_state.hxx>
#include <fwd_search_prob.hxx>

#include <boost/algorithm/string.hpp>
#include <string>
#include <aptk/string_conversions.hxx>

void clone_problem(const aptk::STRIPS_Problem& prob, aptk::STRIPS_Problem& trans_prob, std::vector<unsigned> init_state);
//std::string print_fluent_vec(aptk::STRIPS_Problem& prob, std::vector<unsigned int> f_v);
//std::string print_action_vec(aptk::STRIPS_Problem& prob, std::vector<unsigned int> a_v);

std::string print_fluent_vec(const aptk::STRIPS_Problem& prob, std::vector<unsigned int> f_v);
std::string print_action_vec(const aptk::STRIPS_Problem& prob, std::vector<unsigned int> a_v);

template<class T>
bool element_of(T e, std::vector<T> es){
  return std::find(es.begin(), es.end(), e) != es.end();
}

template<class T>
void print_vector(std::vector<T> v){
  for (unsigned int i = 0; i <  v.size(); i++){
    std::cout << i << ": " << std::to_string(v[i]) << std::endl;
  }
}

template<class T>
std::string vector_as_string(std::vector<T> v, std::string del){

  std::stringstream s;

  for (unsigned int i = 0; i <  v.size(); i++){
    if(i != v.size()-1)
      s << std::to_string(v[i]) << del;
    else
      s << std::to_string(v[i]);
  }

  return s.str();
}

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
std::vector<double> normalize_vector(std::vector<T> v){
	std::vector<double> n_v;
	double sum =  (double) std::accumulate(v.begin(), v.end(), (T) 0);

  if(sum == 0){
    for (unsigned i = 0; i < v.size(); i++)
      n_v.push_back((double) 1.0f / ((double) v.size()));
  }else{

    for(T e : v){
      n_v.push_back( ((double) e) / sum);
    }
  }

	return n_v;

}

#endif //UTILITY_FILE_H_

ext: hxx


text:
#include <iostream>
#include <vector>
#include <string>
#include <complex>

#include <boost/program_options.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>

#include <amgcl/backend/builtin.hpp>
#include <amgcl/value_type/complex.hpp>
#include <amgcl/adapter/crs_tuple.hpp>

#include <amgcl/mpi/util.hpp>
#include <amgcl/mpi/make_solver.hpp>
#include <amgcl/mpi/preconditioner.hpp>
#include <amgcl/mpi/solver/runtime.hpp>

#include <amgcl/io/mm.hpp>
#include <amgcl/io/binary.hpp>
#include <amgcl/profiler.hpp>

namespace amgcl {
    profiler<> prof;
}

namespace math = amgcl::math;

//---------------------------------------------------------------------------
ptrdiff_t assemble_poisson3d(amgcl::mpi::communicator comm,
        ptrdiff_t n, int block_size,
        std::vector<ptrdiff_t> &ptr,
        std::vector<ptrdiff_t> &col,
        std::vector<std::complex<double>> &val,
        std::vector<std::complex<double>> &rhs)
{
    ptrdiff_t n3 = n * n * n;

    ptrdiff_t chunk = (n3 + comm.size - 1) / comm.size;
    if (chunk % block_size != 0) {
        chunk += block_size - chunk % block_size;
    }
    ptrdiff_t row_beg = std::min(n3, chunk * comm.rank);
    ptrdiff_t row_end = std::min(n3, row_beg + chunk);
    chunk = row_end - row_beg;

    ptr.clear(); ptr.reserve(chunk + 1);
    col.clear(); col.reserve(chunk * 7);
    val.clear(); val.reserve(chunk * 7);

    rhs.resize(chunk);
    std::fill(rhs.begin(), rhs.end(), 1.0);

    const double h2i = (n - 1) * (n - 1);
    ptr.push_back(0);

    for (ptrdiff_t idx = row_beg; idx < row_end; ++idx) {
        ptrdiff_t k = idx / (n * n);
        ptrdiff_t j = (idx / n) % n;
        ptrdiff_t i = idx % n;

        if (k > 0)  {
            col.push_back(idx - n * n);
            val.push_back(-h2i);
        }

        if (j > 0)  {
            col.push_back(idx - n);
            val.push_back(-h2i);
        }

        if (i > 0) {
            col.push_back(idx - 1);
            val.push_back(-h2i);
        }

        col.push_back(idx);
        val.push_back(6 * h2i);

        if (i + 1 < n) {
            col.push_back(idx + 1);
            val.push_back(-h2i);
        }

        if (j + 1 < n) {
            col.push_back(idx + n);
            val.push_back(-h2i);
        }

        if (k + 1 < n) {
            col.push_back(idx + n * n);
            val.push_back(-h2i);
        }

        ptr.push_back( col.size() );
    }

    return chunk;
}

//---------------------------------------------------------------------------
void solve_scalar(
        amgcl::mpi::communicator comm,
        ptrdiff_t chunk,
        const std::vector<ptrdiff_t> &ptr,
        const std::vector<ptrdiff_t> &col,
        const std::vector<std::complex<double>> &val,
        const boost::property_tree::ptree &prm,
        const std::vector<std::complex<double>> &rhs
        )
{
    typedef amgcl::backend::builtin<std::complex<double>> Backend;

    typedef
        amgcl::mpi::make_solver<
            amgcl::runtime::mpi::preconditioner<Backend>,
            amgcl::runtime::mpi::solver::wrapper<Backend>
            >
        Solver;

    using amgcl::prof;

    prof.tic("setup");
    Solver solve(comm, std::tie(chunk, ptr, col, val), prm);
    prof.toc("setup");

    if (comm.rank == 0) {
        std::cout << solve << std::endl;
    }

    std::vector<std::complex<double>> x(chunk);

    int    iters;
    double error;

    prof.tic("solve");
    std::tie(iters, error) = solve(rhs, x);
    prof.toc("solve");

    if (comm.rank == 0) {
        std::cout
            << "Iterations: " << iters << std::endl
            << "Error:      " << error << std::endl
            << prof << std::endl;
    }
}

//---------------------------------------------------------------------------
int main(int argc, char *argv[]) {
    amgcl::mpi::init_thread mpi(&argc, &argv);
    amgcl::mpi::communicator comm(MPI_COMM_WORLD);

    if (comm.rank == 0)
        std::cout << "World size: " << comm.size << std::endl;

    using amgcl::prof;

    // Read configuration from command line
    namespace po = boost::program_options;
    po::options_description desc("Options");

    desc.add_options()
        ("help,h", "show help")
        (
         "size,n",
         po::value<ptrdiff_t>()->default_value(128),
         "domain size"
        )
        ("prm-file,P",
         po::value<std::string>(),
         "Parameter file in json format. "
        )
        (
         "prm,p",
         po::value< std::vector<std::string> >()->multitoken(),
         "Parameters specified as name=value pairs. "
         "May be provided multiple times. Examples:\n"
         "  -p solver.tol=1e-3\n"
         "  -p precond.coarse_enough=300"
        )
        ;

    po::positional_options_description p;
    p.add("prm", -1);

    po::variables_map vm;
    po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
    po::notify(vm);

    if (vm.count("help")) {
        if (comm.rank == 0) std::cout << desc << std::endl;
        return 0;
    }

    boost::property_tree::ptree prm;
    if (vm.count("prm-file")) {
        read_json(vm["prm-file"].as<std::string>(), prm);
    }

    if (vm.count("prm")) {
        for(const std::string &v : vm["prm"].as<std::vector<std::string> >()) {
            amgcl::put(prm, v);
        }
    }

    ptrdiff_t n;
    std::vector<ptrdiff_t> ptr;
    std::vector<ptrdiff_t> col;
    std::vector<std::complex<double>> val;
    std::vector<std::complex<double>> rhs;

    prof.tic("assemble");
    n = assemble_poisson3d(comm, vm["size"].as<ptrdiff_t>(), 1, ptr, col, val, rhs);
    prof.toc("assemble");

    solve_scalar(comm, n, ptr, col, val, prm, rhs);
}

max_stars_repo_name: ivhak/amgcl

max_issues_repo_name: ivhak/amgcl

max_forks_repo_name: ivhak/amgcl


text:
//| This file is a part of the sferes2 framework.
//| Copyright 2009, ISIR / Universite Pierre et Marie Curie (UPMC)
//| Main contributor(s): Jean-Baptiste Mouret, mouret@isir.fr
//|
//| This software is a computer program whose purpose is to facilitate
//| experiments in evolutionary computation and evolutionary robotics.
//|
//| This software is governed by the CeCILL license under French law
//| and abiding by the rules of distribution of free software.  You
//| can use, modify and/ or redistribute the software under the terms
//| of the CeCILL license as circulated by CEA, CNRS and INRIA at the
//| following URL "http://www.cecill.info".
//|
//| As a counterpart to the access to the source code and rights to
//| copy, modify and redistribute granted by the license, users are
//| provided only with a limited warranty and the software's author,
//| the holder of the economic rights, and the successive licensors
//| have only limited liability.
//|
//| In this respect, the user's attention is drawn to the risks
//| associated with loading, using, modifying and/or developing or
//| reproducing the software by the user in light of its specific
//| status of free software, that may mean that it is complicated to
//| manipulate, and that also therefore means that it is reserved for
//| developers and experienced professionals having in-depth computer
//| knowledge. Users are therefore encouraged to load and test the
//| software's suitability as regards their requirements in conditions
//| enabling the security of their systems and/or data to be ensured
//| and, more generally, to use and operate it in the same conditions
//| as regards security.
//|
//| The fact that you are presently reading this means that you have
//| had knowledge of the CeCILL license and that you accept its terms.

#ifndef FITNESS_SIMU_HPP
#define FITNESS_SIMU_HPP

#include <sferes/stc.hpp>
#include <sferes/fit/fitness.hpp>
#include <iostream>
#include <boost/foreach.hpp>

#define SFERES_FITNESS_SIMU(Class, Parent)					\
  template <typename Simu, typename Agent, typename Params, typename Exact = stc::Itself> \
  class Class : public Parent<Simu, Agent, Params, typename stc::FindExact<Class<Simu, Agent, Params, Exact>, Exact>::ret>

namespace sferes {
  namespace fit {
    namespace state {
      enum state_t { not_started, running, end_exp, end_eval, fast_fw, eval_done };
    }

    template<typename Simu, typename Agent, typename Params, typename Exact = stc::Itself>
    class FitnessSimu : public stc::Any<Exact> {
     public:
      typedef Simu simu_t;
      FitnessSimu() :
        _step(0),
        _exp_step(0),
        _nb_exps(0),
        _value(0.0f),
        _state(state::not_started),
        _mode(mode::eval) {
      }

      template<typename Phen>
      void eval(Phen& p) {

        dbg::out(dbg::info, "fit")<<"eval mode="<<this->mode()<<" (mode view="<<mode::view<<")"<<std::endl;

        if (this->mode() == mode::view)
          _simu.init_view();
        if (_state == state::eval_done)
          return;
        _state = state::not_started;
        while (_state != state::end_eval)
          _exp(p);
        _state = state::eval_done;
      }
      template<typename Phen>
      int refresh(Phen& p) {
        return stc::exact(this)->refresh(p);
      }
      template<typename Phen>
      void refresh_end_exp(Phen& p) {
        dbg::trace t1("fit", DBG_HERE);
        stc::exact(this)->refresh_end_exp(p);
      }
      template<typename Phen>
      void refresh_end_eval(Phen& p) {
        dbg::trace t1("fit", DBG_HERE);
        stc::exact(this)->refresh_end_eval(p);
      }
      template<typename Phen>
      void scheduler(Phen& p) {
        dbg::trace t1("fit", DBG_HERE);
        stc::exact(this)->scheduler(p);
      }

      template<typename Phen>
      void new_exp(Phen& p) {
        dbg::out(dbg::info, "fit")<<"new_exp, _step="<<_step<<std::endl;
        _state = state::running;
        _exp_step = 0;
      }
      template<typename Phen>
      void end_exp(Phen& p) {
        dbg::out(dbg::info, "fit")<<"end_exp, _step="<<_step<<std::endl;
        assert(_state == state::running || _state == state::fast_fw);
        _state = state::end_exp;
        refresh_end_exp(p);
        _exp_step = 0;
        ++_nb_exps;
      }
      template<typename Phen>
      void end_eval(Phen& p) {
        assert(_state == state::end_exp);
        dbg::out(dbg::info, "fit")<<"end_eval, _step="<<_step<<std::endl;
        _state = state::end_eval;
        refresh_end_eval(p);
        if (!_objs.empty()) {
          BOOST_FOREACH(float v, _objs)
          _value += v;
          _value /= _objs.size();
        }
      }

      size_t step() const {
        return _step;
      }
      size_t exp_step() const {
        return _exp_step;
      }
      // number of finished experiments
      size_t nb_exps() const {
        return _nb_exps;
      }

      const Simu& simu() const {
        return _simu;
      }
      Simu& simu() {
        return _simu;
      }

      const Agent& agent() const {
        return _agent;
      }
      Agent& agent() {
        return _agent;
      }

      float value() const {
        return _value;
      }
      const std::vector<float>& objs() const {
        assert(!_objs.empty());
        return _objs;
      }
      float obj(size_t i) const {
        assert(i < _objs.size());
        return _objs[i];
      }

      template<class Archive>
      void serialize(Archive & ar, const unsigned int version) {
        ar & BOOST_SERIALIZATION_NVP(_value);
        ar & BOOST_SERIALIZATION_NVP(_objs);
      }

      void set_mode(mode::mode_t m) {
        _mode = m;
      }
      mode::mode_t mode() const {
        return _mode;
      }

     protected:
      Simu _simu;
      Agent _agent;
      int _step;
      size_t _exp_step;
      size_t _nb_exps;
      std::vector<float> _objs;
      float _value;
      state::state_t _state;
      mode::mode_t _mode;

      template<typename Phen>
      void _exp(Phen& p) {
        dbg::out(dbg::tracing, "fit")<<"starting _step = "
                                     <<_step<<" state="<<_state<<std::endl;
        _simu.init();
        _agent.init(p);
        //	_agent.refresh_params(p);
        _state = state::not_started;
        while(_state != state::end_exp
              && _state != state::end_eval) {
          scheduler(p);
          _simu.refresh();
          if (mode() == mode::view)
            _simu.refresh_view();
          _agent.refresh(_simu, p);// call refresh param only the
          // first time
          int res = refresh(p);
          ++_step;
          ++_exp_step;
          if (res == -1)
            _goto_next_exp(p);
        }
      }
      template<typename Phen>
      void _goto_next_exp(Phen& p) {
        dbg::trace t1("fit", DBG_HERE);
        dbg::out(dbg::tracing, "fit")<<"exp stopped, _step = "<<_step<<" state="<<_state<<std::endl;
        while(_state != state::end_exp && _state != state::end_eval) {
          _state = state::fast_fw;
          scheduler(p);
          ++_step;
        }
      }

    };

#define AT(K) if (this->_state == ::sferes::fit::state::running	\
		  && this->_step == K)

#define EVERY(K, E) assert(K != 0);				\
    if (this->_state == ::sferes::fit::state::running		\
	&& ((this->_step + K) % E) == 0)

#define SFERES_SCHEDULER()			\
    template<typename Phen>			\
    void scheduler(Phen& p)

#define NEW_EXP(K) { if (this->_step == K) this->new_exp(p); }
#define END_EXP(K) { if (this->_step == K) this->end_exp(p); }
#define END_EVAL(K) { if (this->_step == K) this->end_eval(p); }

    SFERES_FITNESS_SIMU(FitnessSimuDummy, FitnessSimu) {
    public:
      template<typename Phen>
      int refresh(Phen& p) {
        return 0;
      }
      template<typename Phen>
      void refresh_end_exp(Phen& p) { }
      template<typename Phen>
      void refresh_end_eval(Phen& p) {}
      SFERES_SCHEDULER() {
        NEW_EXP(0);
        END_EXP(1);
        END_EVAL(1);
      }
    };

  }
};

#endif

ext: hpp

max_stars_repo_name: Evolving-AI-Lab/innovation-engine

max_issues_repo_name: Evolving-AI-Lab/innovation-engine

max_forks_repo_name: Evolving-AI-Lab/innovation-engine


text:
/*
 * File:   Sf1DriverBase.hpp
 * Author: Paolo D'Apice
 *
 * Created on March 1, 2012, 10:23 AM
 */

#ifndef SF1DRIVER_BASE_HPP
#define	SF1DRIVER_BASE_HPP

#include "config.h"
#include "Errors.hpp"
#include "Sf1Config.hpp"
#include <boost/noncopyable.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/asio/io_service.hpp>
#include <boost/asio/ip/tcp.hpp>

NS_IZENELIB_SF1R_BEGIN

namespace ba = boost::asio;

class PoolFactory;
class RawClient;
class Writer;

namespace {

/**
 * Max sequence number.
 * Valued 4294967294 as defined in the Ruby client:
 * \code
 * MAX_SEQUENCE = (1 << 31) - 2
 * \endcode
 * @see \ref limits
 */
const uint32_t MAX_SEQUENCE = std::numeric_limits<uint32_t>::max() - 1;

}

/**
 * Base class for SF1 driver.
 */
class Sf1DriverBase : private boost::noncopyable {
public:

    /// Available data formats for request/response body.
    enum Format {
        JSON
    };

    /**
     * Initializes the pool factory and the data writer.
     * @param parameters configuration parameters.
     * @param format The format of request/response body (defaults to JSON).
     */
    Sf1DriverBase(const Sf1Config& parameters, const Format& format = JSON);

    /// Destructor.
    virtual ~Sf1DriverBase();

    /**
     * Sends a synchronous request to a running SF1 and get the response.
     * The format of both the request and the response is the one defined
     * in the constructor.
     * Usage example:
     * \code
     * Sf1Driver driver(host, port);
     * std::string response = driver.call(request);
     * \endcode
     * @param[in] uri Requested URI.
     * @param[in] tokens Custom tokens.
     * @param[in,out] request Request body.
     * @return The response body.
     * @throw ClientError if errors due to client request occur.
     * @throw ConnectionPoolError if there is no connection available.
     * @throw ServerError if errors due to server response occur.
     * @throw NetworkError if cannot connect to the server.
     */
    virtual std::string call(const std::string& uri, const std::string& tokens,
                             std::string& request) = 0;

    /**
     * @return The sequence number of the next request.
     * Note that if the request is discarded due to client error
     * (i.e. \ref call throws \ref ClientError, the sequence number
     * is not incremented because no request has been sent to the server.
     */
    uint32_t getSequence() const {
        return sequence;
    }

    /**
     * @return The actual data format.
     */
    Format getFormat() const {
        return format;
    }

    /**
     * @return The actual data format (string).
     */
    std::string getFormatString() const {
        switch (format) {
        case JSON:
        default:
            return "JSON";
    }
}

protected:

    /* pure virtual methods */

    /// Acquire a connection to the SF1 according to the given collection.
    virtual RawClient& acquire(const std::string& collection) const = 0;

    /// Release the given connection.
    virtual void release(const RawClient& connection) const = 0;

    /* basic methods (private API) */

    /**
     * Parse the uri for controller and action.
     * @param[in] uri The request URI.
     * @param[out] controller The request controller.
     * @param[out] action The request action (may be empty).
     * @throw ClientError if the controller is not specified.
     */
    void parseUri(const std::string& uri, std::string& controller,
                  std::string& action) const;

    /**
     * Preprocess the request:
     * - check data format
     * - set request header
     * - extract collection
     * @param[in] controller The request controller.
     * @param[in] action The request action.
     * @param[in] tokens The request tokens.
     * @param[in,out] request The current request.
     * @param[out] collection The request collection.
     * @throw ClientError if the request is not valid.
     */
    void preprocessRequest(const std::string& controller, const std::string& action,
                           const std::string& tokens, std::string& request,
                           std::string& collection) const;

    /// Increment the request sequence number.
    void incrementSequence() {
        if (++sequence == MAX_SEQUENCE) {
            sequence = 1; // sequence == 0 means server error
        }
    }

    /**
     * Send request to and receive response from the SF1.
     * @param[in] connection The current connection to the SF1.
     * @param[in] request The current request.
     * @param[out] response The response to the current request.
     * @throw ServerError if error occur on SF1 while processing the request.
     * @see RawClient
     */
    void sendAndReceive(RawClient& connection, const std::string& request,
                        std::string& response);

protected:

    friend class Releaser;

    /// Set data format used for request and responses.
    void setFormat();

    /// Request sequence number.
    uint32_t sequence;

    /// Actual request/response format.
    Format format;

    /// Input/Output service.
    ba::io_service service;

    /// Pointer to the actual request/response format handler.
    boost::scoped_ptr<Writer> writer;

    /// ConnectionPool factory.
    boost::scoped_ptr<PoolFactory> factory;
};

NS_IZENELIB_SF1R_END

#endif	/* SF1DRIVSF1DRIVER_BASE_HPP */

max_stars_repo_name: izenecloud/izenelib

max_issues_repo_name: izenecloud/izenelib

max_forks_repo_name: izenecloud/izenelib


text:
/*
	Copyright (C) 2003-2014 by David White <davewx7@gmail.com>

	This software is provided 'as-is', without any express or implied
	warranty. In no event will the authors be held liable for any damages
	arising from the use of this software.

	Permission is granted to anyone to use this software for any purpose,
	including commercial applications, and to alter it and redistribute it
	freely, subject to the following restrictions:

	   1. The origin of this software must not be misrepresented; you must not
	   claim that you wrote the original software. If you use this software
	   in a product, an acknowledgement in the product documentation would be
	   appreciated but is not required.

	   2. Altered source versions must be plainly marked as such, and must not be
	   misrepresented as being the original software.

	   3. This notice may not be removed or altered from any source
	   distribution.
*/

#pragma once

#include <map>
#include <vector>

#include "intrusive_ptr.hpp"
#include <boost/scoped_ptr.hpp>

#include "formula_callable.hpp"
#include "formula_callable_definition.hpp"
#include "variant.hpp"
#include "variant_type.hpp"

namespace game_logic
{
	struct FormulaInterfaceImpl;

	class FormulaInterfaceInstanceFactory : public FormulaCallable
	{
	public:
		virtual ~FormulaInterfaceInstanceFactory();

		virtual bool all_static_lookups() const = 0;
		virtual variant create(const variant& v) const = 0;
		virtual int getId() const = 0;

		variant getValue(const std::string& key) const override { return variant(); }
	};

	class FormulaInterface : public FormulaCallable
	{
	public:
		struct interface_mismatch_error {
			explicit interface_mismatch_error(const std::string& msg) : msg(msg) {}
			std::string msg;
		};

		explicit FormulaInterface(const std::map<std::string, variant_type_ptr>& types);
		const std::map<std::string, variant_type_ptr>& get_types() const { return types_; }

		FormulaInterfaceInstanceFactory* createFactory(variant_type_ptr type) const; //throw interface_mismatch_error
		FormulaInterfaceInstanceFactory* getDynamicFactory() const;

		ConstFormulaCallableDefinitionPtr getDefinition() const;

		bool match(const variant& v) const;

		std::string to_string() const;

	private:
		variant getValue(const std::string& key) const override;

		std::map<std::string, variant_type_ptr> types_;
		std::unique_ptr<FormulaInterfaceImpl> impl_;
	};
}

typedef ffl::IntrusivePtr<game_logic::FormulaInterface> FormulaInterfacePtr;
typedef ffl::IntrusivePtr<const game_logic::FormulaInterface> ConstFormulaInterfacePtr;

max_stars_repo_name: gamobink/anura

max_issues_repo_name: gamobink/anura

max_forks_repo_name: gamobink/anura


text:
/*
 * Copyright (c) 2011, Peter Thorson. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the WebSocket++ Project nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
//#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE sha1
#include <boost/test/unit_test.hpp>

#include <iostream>
#include <string>

#include <websocketpp/sha1/sha1.hpp>
#include <websocketpp/utilities.hpp>

BOOST_AUTO_TEST_SUITE ( sha1 )

BOOST_AUTO_TEST_CASE( sha1_test_a ) {
    websocketpp::sha1 sha;
    uint32_t digest[5];

    sha << "abc";
    BOOST_CHECK(sha.get_raw_digest(digest));

    BOOST_CHECK_EQUAL( digest[0], 0xa9993e36 );
    BOOST_CHECK_EQUAL( digest[1], 0x4706816a );
    BOOST_CHECK_EQUAL( digest[2], 0xba3e2571 );
    BOOST_CHECK_EQUAL( digest[3], 0x7850c26c );
    BOOST_CHECK_EQUAL( digest[4], 0x9cd0d89d );
}

BOOST_AUTO_TEST_CASE( sha1_test_b ) {
    websocketpp::sha1 sha;
    uint32_t digest[5];

    sha << "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
    BOOST_CHECK(sha.get_raw_digest(digest));

    BOOST_CHECK_EQUAL( digest[0], 0x84983e44 );
    BOOST_CHECK_EQUAL( digest[1], 0x1c3bd26e );
    BOOST_CHECK_EQUAL( digest[2], 0xbaae4aa1 );
    BOOST_CHECK_EQUAL( digest[3], 0xf95129e5 );
    BOOST_CHECK_EQUAL( digest[4], 0xe54670f1 );
}

BOOST_AUTO_TEST_CASE( sha1_test_c ) {
    websocketpp::sha1 sha;
    uint32_t digest[5];

    for (int i = 1; i <= 1000000; i++) {
        sha.input('a');
    }

    BOOST_CHECK(sha.get_raw_digest(digest));

    BOOST_CHECK_EQUAL( digest[0], 0x34aa973c );
    BOOST_CHECK_EQUAL( digest[1], 0xd4c4daa4 );
    BOOST_CHECK_EQUAL( digest[2], 0xf61eeb2b );
    BOOST_CHECK_EQUAL( digest[3], 0xdbad2731 );
    BOOST_CHECK_EQUAL( digest[4], 0x6534016f );
}

BOOST_AUTO_TEST_SUITE_END()

max_stars_repo_name: anypath/CoinVault

max_stars_repo_licenses: Unlicense OpenSSL MIT

max_issues_repo_name: anypath/CoinVault

max_issues_repo_licenses: Unlicense OpenSSL MIT

max_forks_repo_name: anypath/CoinVault

max_forks_repo_licenses: Unlicense OpenSSL MIT


text:
#include "Domain.hpp"
#include <algorithm>
#include <sstream>
#include <boost/foreach.hpp>
#include <boost/assign/list_of.hpp>
#include <base-logging/Logging.hpp>
#include "grammar/lisp/Expression.hpp"

namespace pddl_planner {
namespace representation {

Expression::Expression(const Label& label)
    : label(label)
{
    if(isQuantor(label))
    {
        throw std::invalid_argument("pddl_planner::representation::Expression: quantor expression should use quantor related constructor");
    }
}

Expression::Expression(const Expression& other)
{
    *this = other;
}

Expression::~Expression()
{
    ExpressionPtrList::iterator it = parameters.begin();
    for(; it != parameters.end(); ++it)
    {
        delete *it;
        *it = NULL;
    }
}

Expression::Expression(const Label& label, const Expression& arg0, const Expression& arg1, const Expression& arg2, const Expression& arg3, const Expression& arg4, const Expression& arg5, const Expression& arg6, const Expression& arg7, const Expression& arg8, const Expression& arg9, const Expression& arg10)
    : label(label)
{
    if(isQuantor(label))
    {
        throw std::invalid_argument("pddl_planner::representation::Expression: quantor expression should use quantor related constructor");
    }

    if(!arg0.isNull())
    {
        addParameter(arg0);
    }

    if(!arg1.isNull())
    {
        addParameter(arg1);
    }

    if(!arg2.isNull())
    {
        addParameter(arg2);
    }

    if(!arg3.isNull())
    {
        addParameter(arg3);
    }

    if(!arg4.isNull())
    {
        addParameter(arg4);
    }

    if(!arg5.isNull())
    {
        addParameter(arg5);
    }

    if(!arg6.isNull())
    {
        addParameter(arg6);
    }

    if(!arg7.isNull())
    {
        addParameter(arg7);
    }

    if(!arg8.isNull())
    {
        addParameter(arg8);
    }

    if(!arg9.isNull())
    {
        addParameter(arg9);
    }

    if(!arg10.isNull())
    {
        addParameter(arg10);
    }
}

Expression::Expression(const Label& label, const Label& arg0, const Label& arg1, const Label& arg2)
    : label(label)
{
    if(isQuantor(label))
    {
        throw std::invalid_argument("pddl_planner::representation::Expression: quantor expression should use quantor related constructor");
    }

    if(!arg0.empty())
    {
        addParameter(arg0);
    }

    if(!arg1.empty())
    {
        addParameter(arg1);
    }

    if(!arg2.empty())
    {
        addParameter(arg2);
    }
}

Expression::Expression(Quantor quantor, const TypedItem& typedItem, const Expression& e)
    : typedItem(typedItem)
{
    label = QuantorTxt[quantor];

    if(!e.isNull())
    {
        addParameter(e);
    } else {
        throw std::runtime_error("pddl_planner::representation::Expression: quantor expects expression");
    }
}

void Expression::addParameter(const Label& e)
{
    parameters.push_back( new Expression(e) );
}

void Expression::addParameter(const Expression& e)
{
    parameters.push_back( new Expression(e) );
}

std::map<Quantor, std::string> QuantorTxt = boost::assign::map_list_of
    (FORALL, "forall")
    (EXISTS, "exists");

std::string Expression::toLISP() const
{
    if(isAtomic())
    {
        return label;
    }

    if(isQuantor(label))
    {
        std::string txt = "(" + label + " (";
        txt += typedItem.label;
        if(!typedItem.type.empty())
        {
            txt += " - " + typedItem.type;
        }
        txt += ")";

        BOOST_FOREACH(Expression* e, parameters)
        {
            txt += " " + e->toLISP();
        }
        txt += ")";
        return txt;
    }

    std::string txt = "(" + label;
    BOOST_FOREACH(Expression* e, parameters)
    {
        txt += " " + e->toLISP();
    }
    txt += ")";
    return txt;
}

Expression Expression::fromString(const std::string& expressionString)
{
    typedef grammar::lisp::Expression<std::string::const_iterator, qi::space_type> lisp_expression_grammar;
    lisp_expression_grammar grammar;
    Expression expression;

    std::string::const_iterator iter = expressionString.begin();
    std::string::const_iterator end = expressionString.end();

    bool r = phrase_parse(iter, end, grammar, qi::space, expression);
    if(r && iter == end)
    {
        return expression;
    }

    throw std::invalid_argument("pddl_planner::representation::Expression::fromString: '" +\
            expressionString + "' is not a valid expression");
}

bool Expression::isQuantor(const Label& label)
{
    std::map<Quantor, std::string>::const_iterator cit = QuantorTxt.begin();
    for(; cit != QuantorTxt.end(); ++cit)
    {
        if(cit->second == label)
        {
            return true;
        }
    }
    return false;
}

Expression& Expression::operator=(const Expression& other)
{
    if( this != &other)
    {
        label = other.label;
        typedItem = other.typedItem;

        {
            parameters.clear();
            BOOST_FOREACH(Expression* e, other.parameters)
            {
                addParameter(*e);
            }
        }
    }
    return *this;
}

bool Expression::operator==(const Expression& other) const
{
    if(label != other.label && typedItem != other.typedItem)
    {
        return false;
    }

    if(parameters.size() != other.parameters.size())
    {
        return false;
    }

    ExpressionPtrList::const_iterator pit = parameters.begin();
    for(;pit != parameters.end(); ++pit)
    {
        bool found = false;
        ExpressionPtrList::const_iterator oit = other.parameters.begin();
        for(; oit != other.parameters.end(); ++oit)
        {
            if(*pit == *oit)
            {
                found = true;
                break;
            }
        }
        if(!found)
        {
            return false;
        }
    }
    return true;
}

VariableManager::VariableManager(const ArgumentList& arguments)
{
    BOOST_FOREACH(TypedItem t, arguments)
    {
        registerVariable(t.label);
    }
}

void VariableManager::push(const Label& label)
{
    mOperatorStack.push_back(label);
}

Label VariableManager::pop()
{
    if(mOperatorStack.empty())
    {
        throw std::runtime_error("pddl_planner::representation::VariableManager::pop cannot pop element from empty stack");
    }

    Label label = mOperatorStack.back();
    mOperatorStack.pop_back();
    return label;
}

std::string VariableManager::getOperatorStackAsString() const
{
    std::string txt;
    BOOST_FOREACH(Label l, mOperatorStack)
    {
        txt += "|" + l + "|";
    }
    return txt;
}

std::string VariableManager::canonize(const std::string& name)
{
    // make sure a valid variable is passed
    if( !isVariable(name) )
    {
        return "?" + name;
    } else {
        return name;
    }
}

bool VariableManager::isVariable(const std::string& name)
{
    return !name.empty() && name.data()[0] == '?';
}

void VariableManager::registerVariable(const std::string& name)
{
    mKnownVariables.push_back( canonize(name) );
}

bool VariableManager::isKnownVariable(const std::string& name) const
{
    return mKnownVariables.end() != std::find(mKnownVariables.begin(), mKnownVariables.end(), canonize(name) );
}

bool VariableManager::hasTypedVariable(const TypedItemList& list, const TypedItem& item) const
{
    TypedItemList::const_iterator cit = list.begin();
    for(; cit != list.end(); ++cit)
    {
        if(cit->label == canonize(item.label))
        {
            if(cit->type != item.type)
            {
                throw std::runtime_error("pddl_planner::representation::Variable exist but with different type: '" + cit->type  + "' while trying to add: '" + item.type);
            }
            return true;
        }
    }

    return false;
}

void VariableManager::addTypedVariable(TypedItemList& list, const TypedItem& item)
{
    list.push_back( TypedItem( canonize(item.label), item.type ) );
}

void Action::addArgument(const TypedItem& arg)
{
    VariableManager::addTypedVariable(arguments, arg);
}

bool Action::isArgument(const Label& label)
{
    BOOST_FOREACH(TypedItem t, arguments)
    {
        if(t.label == label)
        {
            return true;
        }
    }
    return false;
}

ArityValidator::ArityValidator(const PredicateList& predicates)
{
    PredicateList::const_iterator cit = predicates.begin();
    for(; cit != predicates.end(); ++cit)
    {
        mArityMap[cit->label] = Arity::exact( cit->arguments.size() );
    }

    addDefaults();
}

void ArityValidator::addDefaults()
{
    mArityMap["and"]  = Arity::min(2);
    mArityMap["or"]   = Arity::min(2);
    mArityMap["not"]  = Arity::exact(1);
    mArityMap["="]    = Arity::exact(2);
    mArityMap["when"] = Arity::min(1);
    mArityMap["forall"] = Arity::min(2);
}

bool ArityValidator::isQuantifier(const Label& label) const
{
    if(label == "forall" || label == "exists")
    {
        return true;
    }
    return false;
}

bool ArityValidator::isOperator(const Label& label) const
{
    return mArityMap.count(label);
}

void ArityValidator::validate(const Label& label, uint8_t arity)
{
    ArityMap::const_iterator cit = mArityMap.find(label);
    if(cit == mArityMap.end())
    {
        throw std::invalid_argument("pddl_planner::representation::ArityValidator: unknown predicate or operator: '" + label + "'");
    }

    Arity allowedArity = cit->second;

    if(arity < allowedArity.getMin())
    {
        throw std::invalid_argument("pddl_planner::representation::ArityValidator predicate or operator: '" + label + "' provided with too few parameters");
    } else if(arity > allowedArity.getMax())
    {
        throw std::invalid_argument("pddl_planner::representation::ArityValidator predicate or operator: '" + label + "' provided with too many parameters");
    }
}

Domain::Domain(const std::string& name)
    : name(name)
{
    addRequirement("typing");
}

void Domain::addType(const Type& type, const Type& parentType)
{
    std::map<Type,Type>::const_iterator cit = type2parent.find(type);
    if(cit != type2parent.end())
    {
        if(cit->second != parentType)
        {
            throw std::invalid_argument("pddl_planner::representation::Domain: type '"
                    + type + "' has already been added with different parent type: '"
                    + cit->second + "' vs. '" + parentType);
        }
        return;
    }

    types.push_back(type);
    type2parent[type] = parentType;
}

void Domain::addConstant(const TypedItem& constant, bool overwrite)
{
    if(isConstant(constant.label))
    {
        if(overwrite)
        {
            removeConstant(constant.label);
        } else {
            throw std::invalid_argument("pddl_planner::representation::Domain::addConstant constant '" + constant.label + "' already exists");
        }
    } else if(!isType(constant.type))
    {
        throw std::invalid_argument("pddl_planner::representation::Domain::addConstant unknown type '" + constant.type + "'");
    }

    constants.push_back( constant );
}

void Domain::addPredicate(const Predicate& predicate, bool overwrite)
{
    if(isPredicate(predicate.label) && !overwrite)
    {
        throw std::invalid_argument("pddl_planner::representation::Domain::addPredicate predicate '" + predicate.label + "' already exists");
    } else {
        removePredicate(predicate.label);
    }
    predicates.push_back(predicate);
}

void Domain::addRequirement(const Requirement& requirement)
{
    if(!isRequirement(requirement))
    {
        requirements.push_back(requirement);
    } else {
        LOG_INFO_S << "Requirement: '" << requirement << "' already exists";
    }
}

void Domain::addAction(const Action& action, bool overwrite)
{
    if(isAction(action.label) && !overwrite)
    {
        throw std::invalid_argument("pddl_planner::representation::Domain::addAction action '" + action.label + "' already exists");
    } else {
        removeAction(action.label);
    }

    actions.push_back(action);
}

void Domain::addFunction(const Function& function, bool overwrite)
{
    if(isFunction(function.label) && !overwrite)
    {
        throw std::invalid_argument("pddl_planner::representation::Domain::addFunction function '" + function.label + "' already exists");
    } else {
        removeFunction(function.label);
    }
    functions.push_back(function);
}

void Domain::removeConstant(const Label& label)
{
    ConstantList::iterator cit = constants.begin();
    for(; cit != constants.end(); ++cit)
    {
        if(cit->label == label)
        {
            constants.erase(cit);
        }
    }
}

void Domain::removePredicate(const Label& label)
{
    PredicateList::iterator cit = predicates.begin();
    for(; cit != predicates.end(); ++cit)
    {
        if(cit->label == label)
        {
            predicates.erase(cit);
        }
    }
}

void Domain::removeAction(const Label& label)
{
    ActionList::iterator cit = actions.begin();
    for(; cit != actions.end(); ++cit)
    {
        if(cit->label == label)
        {
            actions.erase(cit);
        }
    }
}

void Domain::removeFunction(const Label& label)
{
    FunctionList::iterator cit = functions.begin();
    for(; cit != functions.end(); ++cit)
    {
        if(cit->label == label)
        {
            functions.erase(cit);
        }
    }
}

bool Domain::isType(const Type& type) const
{
    return types.end() != std::find(types.begin(), types.end(), type);
}

bool Domain::isConstant(const Label& label) const
{
    ConstantList::const_iterator cit = constants.begin();
    for(; cit != constants.end(); ++cit)
    {
        if(cit->label == label)
        {
            return true;
        }
    }
    return false;
}

bool Domain::isPredicate(const Label& label) const
{
    PredicateList::const_iterator cit = predicates.begin();
    for(; cit != predicates.end(); ++cit)
    {
        if(cit->label == label)
        {
            return true;
        }
    }
    return false;
}

bool Domain::isRequirement(const Requirement& requirement) const
{
    RequirementList::const_iterator cit = requirements.begin();
    for(; cit != requirements.end(); ++cit)
    {
        if(*cit == requirement)
        {
            return true;
        }
    }
    return false;
}

bool Domain::isAction(const Label& label) const
{
    ActionList::const_iterator cit = actions.begin();
    for(; cit != actions.end(); ++cit)
    {
        if(cit->label == label)
        {
            return true;
        }
    }
    return false;
}

bool Domain::isFunction(const Label& label) const
{
    FunctionList::const_iterator cit = functions.begin();
    for(; cit != functions.end(); ++cit)
    {
        if(cit->label == label)
        {
            return true;
        }
    }
    return false;
}

Predicate Domain::getPredicate(const Label& label) const
{
    BOOST_FOREACH(Predicate p, predicates)
    {
        if(p.label == label)
        {
            return p;
        }
    }
    throw std::runtime_error("pddl_planner::representation::Domain::getPredicate: predicate '" + label + "' could not be found");
}

Action Domain::getAction(const Label& label) const
{
    BOOST_FOREACH(Action a, actions)
    {
        if(a.label == label)
        {
            return a;
        }
    }
    throw std::runtime_error("pddl_planner::representation::Domain::getAction: action '" + label + "' could not be found");
}

std::string Domain::toLISP() const
{
    std::stringstream ss;
    ss << "; BEGIN domain definition" << std::endl;
    ss << "(define (domain " << name << ")" << std::endl;
    if(!requirements.empty())
    {
        ss << "    (:requirements" << std::endl;
        BOOST_FOREACH(Requirement r, requirements)
        {
            ss << "        :" << r << std::endl;
        }
        ss << "    ); end requirements" << std::endl;
    }

    if(!types.empty())
    {
        ss << "    (:types" << std::endl;
        BOOST_FOREACH(Type t, types)
        {
            std::map<Type,Type>::const_iterator cit = type2parent.find(t);
            if(cit != type2parent.end() && !cit->second.empty())
            {
                ss << "        " << t << " - " << cit->second << std::endl;
            } else {
                ss << "        " << t << std::endl;
            }
        }
        ss << "    ); end types" << std::endl;
    }

    if(!constants.empty())
    {
        ss << "    (:constants" << std::endl;
        BOOST_FOREACH(Type t, types)
        {
            std::stringstream constantLine;
            BOOST_FOREACH(Constant c, constants)
            {
                if(c.type == t)
                {
                    constantLine << "        " << c.label << std::endl;
                }
            }
            if(constantLine.str() != "")
            {
                ss << constantLine.str();
                ss << "            - " << t << std::endl;
            }
        }
        ss << "    ); end constants" << std::endl;
    }

    if(!predicates.empty())
    {
        ss << "    (:predicates " << std::endl;

        BOOST_FOREACH(Predicate p, predicates)
        {
            ss << "        ( " << p.label;

            BOOST_FOREACH(TypedItem arg, p.arguments)
            {
                ss << " " << arg.label << " - " << arg.type;
            }
            ss << " )" << std::endl;
        }
        ss << "    ); end predicates" << std::endl;
    }

    if(!functions.empty())
    {
        ss << "    (:functions " << std::endl;
        BOOST_FOREACH(Function f, functions)
        {
            ss << "        ( " << f.label;
            BOOST_FOREACH(TypedItem arg, f.arguments)
            {
                ss << " " << arg.label << " - " << arg.type;
            }
            ss << " )" << std::endl;

        }
        ss << "    )" << std::endl;
    }

    if(!actions.empty())
    {
        BOOST_FOREACH(Action a, actions)
        {
            ss << "    (:action " << a.label << std::endl;

            ss << "        :parameters (";
            BOOST_FOREACH(TypedItem arg, a.arguments)
            {
                ss << " " << arg.label << " - " << arg.type;
            }
            ss << " )" << std::endl;

            if(!a.preconditions.empty())
            {
                ss << "        :precondition";
                BOOST_FOREACH(Expression e, a.preconditions)
                {
                    ss << " " << e.toLISP();
                }
                ss << std::endl;
            }

            if(!a.effects.empty())
            {
                ss << "        :effect";
                BOOST_FOREACH(Expression e, a.effects)
                {
                    ss <<  " " << e.toLISP();
                }
            }
            ss << std::endl << "    )" << std::endl;
        }
    }
    ss << "; END domain definition" << std::endl;
    ss << ")" << std::endl;

    return ss.str();
}

void Domain::validate(const Expression& e, const VariableManager& variableManager) const
{
    ArityValidator operatorValidator;

    if(e.isAtomic())
    {
        LOG_DEBUG_S << "Validating atomic statement: '" << e.label << "'";
        if(! (isConstant(e.label) || isType(e.label) || variableManager.isKnownVariable(e.label)))
        {
            throw std::runtime_error("pddl_planner::representation::Domain::validateExpression '" + e.label + "' in '" + variableManager.getOperatorStackAsString() + "' is not a registered constant, type or local variable");
        }
    } else if( isPredicate(e.label) )
    {
        LOG_DEBUG_S << "Validating predicate: '" << e.label << "'";
        Predicate p = getPredicate(e.label);
        BOOST_FOREACH(Expression* ePtr, e.parameters)
        {
            validate(*ePtr, variableManager);
        }
    } else if( isAction(e.label))
    {
        LOG_DEBUG_S << "Validating action: '" << e.label << "'";
        Action a = getAction(e.label);
        BOOST_FOREACH(Expression* ePtr, e.parameters)
        {
            validate(*ePtr, variableManager);
        }
    } else if( operatorValidator.isQuantifier(e.label) )
    {
        // TODO: support here to validate free and bound variables
        LOG_DEBUG_S << "Validation of quantifier '" << e.label << "' unsupported, i.e. defaulting to true";
    } else if( operatorValidator.isOperator(e.label) )
    {
        LOG_DEBUG_S << "Validating operator: '" << e.label << "'";
        BOOST_FOREACH(Expression* ePtr, e.parameters)
        {
            validate(*ePtr, variableManager);
        }
    } else {
        LOG_DEBUG_S << "expression '" << e.label << "' is not a known constant, predicate, action or variable";
        throw std::runtime_error("pddl_planner::representation::Domain::validateExpression '" + e.label + "' in '" + variableManager.getOperatorStackAsString() + "' is neither a known constant, predicate, action or variable");
    }
}

void Domain::validate() const
{
    if(isNull())
    {
        throw std::runtime_error("pddl_planner::representation::Domain::validate domain is empty");
    }

    BOOST_FOREACH(Action action, actions)
    {
        LOG_DEBUG_S << "Validating action: " << action.label;

        VariableManager variableManager(action.arguments);

        BOOST_FOREACH(Expression e, action.preconditions)
        {
            LOG_DEBUG_S << "Validating precondition: " << e.label;
            validate(e, variableManager);
        }

        BOOST_FOREACH(Expression e, action.effects)
        {
            LOG_DEBUG_S << "Validating effect: " << e.label;
            validate(e, variableManager);
        }
    }
}

} // end namespace pddl_planner
} // end namespace representation


text:
#include "aligner.h"

#include <cstdio>
#include <set>

#include <boost/scoped_ptr.hpp>

#include "array2d.h"
#include "hg.h"
#include "kbest.h"
#include "sentence_metadata.h"
#include "inside_outside.h"
#include "viterbi.h"
#include "alignment_io.h"

using namespace std;

// used with lexical models since they may not fully generate the
// source string
void SourceEdgeCoveragesUsingParseIndices(const Hypergraph& g,
                                          vector<set<int> >* src_cov) {
  src_cov->clear();
  src_cov->resize(g.edges_.size());

  for (int i = 0; i < g.edges_.size(); ++i) {
    const Hypergraph::Edge& edge = g.edges_[i];
    set<int>& cov = (*src_cov)[i];
    // no words
    if (edge.rule_->EWords() == 0 || edge.rule_->FWords() == 0)
      continue;
    // aligned to NULL (crf ibm variant only)
    if (edge.prev_i_ == -1 || edge.i_ == -1) {
      cov.insert(-1);
      continue;
    }
    assert(edge.j_ >= 0);
    assert(edge.prev_j_ >= 0);
    if (edge.Arity() == 0) {
      for (int k = edge.prev_i_; k < edge.prev_j_; ++k)
        cov.insert(k);
    } else {
      // note: this code, which handles mixed NT and terminal
      // rules assumes that nodes uniquely define a src and trg
      // span.
      int k = edge.prev_i_;
      int j = 0;
      const vector<WordID>& f = edge.rule_->e();  // rules are inverted
      while (k < edge.prev_j_) {
        if (f[j] > 0) {
          cov.insert(k);
          // cerr << "src: " << k << endl;
          ++k;
          ++j;
        } else {
          const Hypergraph::Node& tailnode = g.nodes_[edge.tail_nodes_[-f[j]]];
          assert(tailnode.in_edges_.size() > 0);
          // any edge will do:
          const Hypergraph::Edge& rep_edge = g.edges_[tailnode.in_edges_.front()];
          //cerr << "skip " << (rep_edge.prev_j_ - rep_edge.prev_i_) << endl;  // src span
          k += (rep_edge.prev_j_ - rep_edge.prev_i_);  // src span
          ++j;
        }
      }
    }
  }
}

int SourceEdgeCoveragesUsingTree(const Hypergraph& g,
                                 int node_id,
                                 int span_start,
                                 vector<int>* spans,
                                 vector<set<int> >* src_cov) {
  const Hypergraph::Node& node = g.nodes_[node_id];
  int k = -1;
  for (int i = 0; i < node.in_edges_.size(); ++i) {
    const int edge_id = node.in_edges_[i];
    const Hypergraph::Edge& edge = g.edges_[edge_id];
    set<int>& cov = (*src_cov)[edge_id];
    const vector<WordID>& f = edge.rule_->e();  // rules are inverted
    int j = 0;
    k = span_start;
    while (j < f.size()) {
      if (f[j] > 0) {
        cov.insert(k);
        ++k;
        ++j;
      } else {
        const int tail_node_id = edge.tail_nodes_[-f[j]];
        int &right_edge = (*spans)[tail_node_id];
        if (right_edge < 0)
          right_edge = SourceEdgeCoveragesUsingTree(g, tail_node_id, k, spans, src_cov);
        k = right_edge;
        ++j;
      }
    }
  }
  return k;
}

void SourceEdgeCoveragesUsingTree(const Hypergraph& g,
                                  vector<set<int> >* src_cov) {
  src_cov->clear();
  src_cov->resize(g.edges_.size());
  vector<int> span_sizes(g.nodes_.size(), -1);
  SourceEdgeCoveragesUsingTree(g, g.nodes_.size() - 1, 0, &span_sizes, src_cov);
}

int TargetEdgeCoveragesUsingTree(const Hypergraph& g,
                                 int node_id,
                                 int span_start,
                                 vector<int>* spans,
                                 vector<set<int> >* trg_cov) {
  const Hypergraph::Node& node = g.nodes_[node_id];
  int k = -1;
  for (int i = 0; i < node.in_edges_.size(); ++i) {
    const int edge_id = node.in_edges_[i];
    const Hypergraph::Edge& edge = g.edges_[edge_id];
    set<int>& cov = (*trg_cov)[edge_id];
    int ntc = 0;
    const vector<WordID>& e = edge.rule_->f();  // rules are inverted
    int j = 0;
    k = span_start;
    while (j < e.size()) {
      if (e[j] > 0) {
        cov.insert(k);
        ++k;
        ++j;
      } else {
        const int tail_node_id = edge.tail_nodes_[ntc];
        ++ntc;
        int &right_edge = (*spans)[tail_node_id];
        if (right_edge < 0)
          right_edge = TargetEdgeCoveragesUsingTree(g, tail_node_id, k, spans, trg_cov);
        k = right_edge;
        ++j;
      }
    }
    // cerr << "node=" << node_id << ": k=" << k << endl;
  }
  return k;
}

void TargetEdgeCoveragesUsingTree(const Hypergraph& g,
                                  vector<set<int> >* trg_cov) {
  trg_cov->clear();
  trg_cov->resize(g.edges_.size());
  vector<int> span_sizes(g.nodes_.size(), -1);
  TargetEdgeCoveragesUsingTree(g, g.nodes_.size() - 1, 0, &span_sizes, trg_cov);
}

struct TransitionEventWeightFunction {
  typedef SparseVector<prob_t> Result;
  inline SparseVector<prob_t> operator()(const Hypergraph::Edge& e) const {
    SparseVector<prob_t> result;
    result.set_value(e.id_, e.edge_prob_);
    return result;
  }
};

inline void WriteProbGrid(const Array2D<prob_t>& m, ostream* pos) {
  ostream& os = *pos;
  char b[1024];
  for (int i=0; i<m.width(); ++i) {
    for (int j=0; j<m.height(); ++j) {
      if (m(i,j) == prob_t::Zero()) {
        os << "\t---X---";
      } else {
        snprintf(b, 1024, "%0.5f", m(i,j).as_float());
        os << '\t' << b;
      }
    }
    os << '\n';
  }
}

// this code is rather complicated since it must deal with generating alignments
// when lattices are specified as input as well as with models that do not generate
// full sentence pairs (like lexical alignment models)
void AlignerTools::WriteAlignment(const Lattice& src_lattice,
                                  const Lattice& trg_lattice,
                                  const Hypergraph& in_g,
                                  ostream* out,
                                  bool map_instead_of_viterbi,
                                  int k_best, // must = 0 if MAP
                                  const vector<bool>* edges) {
  bool fix_up_src_spans = false;
  if (k_best > 1 && edges) {
    cerr << "ERROR: cannot request multiple best alignments and provide an edge set!\n";
    abort();
  }
  if (map_instead_of_viterbi) {
    if (k_best != 0) {
      cerr << "WARNING: K-best alignment extraction not available for MAP, use --aligner_use_viterbi\n";
    }
    k_best = 1;
  } else {
    if (k_best == 0) k_best = 1;
  }
  const Hypergraph* g = &in_g;
  HypergraphP new_hg;
  if (!IsSentence(src_lattice) ||
      !IsSentence(trg_lattice)) {
    if (map_instead_of_viterbi) {
      cerr << "  Lattice alignment: using Viterbi instead of MAP alignment\n";
    }
    map_instead_of_viterbi = false;
    fix_up_src_spans = !IsSentence(src_lattice);
  }

  KBest::KBestDerivations<vector<Hypergraph::Edge const*>, ViterbiPathTraversal> kbest(in_g, k_best);
  boost::scoped_ptr<vector<bool> > kbest_edges;

  for (int best = 0; best < k_best; ++best) {
    const KBest::KBestDerivations<vector<Hypergraph::Edge const*>, ViterbiPathTraversal>::Derivation* d = NULL;
    if (!map_instead_of_viterbi) {
      d = kbest.LazyKthBest(in_g.nodes_.size() - 1, best);
      if (!d) break;  // there are fewer than k_best derivations!
      const vector<Hypergraph::Edge const*>& yield = d->yield;
      kbest_edges.reset(new vector<bool>(in_g.edges_.size(), false));
      for (int i = 0; i < yield.size(); ++i) {
        assert(yield[i]->id_ < kbest_edges->size());
        (*kbest_edges)[yield[i]->id_] = true;
      }
    }
    if (!map_instead_of_viterbi || edges) {
      if (kbest_edges) edges = kbest_edges.get();
      new_hg = in_g.CreateViterbiHypergraph(edges);
      for (int i = 0; i < new_hg->edges_.size(); ++i)
        new_hg->edges_[i].edge_prob_ = prob_t::One();
      g = new_hg.get();
    }

    vector<prob_t> edge_posteriors(g->edges_.size(), prob_t::Zero());
    vector<WordID> trg_sent;
    vector<WordID> src_sent;
    if (fix_up_src_spans) {
      ViterbiESentence(*g, &src_sent);
    } else {
      src_sent.resize(src_lattice.size());
      for (int i = 0; i < src_sent.size(); ++i)
        src_sent[i] = src_lattice[i][0].label;
    }

    ViterbiFSentence(*g, &trg_sent);

    if (edges || !map_instead_of_viterbi) {
      for (int i = 0; i < edge_posteriors.size(); ++i)
        edge_posteriors[i] = prob_t::One();
    } else {
      SparseVector<prob_t> posts;
      const prob_t z = InsideOutside<prob_t, EdgeProb, SparseVector<prob_t>, TransitionEventWeightFunction>(*g, &posts);
      for (int i = 0; i < edge_posteriors.size(); ++i)
        edge_posteriors[i] = posts.value(i) / z;
    }
    vector<set<int> > src_cov(g->edges_.size());
    vector<set<int> > trg_cov(g->edges_.size());
    TargetEdgeCoveragesUsingTree(*g, &trg_cov);

    if (fix_up_src_spans)
      SourceEdgeCoveragesUsingTree(*g, &src_cov);
    else
      SourceEdgeCoveragesUsingParseIndices(*g, &src_cov);

    // figure out the src and reference size;
    int src_size = src_sent.size();
    int ref_size = trg_sent.size();
    Array2D<prob_t> align(src_size + 1, ref_size, prob_t::Zero());
    for (int c = 0; c < g->edges_.size(); ++c) {
      const prob_t& p = edge_posteriors[c];
      const set<int>& srcs = src_cov[c];
      const set<int>& trgs = trg_cov[c];
      for (set<int>::const_iterator si = srcs.begin();
           si != srcs.end(); ++si) {
        for (set<int>::const_iterator ti = trgs.begin();
             ti != trgs.end(); ++ti) {
          align(*si + 1, *ti) += p;
        }
      }
    }
    new_hg.reset();
    //if (g != &in_g) { g.reset(); }

    prob_t threshold(0.9);
    const bool use_soft_threshold = true; // TODO configure

    Array2D<bool> grid(src_size, ref_size, false);
    for (int j = 0; j < ref_size; ++j) {
      if (use_soft_threshold) {
        threshold = prob_t::Zero();
        for (int i = 0; i <= src_size; ++i)
          if (align(i, j) > threshold) threshold = align(i, j);
        //threshold *= prob_t(0.99);
      }
      for (int i = 0; i < src_size; ++i)
        grid(i, j) = align(i+1, j) >= threshold;
    }
    if (out == &cout && k_best < 2) {
      // TODO need to do some sort of verbose flag
      WriteProbGrid(align, &cerr);
      cerr << grid << endl;
    }
    (*out) << TD::GetString(src_sent) << " ||| " << TD::GetString(trg_sent) << " ||| ";
    AlignmentIO::SerializePharaohFormat(grid, out);
  }
};

ext: cc

max_stars_repo_name: pks/cdec-dtrain-legacy

max_issues_repo_name: pks/cdec-dtrain-legacy

max_forks_repo_name: pks/cdec-dtrain-legacy


text:
/*

Copyright (c) 2015, Project OSRM, Dennis Luxen, others
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list
of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

#include "extraction_containers.hpp"
#include "extraction_way.hpp"

#include "../data_structures/node_id.hpp"
#include "../data_structures/range_table.hpp"

#include "../Util/osrm_exception.hpp"
#include "../Util/simple_logger.hpp"
#include "../Util/timing_util.hpp"

#include <boost/assert.hpp>
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>

#include <stxxl/sort>

#include <chrono>
#include <limits>

ExtractionContainers::ExtractionContainers()
{
    // Check if stxxl can be instantiated
    stxxl::vector<unsigned> dummy_vector;
    name_list.push_back("");
}

ExtractionContainers::~ExtractionContainers()
{
    used_node_id_list.clear();
    all_nodes_list.clear();
    all_edges_list.clear();
    name_list.clear();
    restrictions_list.clear();
    way_start_end_id_list.clear();
}

void ExtractionContainers::PrepareData(const std::string &output_file_name,
                                       const std::string &restrictions_file_name)
{
    try
    {
        unsigned number_of_used_nodes = 0;
        unsigned number_of_used_edges = 0;

        std::cout << "[extractor] Sorting used nodes        ... " << std::flush;
        TIMER_START(sorting_used_nodes);
        stxxl::sort(used_node_id_list.begin(), used_node_id_list.end(), Cmp(), stxxl_memory);
        TIMER_STOP(sorting_used_nodes);
        std::cout << "ok, after " << TIMER_SEC(sorting_used_nodes) << "s" << std::endl;

        std::cout << "[extractor] Erasing duplicate nodes   ... " << std::flush;
        TIMER_START(erasing_dups);
        auto new_end = std::unique(used_node_id_list.begin(), used_node_id_list.end());
        used_node_id_list.resize(new_end - used_node_id_list.begin());
        TIMER_STOP(erasing_dups);
        std::cout << "ok, after " << TIMER_SEC(erasing_dups) << "s" << std::endl;

        std::cout << "[extractor] Sorting all nodes         ... " << std::flush;
        TIMER_START(sorting_nodes);
        stxxl::sort(all_nodes_list.begin(),
                    all_nodes_list.end(),
                    ExternalMemoryNodeSTXXLCompare(),
                    stxxl_memory);
        TIMER_STOP(sorting_nodes);
        std::cout << "ok, after " << TIMER_SEC(sorting_nodes) << "s" << std::endl;

        std::cout << "[extractor] Sorting used ways         ... " << std::flush;
        TIMER_START(sort_ways);
        stxxl::sort(way_start_end_id_list.begin(),
                    way_start_end_id_list.end(),
                    FirstAndLastSegmentOfWayStxxlCompare(),
                    stxxl_memory);
        TIMER_STOP(sort_ways);
        std::cout << "ok, after " << TIMER_SEC(sort_ways) << "s" << std::endl;

        std::cout << "[extractor] Sorting " << restrictions_list.size() << " restrictions. by from... " << std::flush;
        TIMER_START(sort_restrictions);
        stxxl::sort(restrictions_list.begin(),
                    restrictions_list.end(),
                    CmpRestrictionContainerByFrom(),
                    stxxl_memory);
        TIMER_STOP(sort_restrictions);
        std::cout << "ok, after " << TIMER_SEC(sort_restrictions) << "s" << std::endl;

        std::cout << "[extractor] Fixing restriction starts ... " << std::flush;
        TIMER_START(fix_restriction_starts);
        auto restrictions_iterator = restrictions_list.begin();
        auto way_start_and_end_iterator = way_start_end_id_list.begin();

        while (way_start_and_end_iterator != way_start_end_id_list.end() &&
               restrictions_iterator != restrictions_list.end())
        {
            if (way_start_and_end_iterator->way_id < restrictions_iterator->restriction.from.way)
            {
                ++way_start_and_end_iterator;
                continue;
            }

            if (way_start_and_end_iterator->way_id > restrictions_iterator->restriction.from.way)
            {
                ++restrictions_iterator;
                continue;
            }

            BOOST_ASSERT(way_start_and_end_iterator->way_id == restrictions_iterator->restriction.from.way);
            const NodeID via_node_id = restrictions_iterator->restriction.via.node;

            if (way_start_and_end_iterator->first_segment_source_id == via_node_id)
            {
                restrictions_iterator->restriction.from.node =
                    way_start_and_end_iterator->first_segment_source_id;
            }
            else if (way_start_and_end_iterator->first_segment_source_id == via_node_id)
            {
                restrictions_iterator->restriction.from.node =
                    way_start_and_end_iterator->first_segment_source_id;
            }
            else if (way_start_and_end_iterator->last_segment_source_id == via_node_id)
            {
                restrictions_iterator->restriction.from.node =
                    way_start_and_end_iterator->last_segment_target_id;
            }
            else if (way_start_and_end_iterator->last_segment_target_id == via_node_id)
            {
                restrictions_iterator->restriction.from.node = way_start_and_end_iterator->last_segment_source_id;
            }
            ++restrictions_iterator;
        }

        TIMER_STOP(fix_restriction_starts);
        std::cout << "ok, after " << TIMER_SEC(fix_restriction_starts) << "s" << std::endl;

        std::cout << "[extractor] Sorting restrictions. by to  ... " << std::flush;
        TIMER_START(sort_restrictions_to);
        stxxl::sort(restrictions_list.begin(),
                    restrictions_list.end(),
                    CmpRestrictionContainerByTo(),
                    stxxl_memory);
        TIMER_STOP(sort_restrictions_to);
        std::cout << "ok, after " << TIMER_SEC(sort_restrictions_to) << "s" << std::endl;

        unsigned number_of_useable_restrictions = 0;
        std::cout << "[extractor] Fixing restriction ends   ... " << std::flush;
        TIMER_START(fix_restriction_ends);
        restrictions_iterator = restrictions_list.begin();
        way_start_and_end_iterator = way_start_end_id_list.begin();
        while (way_start_and_end_iterator != way_start_end_id_list.end() &&
               restrictions_iterator != restrictions_list.end())
        {
            if (way_start_and_end_iterator->way_id < restrictions_iterator->restriction.to.way)
            {
                ++way_start_and_end_iterator;
                continue;
            }
            if (way_start_and_end_iterator->way_id > restrictions_iterator->restriction.to.way)
            {
                ++restrictions_iterator;
                continue;
            }
            NodeID via_node_id = restrictions_iterator->restriction.via.node;
            if (way_start_and_end_iterator->last_segment_source_id == via_node_id)
            {
                restrictions_iterator->restriction.to.node = way_start_and_end_iterator->last_segment_target_id;
            }
            else if (way_start_and_end_iterator->last_segment_target_id == via_node_id)
            {
                restrictions_iterator->restriction.to.node = way_start_and_end_iterator->last_segment_source_id;
            }
            else if (way_start_and_end_iterator->first_segment_source_id == via_node_id)
            {
                restrictions_iterator->restriction.to.node = way_start_and_end_iterator->first_segment_target_id;
            }
            else if (way_start_and_end_iterator->first_segment_target_id == via_node_id)
            {
                restrictions_iterator->restriction.to.node = way_start_and_end_iterator->first_segment_source_id;
            }

            if (std::numeric_limits<unsigned>::max() != restrictions_iterator->restriction.from.node &&
                std::numeric_limits<unsigned>::max() != restrictions_iterator->restriction.to.node)
            {
                ++number_of_useable_restrictions;
            }
            ++restrictions_iterator;
        }
        TIMER_STOP(fix_restriction_ends);
        std::cout << "ok, after " << TIMER_SEC(fix_restriction_ends) << "s" << std::endl;

        SimpleLogger().Write() << "usable restrictions: " << number_of_useable_restrictions;
        // serialize restrictions
        std::ofstream restrictions_out_stream;
        restrictions_out_stream.open(restrictions_file_name.c_str(), std::ios::binary);
        restrictions_out_stream.write((char *)&fingerprint, sizeof(FingerPrint));
        restrictions_out_stream.write((char *)&number_of_useable_restrictions, sizeof(unsigned));

        for(const auto & restriction_container : restrictions_list)
        {
            if (std::numeric_limits<unsigned>::max() != restriction_container.restriction.from.node &&
                std::numeric_limits<unsigned>::max() != restriction_container.restriction.to.node)
            {
                restrictions_out_stream.write((char *)&(restriction_container.restriction),
                                              sizeof(TurnRestriction));
            }
        }
        restrictions_out_stream.close();

        std::ofstream file_out_stream;
        file_out_stream.open(output_file_name.c_str(), std::ios::binary);
        file_out_stream.write((char *)&fingerprint, sizeof(FingerPrint));
        file_out_stream.write((char *)&number_of_used_nodes, sizeof(unsigned));
        std::cout << "[extractor] Confirming/Writing used nodes     ... " << std::flush;
        TIMER_START(write_nodes);
        // identify all used nodes by a merging step of two sorted lists
        auto node_iterator = all_nodes_list.begin();
        auto node_id_iterator = used_node_id_list.begin();
        while (node_id_iterator != used_node_id_list.end() && node_iterator != all_nodes_list.end())
        {
            if (*node_id_iterator < node_iterator->node_id)
            {
                ++node_id_iterator;
                continue;
            }
            if (*node_id_iterator > node_iterator->node_id)
            {
                ++node_iterator;
                continue;
            }
            BOOST_ASSERT(*node_id_iterator == node_iterator->node_id);

            file_out_stream.write((char *)&(*node_iterator), sizeof(ExternalMemoryNode));

            ++number_of_used_nodes;
            ++node_id_iterator;
            ++node_iterator;
        }

        TIMER_STOP(write_nodes);
        std::cout << "ok, after " << TIMER_SEC(write_nodes) << "s" << std::endl;

        std::cout << "[extractor] setting number of nodes   ... " << std::flush;
        std::ios::pos_type previous_file_position = file_out_stream.tellp();
        file_out_stream.seekp(std::ios::beg + sizeof(FingerPrint));
        file_out_stream.write((char *)&number_of_used_nodes, sizeof(unsigned));
        file_out_stream.seekp(previous_file_position);

        std::cout << "ok" << std::endl;

        // Sort edges by start.
        std::cout << "[extractor] Sorting edges by start    ... " << std::flush;
        TIMER_START(sort_edges_by_start);
        stxxl::sort(all_edges_list.begin(), all_edges_list.end(), CmpEdgeByStartID(), stxxl_memory);
        TIMER_STOP(sort_edges_by_start);
        std::cout << "ok, after " << TIMER_SEC(sort_edges_by_start) << "s" << std::endl;

        std::cout << "[extractor] Setting start coords      ... " << std::flush;
        TIMER_START(set_start_coords);
        file_out_stream.write((char *)&number_of_used_edges, sizeof(unsigned));
        // Traverse list of edges and nodes in parallel and set start coord
        node_iterator = all_nodes_list.begin();
        auto edge_iterator = all_edges_list.begin();
        while (edge_iterator != all_edges_list.end() && node_iterator != all_nodes_list.end())
        {
            if (edge_iterator->start < node_iterator->node_id)
            {
                ++edge_iterator;
                continue;
            }
            if (edge_iterator->start > node_iterator->node_id)
            {
                node_iterator++;
                continue;
            }

            BOOST_ASSERT(edge_iterator->start == node_iterator->node_id);
            edge_iterator->source_coordinate.lat = node_iterator->lat;
            edge_iterator->source_coordinate.lon = node_iterator->lon;
            ++edge_iterator;
        }
        TIMER_STOP(set_start_coords);
        std::cout << "ok, after " << TIMER_SEC(set_start_coords) << "s" << std::endl;

        // Sort Edges by target
        std::cout << "[extractor] Sorting edges by target   ... " << std::flush;
        TIMER_START(sort_edges_by_target);
        stxxl::sort(all_edges_list.begin(), all_edges_list.end(), CmpEdgeByTargetID(), stxxl_memory);
        TIMER_STOP(sort_edges_by_target);
        std::cout << "ok, after " << TIMER_SEC(sort_edges_by_target) << "s" << std::endl;

        std::cout << "[extractor] Setting target coords     ... " << std::flush;
        TIMER_START(set_target_coords);
        // Traverse list of edges and nodes in parallel and set target coord
        node_iterator = all_nodes_list.begin();
        edge_iterator = all_edges_list.begin();

        while (edge_iterator != all_edges_list.end() && node_iterator != all_nodes_list.end())
        {
            if (edge_iterator->target < node_iterator->node_id)
            {
                ++edge_iterator;
                continue;
            }
            if (edge_iterator->target > node_iterator->node_id)
            {
                ++node_iterator;
                continue;
            }
            BOOST_ASSERT(edge_iterator->target == node_iterator->node_id);
            if (edge_iterator->source_coordinate.lat != std::numeric_limits<int>::min() &&
                edge_iterator->source_coordinate.lon != std::numeric_limits<int>::min())
            {
                BOOST_ASSERT(edge_iterator->speed != -1);
                edge_iterator->target_coordinate.lat = node_iterator->lat;
                edge_iterator->target_coordinate.lon = node_iterator->lon;

                const double distance = FixedPointCoordinate::ApproximateEuclideanDistance(
                    edge_iterator->source_coordinate.lat,
                    edge_iterator->source_coordinate.lon,
                    node_iterator->lat,
                    node_iterator->lon);

                const double weight = (distance * 10.) / (edge_iterator->speed / 3.6);
                int integer_weight = std::max(
                    1,
                    (int)std::floor(
                        (edge_iterator->is_duration_set ? edge_iterator->speed : weight) + .5));
                int integer_distance = std::max(1, (int)distance);
                short zero = 0;
                short one = 1;

                file_out_stream.write((char *)&edge_iterator->start, sizeof(unsigned));
                file_out_stream.write((char *)&edge_iterator->target, sizeof(unsigned));
                file_out_stream.write((char *)&integer_distance, sizeof(int));
                switch (edge_iterator->direction)
                {
                case ExtractionWay::notSure:
                    file_out_stream.write((char *)&zero, sizeof(short));
                    break;
                case ExtractionWay::oneway:
                    file_out_stream.write((char *)&one, sizeof(short));
                    break;
                case ExtractionWay::bidirectional:
                    file_out_stream.write((char *)&zero, sizeof(short));
                    break;
                case ExtractionWay::opposite:
                    file_out_stream.write((char *)&one, sizeof(short));
                    break;
                default:
                    throw osrm::exception("edge has broken direction");
                }

                file_out_stream.write((char *)&integer_weight, sizeof(int));
                file_out_stream.write((char *)&edge_iterator->name_id, sizeof(unsigned));
                file_out_stream.write((char *)&edge_iterator->is_roundabout, sizeof(bool));
                file_out_stream.write((char *)&edge_iterator->is_in_tiny_cc, sizeof(bool));
                file_out_stream.write((char *)&edge_iterator->is_access_restricted, sizeof(bool));

                // cannot take adress of bit field, so use local
                const TravelMode  travel_mode = edge_iterator->travel_mode;
                file_out_stream.write((char *)&travel_mode, sizeof(TravelMode));

                file_out_stream.write((char *)&edge_iterator->is_split, sizeof(bool));
                ++number_of_used_edges;
            }
            ++edge_iterator;
        }
        TIMER_STOP(set_target_coords);
        std::cout << "ok, after " << TIMER_SEC(set_target_coords) << "s" << std::endl;

        std::cout << "[extractor] setting number of edges   ... " << std::flush;

        file_out_stream.seekp(previous_file_position);
        file_out_stream.write((char *)&number_of_used_edges, sizeof(unsigned));
        file_out_stream.close();
        std::cout << "ok" << std::endl;

        std::cout << "[extractor] writing street name index ... " << std::flush;
        TIMER_START(write_name_index);
        std::string name_file_streamName = (output_file_name + ".names");
        boost::filesystem::ofstream name_file_stream(name_file_streamName, std::ios::binary);

        unsigned total_length = 0;
        std::vector<unsigned> name_lengths;
        for (const std::string &temp_string : name_list)
        {
            const unsigned string_length = std::min(static_cast<unsigned>(temp_string.length()), 255u);
            name_lengths.push_back(string_length);
            total_length += string_length;
        }

        RangeTable<> table(name_lengths);
        name_file_stream << table;

        name_file_stream.write((char*) &total_length, sizeof(unsigned));
        // write all chars consecutively
        for (const std::string &temp_string : name_list)
        {
            const unsigned string_length = std::min(static_cast<unsigned>(temp_string.length()), 255u);
            name_file_stream.write(temp_string.c_str(), string_length);
        }

        name_file_stream.close();
        TIMER_STOP(write_name_index);
        std::cout << "ok, after " << TIMER_SEC(write_name_index) << "s" << std::endl;

        SimpleLogger().Write() << "Processed " << number_of_used_nodes << " nodes and "
                               << number_of_used_edges << " edges";
    }
    catch (const std::exception &e) { std::cerr << "Caught Execption:" << e.what() << std::endl; }
}

max_stars_repo_name: VRPTools/pkg-osrm-backend

max_issues_repo_name: VRPTools/pkg-osrm-backend

max_forks_repo_name: VRPTools/pkg-osrm-backend


text:
// morpho/cdl/bgl_expansions/algorithms/ullmann.hpp header file//
// Copyright (c) 2003-2008 Vladimir J. Sykora
// Copyright (c) 2007-2008 Vladimir J. Sykora and NCU Studies Ltd
// Modifications by Greg Landrum, January 2009
//
//*****************************************************************************
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

//*****************************************************************************

//-----------------------------------------------------------------------------

// Algorithm due to JR Ullmann. "An Algorithm for Subgraph
// Isomorphism". Journal of the Association for
// Computing Machinery, Vol 23, No.1, January 1976, pp 31-42.

#ifndef MORPHO_CDL_BGL_EXP_ULLMANN_HPP
#define MORPHO_CDL_BGL_EXP_ULLMANN_HPP

// -------- boost
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/graph/graph_traits.hpp>
// -------- std
#include <utility>    // for std::pair<>
#include <algorithm>

namespace boost {
  namespace detail {

    template <class Graph, class UblasMatrix, class BackInsertionSequence, class EdgeLabeling>
    bool forward_checking(const Graph& g1, const Graph& g2,
                          UblasMatrix& M, size_t count, BackInsertionSequence& F, size_t num_vert_g1,
                          size_t num_vert_g2, EdgeLabeling& edge_labeling){
      typedef std::pair<typename graph_traits<Graph>::edge_descriptor,bool> edge_presence;
      typename BackInsertionSequence::iterator   fi, fi_end=F.end();
      for (size_t k=count+1; k<num_vert_g1; ++k) {
        for (size_t l=0; l<num_vert_g2; ++l) {
          if(!M(k,l)) continue;
          // check mapping:
          fi=F.begin();
          while(fi!=fi_end) {
            bool flag1(0), flag1_1(0), flag2(0), flag2_1(0);
            edge_presence ep1=edge(k,fi->first,g1);
            if(ep1.second) {
              flag1_1=true;
              edge_presence ep2=edge(l,fi->second,g2);
              if(ep2.second) flag1=edge_labeling(ep1.first,ep2.first);
            }
            if(flag1_1 && flag1) {
              M(k,l)=1;
              ++fi;
              continue;
            }
            edge_presence ep2=edge(l,fi->second,g2);
            if(ep2.second) {
              flag2_1=true;
              ep1=edge(k,fi->first,g1);
              if(ep1.second){
                flag2=edge_labeling(ep1.first,ep2.first);
              } else {
                // ring closed in main structure, not closed in query. This should pass
                flag2=true;
              }
            }
            if(flag2_1 && flag2) {  //if one edge exists, there must be a mapping
              M(k,l)=1;
            }
            else if ( !flag1_1 && !flag2_1 ) { // or both edges are not present
              M(k,l)=1;
            }
            else M(k,l)=0;         // if not, there's no mapping
            ++fi;
          }
        }
      }
      // TODO: change the data structure of the M matrix to sparse matrix. This wouldn't be necessary
      size_t cero_row(0);
      for (size_t k=0; k<num_vert_g1; ++k) {
        for (size_t l=0; l<num_vert_g2; ++l) {
          if(M(k,l)) break;
          else ++cero_row;
        }
        if(cero_row==num_vert_g2) return false; // if there is a cero row
        cero_row=0;
      }
      return true;
    }

    template <class Graph, class EdgeLabeling, class UblasMatrix, class BackInsertionSequence>
    bool backtrack(const Graph& g1, const Graph& g2, size_t count,
                   const UblasMatrix& M, BackInsertionSequence& F, const size_t num_vert_g1,
                   const size_t num_vert_g2, EdgeLabeling& edge_labeling){
      if(count==num_vert_g1) return true;
      for (size_t i=0; i<num_vert_g2; ++i) {
        if(M(count,i)) {
          F.push_back(std::make_pair(count,i));
          UblasMatrix M_prime(M);
          for (size_t m=count+1; m<num_vert_g1; ++m) {
            M_prime(m,i)=0;
          }

          if (forward_checking(g1,g2,M_prime,count,F,num_vert_g1,num_vert_g2,edge_labeling)){
            if(backtrack(g1,g2,count+1,M_prime,F,num_vert_g1,num_vert_g2,edge_labeling)){
              return true;
            }
          }
          F.erase(std::remove(F.begin(),F.end(),std::make_pair(count,i)), F.end());
        }
      }
      return false;
    }

    template <class Graph, class EdgeLabeling, class UblasMatrix, class DoubleBackInsertionSequence>
    void backtrack_all(const Graph& g1, const Graph& g2, size_t count,
                       const UblasMatrix& M, DoubleBackInsertionSequence& FF, const size_t num_vert_g1,
                       const size_t num_vert_g2, EdgeLabeling& edge_labeling) {
      if(count==num_vert_g1) return;
      DoubleBackInsertionSequence holdFF;
      holdFF.insert(holdFF.begin(),FF.begin(),FF.end());
      FF.clear();
      for (size_t i=0; i<num_vert_g2; ++i) {
        if(M(count,i)) {
          DoubleBackInsertionSequence tFF;

          UblasMatrix M_prime(M);
          for (size_t m=count+1; m<num_vert_g1; ++m){
            M_prime(m,i)=0;
          }

          if(holdFF.size()){
            for(typename DoubleBackInsertionSequence::const_iterator iter=holdFF.begin();
                iter!=holdFF.end();++iter){
              typename DoubleBackInsertionSequence::value_type F=*iter;
              F.push_back(std::make_pair(count,i));
              if (forward_checking(g1,g2,M_prime,count,F,num_vert_g1,num_vert_g2,edge_labeling)){
                tFF.push_back(F);
              }
            }
          } else {
            typename DoubleBackInsertionSequence::value_type F;
            F.push_back(std::make_pair(count,i));
            if (forward_checking(g1,g2,M_prime,count,F,num_vert_g1,num_vert_g2,edge_labeling)){
              tFF.push_back(F);
            }
          }
          backtrack_all(g1,g2,count+1,M_prime,tFF,num_vert_g1,num_vert_g2,edge_labeling);
          if(tFF.size()){
            for(typename DoubleBackInsertionSequence::const_iterator iter=tFF.begin();
                iter!=tFF.end();++iter){
              FF.push_back(*iter);
            }
          }
        }
      }
    }

    template <  class Graph
                , class VertexLabeling    // binary predicate
                ,  class UblasMatrix
                >
    void prepareM(const Graph& g1, const Graph& g2,
                  VertexLabeling& vertex_labeling,UblasMatrix &M){
      size_t rows(num_vertices(g1));
      size_t cols(num_vertices(g2));
      M.resize(rows,cols);
      // initialize the matrix:
      for (size_t i=0; i<rows; ++i) {
        for (size_t j=0; j<cols; ++j) {
          if(out_degree(j,g2)>=out_degree(i,g1) && vertex_labeling(i,j)) {
            M(i,j)=1;
          }
          else M(i,j)=0;
        }
      }
    }
  }  // namespace detail

  // test if g1 is a subgraph of g2. mapped vertices are returned in F
  // Mapping : first: g1 vertices, second : g2 vertices
  // O( num_vertices(g1)! num_vertices(g1) ^ 3 )
  // This function doesnt Work with filtered graphs!
  // The size of F doesn't indicate match!
  template <  class Graph
              , class VertexLabeling    // binary predicate
              , class EdgeLabeling      // binary predicate
              , class BackInsertionSequence   // contains std::pair<vertex_descriptor,vertex_descriptor>
              >
  bool ullmann(const Graph& g1, const Graph& g2,
               VertexLabeling& vertex_labeling, EdgeLabeling& edge_labeling, BackInsertionSequence& F) {
    typedef ::boost::numeric::ublas::matrix<int>   matrix_t;
    size_t rows(num_vertices(g1));
    size_t cols(num_vertices(g2));
    matrix_t M;
    detail::prepareM(g1,g2,vertex_labeling,M);
    size_t    count(0);
    return detail::backtrack(g1,g2,count,M,F,rows,cols,edge_labeling);
  }

  // test if g1 is a subgraph of g2.
  // F returns all mappings of g1 in g2. mapping in separate containers
  template <  class Graph
              , class VertexLabeling    // binary predicate
              , class EdgeLabeling      // binary predicate
              , class DoubleBackInsertionSequence   // contains a back insertion sequence
              >
  bool ullmann_all(const Graph& g1, const Graph& g2,
                   VertexLabeling& vertex_labeling, EdgeLabeling& edge_labeling, DoubleBackInsertionSequence& F) {
    typedef ::boost::numeric::ublas::matrix<int>   matrix_t;
    size_t rows(num_vertices(g1));
    size_t cols(num_vertices(g2));
    matrix_t M;
    detail::prepareM(g1,g2,vertex_labeling,M);
    size_t    count(0);
    detail::backtrack_all(g1,g2,count,M,F,rows,cols,edge_labeling);
    return !F.empty();
  }

}  // namespace boost

#endif // MORPHO_CDL_BGL_EXP_ULLMANN_HPP

max_stars_repo_name: jungb-basf/rdkit

max_issues_repo_name: jungb-basf/rdkit

max_forks_repo_name: jungb-basf/rdkit


text:
#ifndef PYTHONIC_INCLUDE_NUMPY_REDUCE_HPP
#define PYTHONIC_INCLUDE_NUMPY_REDUCE_HPP

#include "pythonic/types/ndarray.hpp"
#include "pythonic/__builtin__/None.hpp"
#include "pythonic/__builtin__/ValueError.hpp"
#include "pythonic/utils/neutral.hpp"

#ifdef USE_BOOST_SIMD
#include <boost/simd/include/functions/broadcast.hpp>
#include <boost/simd/memory/functions/aligned_store.hpp>
#endif

#include <algorithm>

namespace pythonic
{

  namespace numpy
  {

    template <class Op, size_t N, bool vector_form>
    struct _reduce;

    template <class Op, bool vector_form>
    struct _reduce<Op, 1, vector_form> {
      template <class E, class F>
      F operator()(E e, F acc);
    };

#ifdef USE_BOOST_SIMD
    template <class Op>
    struct _reduce<Op, 1, true> {
      template <class E, class F>
      F operator()(E e, F acc);
    };
#endif

    template <class Op, size_t N, bool vector_form>
    struct _reduce {
      template <class E, class F>
      F operator()(E e, F acc);
    };

    namespace
    {
      template <class E>
      using reduce_result_type = typename std::conditional<
          std::is_same<typename E::dtype, bool>::value, long,
          typename E::dtype>::type;
    }

    template <class Op, class E>
    reduce_result_type<E> reduce(E const &expr,
                                 types::none_type _ = types::none_type());

    template <class Op, class E>
    auto reduce(E const &array, long axis) ->
        typename std::enable_if<E::value == 1,
                                decltype(reduce<Op>(array))>::type;

    namespace
    {
      template <class E>
      using reduced_type = types::ndarray<typename E::dtype, E::value - 1>;
    }

    template <class Op, class E>
    typename std::enable_if<E::value != 1, reduced_type<E>>::type
    reduce(E const &array, long axis);
  }
}

#endif


text:
#include <boost/numeric/interval.hpp>

int
main ()
{
  return 0;
}