text:
#include <blitzml/smooth_loss/squared_hinge_loss.h>
#include <blitzml/base/math_util.h>
#include <math.h>

namespace BlitzML {

value_t SquaredHingeLoss::compute_loss(value_t a_dot_omega, value_t label) const {
  value_t one_minus_label_times_aomega = 1 - label * a_dot_omega;
  if (one_minus_label_times_aomega <= 0.)
    return 0;
  return 0.5 * sq(one_minus_label_times_aomega);
}

value_t SquaredHingeLoss::compute_conjugate(value_t dual_variable,
                                        value_t label) const {
  if (label == 0.) return -0.5;
  value_t one_plus_ratio = 1 + dual_variable / label;
  return (sq(one_plus_ratio) - 1) / 2;
}

value_t SquaredHingeLoss::compute_deriative(value_t a_dot_omega,
                                        value_t label) const {
  value_t one_minus_label_times_aomega = 1 - label * a_dot_omega;
  if (one_minus_label_times_aomega <= 0.)
    return 0.;
  return -label * one_minus_label_times_aomega;
}

value_t SquaredHingeLoss::compute_2nd_derivative(value_t a_dot_omega,
                                             value_t label) const {
  value_t one_minus_label_times_aomega = 1 - label * a_dot_omega;
  if (one_minus_label_times_aomega <= 0.) {
    return MIN_SMOOTH_LOSS_2ND_DERIVATIVE;
  } else {
    return sq(label);
  }
}

value_t SquaredHingeLoss::lipschitz_constant() const {
  return 1;
}

} // namespace BlitzML

ext: cpp

max_stars_repo_name: tbjohns/BlitzML

max_issues_repo_name: tbjohns/BlitzML

max_forks_repo_name: tbjohns/BlitzML


text:
// Software License for MTL
//
// Copyright (c) 2007 The Trustees of Indiana University.
//               2008 Dresden University of Technology and the Trustees of Indiana University.
//               2010 SimuNova UG (haftungsbeschränkt), www.simunova.com.
// All rights reserved.
// Authors: Peter Gottschling and Andrew Lumsdaine
//
// This file is part of the Matrix Template Library
//
// See also license.mtl.txt in the distribution.

#include <iostream>
#include <boost/timer.hpp>
#include <boost/numeric/mtl/mtl.hpp>

using namespace std;

template <typename Matrix, typename Vector>
typename mtl::Collection<Matrix>::value_type
matgen (Matrix& A, Vector& b)
{
    size_t                                        n= num_rows(A);
    typename mtl::Collection<Matrix>::value_type  norma= 0, tmp;
    int                                           init= 1325;

    for (size_t i = 0; i < n; i++)
	for (size_t j = 0; j < n; j++) {
	    init = 3125*init % 65536;
	    A[j][i]= tmp= (init - 32768.0) / 16384.0;
	    norma = tmp > norma ? tmp : norma;
	}
    for (size_t i = 0; i < n; i++)
	b[i]= mtl::sum(A[i][mtl::iall]);

    return norma;
}

template <typename Matrix>
void test(Matrix& A, const char* name)
{
    cout << "\n" << name << "\n";

    typedef typename mtl::Collection<Matrix>::value_type  Scalar;
    typedef typename mtl::dense_vector<Scalar>            Vector;

    Vector b(num_rows(A), 0.0), x;
    matgen(A, b);

    boost::timer s;
    x= lu_solve(A, b);
    double t= s.elapsed(), n= num_rows(A), ops= 2./3.*n*n*n + 2*n*n;
    std::cout << "LU solution took " << t << "s. This corresponds to "
	      << ops / t / 1e6 << " MFlops.\n";

#if 0
    cout << "A is\n" << A << '\n';
    cout << "b is " << b << '\n';
    cout << "x is " << x << '\n';

    b= A * x;
    cout << "A*x is " << b << '\n';
#endif
}

int main(int, char**)
{
    using namespace mtl;
    unsigned size= 1000;

    dense2D<double>                                      dr(size, size);
    dense2D<complex<double> >                            dz(size, size);
    dense2D<double, mat::parameters<col_major> >      dc(size, size);

    test(dr, "Row-major dense");
    //test(dz, "Row-major dense with complex numbers");
    test(dc, "Column-major dense");

    return 0;
}


text:
// Copyright (C) 2014-2016 Vicente J. Botet Escriba
//
//  Distributed under the Boost Software License, Version 1.0. (See accompanying
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

// <experimental/make.hpp>

//  template <template <class ...> class M, class X>
//  auto make(X&& x);
//  template <class M, class X>
//  auto make(X&& x);
//  template <class M, class ...Args>
//  auto make(Args&& ...args);

#include <experimental/make.hpp>
#include <experimental/meta.hpp>
#include <experimental/future.hpp>

#include <boost/detail/lightweight_test.hpp>

struct A
{
  int v;
  A(): v(3) {}
  A(int v): v(std::move(v)) {}
  A(int v1, int v2): v(v1+v2) {}
};

int main()
{
  namespace stde = std::experimental;
  {
    int v=0;
    std::shared_future<int> x = stde::make<std::shared_future>(v);
    BOOST_TEST(x.get() == 0);
  }
  {
    int v=0;
    std::shared_future<int> x = stde::make<std::shared_future<int>>(v);
    BOOST_TEST(x.get() == 0);
  }
  {
    int v=1;
    std::shared_future<A> x = stde::emplace<std::shared_future<A>>(v,v);
    BOOST_TEST(x.get().v == 2);
  }
  {
    std::shared_future<int> x = stde::emplace<std::shared_future<int>>();
    BOOST_TEST_EQ(x.get(),  0);
  }
#if 0
  {
    //this fail as v is deduce as int
    // todo: move to failing tests
    int v=0;
    std::shared_future<int&> x = stde::make<std::shared_future<int&>>(v);
    BOOST_TEST(&x.get() == &v);
  }
  {
    //this fail as v is deduce as int
    // todo: move to failing tests
    int v=0;
    std::shared_future<int&> x = stde::make<std::shared_future<stde::_t&>>(v);
    BOOST_TEST(&x.get() == &v);
  }
#endif
  {
    int v=0;
    std::shared_future<int> x = stde::make<std::shared_future<stde::_t>>(v);
    BOOST_TEST(x.get() == 0);
  }
  {
    int v=0;
    auto x = stde::make<std::shared_future<stde::_t>>(std::ref(v));
    BOOST_TEST(&x.get() == &v);
  }
  {
    int v=0;
    auto x = stde::make<std::shared_future>(std::ref(v));
    BOOST_TEST(&x.get() == &v);
  }
  return ::boost::report_errors();
}

max_stars_repo_name: jwakely/std-make

max_issues_repo_name: jwakely/std-make

max_forks_repo_name: jwakely/std-make


text:
/**
 * @author Andre Anjos <andre.anjos@idiap.ch>
 * @date Wed 20 Jan 2016 13:43:56 CET
 *
 * @brief Implements an audio format reader for Bob
 *
 * Copyright (C) 2011-2016 Idiap Research Institute, Martigny, Switzerland
 */

#include <set>

#include <boost/filesystem.hpp>
#include <boost/make_shared.hpp>

#include <bob.io.base/blitz_array.h>

#include <bob.io.base/CodecRegistry.h>
#include <bob.io.base/File.h>

#include "cpp/reader.h"
#include "cpp/writer.h"

class AudioFile: public bob::io::base::File {

  public: //api

    AudioFile(const std::string& path, char mode):
      m_filename(path),
      m_newfile(true) {

        if (mode == 'r') {
          m_reader = boost::make_shared<bob::io::audio::Reader>(m_filename.c_str());
          m_newfile = false;
        }
        else if (mode == 'a' && boost::filesystem::exists(path)) {
          // to be able to append must load all data and save in audio::Writer
          m_reader = boost::make_shared<bob::io::audio::Reader>(m_filename.c_str());
          bob::io::base::array::blitz_array data(m_reader->type());
          m_reader->load(data);
          auto rate = m_reader->rate();
          auto encoding = m_reader->encoding();
          auto bps = m_reader->bitsPerSample();
          m_reader.reset(); ///< cleanup before truncating the file
          m_writer = boost::make_shared<bob::io::audio::Writer>(m_filename.c_str(), rate, encoding, bps);
          m_writer->append(data); ///< we are now ready to append
          m_newfile = false;
        }
        else { //mode is 'w'
          m_newfile = true;
        }

      }

    virtual ~AudioFile() { }

    virtual const char* filename() const {
      return m_filename.c_str();
    }

    virtual const bob::io::base::array::typeinfo& type_all() const {
      return (m_reader)? m_reader->type() : m_writer->type();
    }

    virtual const bob::io::base::array::typeinfo& type() const {
      return (m_reader)? m_reader->type() : m_writer->type();
    }

    virtual size_t size() const {
      return (m_reader)? 1:(!m_newfile);
    }

    virtual const char* name() const {
      return s_codecname.c_str();
    }

    virtual void read_all(bob::io::base::array::interface& buffer) {
      read(buffer, 0); ///we only have 1 audio in a audio file anyways
    }

    virtual void read(bob::io::base::array::interface& buffer, size_t index) {

      if (index != 0)
        throw std::runtime_error("can only read all samples at once in audio codecs");

      if (!m_reader)
        throw std::runtime_error("can only read if opened audio in 'r' mode");

      if(!buffer.type().is_compatible(m_reader->type()))
        buffer.set(m_reader->type());

      m_reader->load(buffer);
    }

    virtual size_t append (const bob::io::base::array::interface& buffer) {

      const bob::io::base::array::typeinfo& type = buffer.type();

      if (type.nd != 1 && type.nd != 2)
        throw std::runtime_error("input buffer for audio input must have either 1 (channel values for 1 sample) or 2 dimensions (channels, samples)");

      if(m_newfile) {
        m_writer = boost::make_shared<bob::io::audio::Writer>(m_filename.c_str());
      }

      if(!m_writer)
        throw std::runtime_error("can only read if open audio in 'a' or 'w' modes");

      m_writer->append(buffer);
      return 1;
    }

    virtual void write (const bob::io::base::array::interface& buffer) {

      append(buffer);

    }

  private: //representation
    std::string m_filename;
    bool m_newfile;
    boost::shared_ptr<bob::io::audio::Reader> m_reader;
    boost::shared_ptr<bob::io::audio::Writer> m_writer;

    static std::string s_codecname;

};

std::string AudioFile::s_codecname = "bob.audio";

/**
 * From this point onwards we have the registration procedure. If you are
 * looking at this file for a coding example, just follow the procedure bellow,
 * minus local modifications you may need to apply.
 */

/**
 * This defines the factory method F that can create codecs of this type.
 *
 * Here are the meanings of the mode flag that should be respected by your
 * factory implementation:
 *
 * 'r': opens for reading only - no modifications can occur; it is an
 *      error to open a file that does not exist for read-only operations.
 * 'w': opens for reading and writing, but truncates the file if it
 *      exists; it is not an error to open files that do not exist with
 *      this flag.
 * 'a': opens for reading and writing - any type of modification can
 *      occur. If the file does not exist, this flag is effectively like
 *      'w'.
 *
 * Returns a newly allocated File object that can read and write data to the
 * file using a specific backend.
 *
 * @note: This method can be static.
 */
boost::shared_ptr<bob::io::base::File> make_file (const char* path, char mode) {
  return boost::make_shared<AudioFile>(path, mode);
}


text:
#include <boost/msm/front/state_machine_def.hpp>
#include <boost/msm/back/state_machine.hpp>
#include <string_view>
#include <string>
#include <iostream>

using namespace boost;
using namespace std;

struct player_def
    : msm::front::state_machine_def<player_def>
{
    using self = player_def;

    // states
    struct stopped : msm::front::state<> {};
    struct playing : msm::front::state<> {};
    struct paused : msm::front::state<> {};

    // events
    struct play_event {};
    struct stop_event {};
    struct pause_event {};

    void do_play( play_event const& )   { cout << "play" << endl; }
    void do_stop( stop_event const& )   { cout << "stop" << endl; }
    void do_pause( pause_event const& ) { cout << "pause" << endl; }
    void do_resume( play_event const& ) { cout << "resume" << endl; }

    using transition_table = mpl::vector<
          a_row<stopped, play_event,  playing, &self::do_play>
        , a_row<playing, stop_event,  stopped, &self::do_stop>
        , a_row<playing, pause_event, paused,  &self::do_pause>
        , a_row<paused,  play_event,  playing, &self::do_resume>
        , a_row<paused,  stop_event,  stopped, &self::do_stop>
        >;

    using initial_state = stopped;
};

using player = msm::back::state_machine<player_def>;

int main()
{
    player p;
    p.start();
    p.process_event( player::play_event() );
    p.process_event( player::pause_event() );
    p.process_event( player::play_event() );
    // p.process_event( player::stop_event() );
}

max_stars_repo_path: lab28/1 - boost-msm.cpp

max_issues_repo_path: lab28/1 - boost-msm.cpp

max_forks_repo_path: lab28/1 - boost-msm.cpp


text:
// C++ Command line parameters parser.
// http://clp-parser.sourceforge.net/
//
// Copyright (C) Denis Shevchenko, 2010.
// shev.denis @ gmail.com
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// See http://www.opensource.org/licenses/mit-license.php

#ifndef CLPP_DETAIL_NECESSARY_PARAMETERS_HPP
#define CLPP_DETAIL_NECESSARY_PARAMETERS_HPP

#include "common_checker.hpp"
#include "../parameter.hpp"
#include "../parameter_parts_extractor.hpp"

#include <boost/assign.hpp>

/// \namespace clpp
/// \brief Main namespace of library.
namespace clpp {

/// \namespace clpp::detail
/// \brief Details of realization.
namespace detail {

typedef common_checker< parameters, parameter_parts_extractor, std::string > checker;

using namespace boost::assign;

/// \class necessary_parameters_checker
/// \brief Necessary parameters existence checker.
///
/// Checks existence of parameters that registered as necessary.
class necessary_parameters_checker : public checker {
public:
    necessary_parameters_checker( const parameters&                    registered_parameters
                                  , const parameter_parts_extractor&   extractor
                                  , const std::string&                 name_value_separator ) :
            checker( registered_parameters, extractor, name_value_separator ) {}
public:
    void check( const str_storage& inputed_parameters ) const {
        str_storage names_that_should_be = collect_names_of_necessary_parameters();
        remove_names_of_inputed_necessary_parameters( inputed_parameters, names_that_should_be );
        if ( !names_that_should_be.empty() ) {
            notify_about_missing_of_necessary_parameters( names_that_should_be );
        } else {}
    }
private:
    str_storage collect_names_of_necessary_parameters() const {
        str_storage names;
    	BOOST_FOREACH ( const parameter& registered_parameter, registered_parameters ) {
            if ( registered_parameter.is_necessary ) {
            	names += registered_parameter.short_name;
            } else {}
        }
        return names;
    }

    void remove_names_of_inputed_necessary_parameters( const str_storage& inputed_parameters
                                                       , str_storage&     names_that_should_be ) const {
        BOOST_FOREACH ( const std::string& inputed_parameter, inputed_parameters ) {
        	const std::string name = extractor.extract_name_from( inputed_parameter );
            detail::delete_element( names_that_should_be, name );
        }
    }

    void notify_about_missing_of_necessary_parameters( const str_storage& names_that_should_be ) const {
        std::string what_happened = lib_prefix() + "Parameter ";

        BOOST_FOREACH ( const std::string& name, names_that_should_be ) {
        	what_happened += "'" + name + "', ";
        }
        boost::erase_last( what_happened, ", " );

        if ( 1 == names_that_should_be.size() ) {
            what_happened += " is defined as necessary, but it missed!";
        } else {
            boost::replace_first( what_happened, "Parameter", "Parameters" );
            what_happened += " are defined as necessary, but they missed!";
        }

        throw std::runtime_error( what_happened );
    }
};

} // namespace detail
} // namespace clpp

#endif // CLPP_DETAIL_NECESSARY_PARAMETERS_HPP

ext: hpp

max_stars_repo_name: HydrArgs/clpp

max_issues_repo_name: HydrArgs/clpp

max_forks_repo_name: HydrArgs/clpp


text:
#include "viennautils/dfise/data_reader.hpp"

#include <boost/ref.hpp>
#include <boost/bind.hpp>

#include "viennautils/filesystem/filesystem.hpp"
#include "viennautils/dfise/parsing_error.hpp"
#include "viennautils/dfise/primary_reader.hpp"

namespace viennautils
{
namespace dfise
{

namespace
{

void expect(primary_reader & preader, std::string const & attribute_name, std::string const & expected_value)
{
  std::string value;
  preader.read_attribute(attribute_name, value);
  if (value != expected_value)
  {
    throw make_exception<parsing_error>("unexpected value for attribute: " + attribute_name + " expected: " + expected_value + " but got: " + value + " instead");
  }
}

} //end of anonyomous namespace

struct data_reader::Dataset
{
  std::string name_;
  std::string function_;
  std::vector<std::string> validity_;
  unsigned int dimension_;
  ValueVector values_;
};

data_reader::data_reader( grd_bnd_reader const & gbreader
                        )
                        : dimension_(gbreader.get_dimension())
                        , vertex_count_(gbreader.get_vertices().size()/dimension_)
                        , element_count_(gbreader.get_elements().size())
{
  //find and sort all vertices of every region
  //this is actually redundant information, however it will be needed often when reading additional dataset files
  region_vertex_indices_.reserve(gbreader.get_regions().size());
  for (grd_bnd_reader::RegionMap::const_iterator region_it = gbreader.get_regions().begin(); region_it != gbreader.get_regions().end(); ++region_it)
  {
    VertexIndexSet & region_vertices = region_vertex_indices_[region_it->first];
    for ( std::vector<grd_bnd_reader::ElementIndex>::const_iterator element_it = region_it->second.element_indices_.begin()
        ; element_it != region_it->second.element_indices_.end()
        ; ++element_it
        )
    {
      //indices in greader are guaranteed to be valid (not out of bounds)
      grd_bnd_reader::element const & element = gbreader.get_elements()[*element_it];
      for ( std::vector<grd_bnd_reader::VertexIndex>::const_iterator vertex_it = element.vertex_indices_.begin()
          ; vertex_it != element.vertex_indices_.end()
          ; ++vertex_it
          )
      {
        region_vertices.insert(*vertex_it);
      }
    }
  }
}

void data_reader::read(std::string const & filepath)
{
  try
  {
    //start by reading all datasets in the file using the primary_reader
    DatasetList datasets;
    primary_reader preader( filepath
                          , boost::bind(&data_reader::parse_additional_info, this, _1, boost::ref(datasets))
                          , boost::bind(&data_reader::parse_data_block, this, _1, boost::ref(datasets))
                          );

    while (!datasets.empty())
    {
      std::string const & dataset_name = datasets.begin()->name_;
      try
      {
        std::vector<DatasetList::iterator> subset;
        boost::container::flat_set<std::string> total_validities;
        unsigned int dimension = datasets.begin()->dimension_;
        for (DatasetList::iterator dataset_it = datasets.begin(); dataset_it != datasets.end(); ++dataset_it)
        {
          if (dataset_it->name_ == dataset_name)
          {
            if (dataset_it->dimension_ != dimension)
            {
              throw make_exception<parsing_error>("different dimension given at different places");
            }
            subset.push_back(dataset_it);
            for (std::vector<std::string>::const_iterator region_it = dataset_it->validity_.begin(); region_it != dataset_it->validity_.end(); ++region_it)
            {
              if (region_vertex_indices_.find(*region_it) == region_vertex_indices_.end())
              {
                throw make_exception<parsing_error>("invalid validity region: " + *region_it);
              }
              if (total_validities.find(*region_it) != total_validities.end())
              {
                throw make_exception<parsing_error>("region: " + *region_it + " is specified in more than one validity array in a single file for a single dataset");
              }
              total_validities.insert(*region_it);
            }
          }
        }

        std::string unique_name = generate_unique_name(dataset_name, filepath);
        if (total_validities.size() == region_vertex_indices_.size())
        {
          //complete dataset
          complete_datasets_[unique_name].first = dimension;
          ValueVector & values = complete_datasets_[unique_name].second;
          if (subset.size() == 1)
          {
            //optimization for datasets that define all their values in one fell swoop
            values = datasets.begin()->values_;
          }
          else
          {
            values.resize(vertex_count_*dimension);
            for (std::vector<DatasetList::iterator>::iterator it = subset.begin(); it != subset.end(); ++it)
            {
              VertexIndexSet combined_indices;
              combine_region_indices((*it)->validity_, combined_indices);

              if (combined_indices.size()*dimension != (*it)->values_.size())
              {
                throw make_exception<parsing_error>( "invalid number of values, expected: "
                                                   + boost::lexical_cast<std::string>(combined_indices.size()*dimension)
                                                   + ", got: " + boost::lexical_cast<std::string>((*it)->values_.size())
                                                   );
              }

              size_t i = 0;
              for (VertexIndexSet::const_iterator combined_it = combined_indices.begin(); combined_it != combined_indices.end(); ++i, ++combined_it)
              {
                for (size_t j = 0; j < dimension; ++j)
                {
                  values[(*combined_it)*dimension+j] = (*it)->values_[i*dimension+j];
                }
              }
            }
          }
        }
        else
        {
          //partial dataset
          partial_datasets_[unique_name].first = dimension;
          VertexIndexSet total_combined_indices;
          combine_region_indices(std::vector<std::string>(total_validities.begin(), total_validities.end()), total_combined_indices);

          VertexIndexVector & vertex_indices = partial_datasets_[unique_name].second.first;
          vertex_indices.reserve(total_combined_indices.size());
          vertex_indices.insert(vertex_indices.begin(), total_combined_indices.begin(), total_combined_indices.end());
          ValueVector & values = partial_datasets_[unique_name].second.second;

          values.resize(total_combined_indices.size()*dimension);
          for (std::vector<DatasetList::iterator>::iterator it = subset.begin(); it != subset.end(); ++it)
          {
            VertexIndexSet combined_indices;
            combine_region_indices((*it)->validity_, combined_indices);

            if (combined_indices.size()*dimension != (*it)->values_.size())
            {
              throw make_exception<parsing_error>( "invalid number of values, expected: "
                                                  + boost::lexical_cast<std::string>(combined_indices.size()*dimension)
                                                  + ", got: " + boost::lexical_cast<std::string>((*it)->values_.size())
                                                  );
            }

            size_t i = 0;
            for (VertexIndexSet::const_iterator combined_it = combined_indices.begin(); combined_it != combined_indices.end(); ++i, ++combined_it)
            {
              size_t offset = (total_combined_indices.find(*combined_it)-total_combined_indices.begin())*dimension;
              for (size_t j = 0; j < dimension; ++j)
              {
                values[offset+j] = (*it)->values_[i*dimension+j];
              }
            }
          }
        }

        //remove all datasets that we just unified
        for (std::vector<DatasetList::iterator>::iterator it = subset.begin(); it != subset.end(); ++it)
        {
          datasets.erase(*it);
        }
      }
      catch (parsing_error const & e)
      {
        throw make_exception<parsing_error>("while unifying dataset: " + dataset_name + " - " + e.what());
      }
    }
  }
  catch(parsing_error const & e)
  {
    throw make_exception<parsing_error>("while parsing file: " + filepath + " - " + e.what());
  }
}

void data_reader::parse_additional_info(primary_reader & preader, DatasetList & datasets)
{
  if(preader.get_mandatory_info().type_ != primary_reader::filetype_dataset)
  {
    throw make_exception<parsing_error>( "invalid file type: " + boost::lexical_cast<std::string>(preader.get_mandatory_info().type_)
                                       + " - data_reader parses dataset files only"
                                       );
  }

  if (  preader.get_mandatory_info().dimension_ != dimension_
     || preader.get_mandatory_info().nb_vertices_ != vertex_count_
     || preader.get_mandatory_info().nb_elements_ != element_count_
     || preader.get_mandatory_info().nb_regions_ != region_vertex_indices_.size()
     )
  {
    throw make_exception<parsing_error>("basic information (dimension, number of vertices/elements/regions) mismatch");
  }

  std::vector<std::string> names;
  std::vector<std::string> functions;
  preader.read_array("datasets", names);
  preader.read_array("functions", functions);
  if (names.size() != functions.size())
  {
    throw make_exception<parsing_error>("number of datasets and functions in Info block does not match");
  }

  for (size_t i = 0; i < names.size(); ++i)
  {
    Dataset tmp;
    tmp.name_ = names[i];
    tmp.function_ = functions[i];
    datasets.push_back(tmp);
  }
}

void data_reader::parse_data_block(primary_reader & preader, DatasetList & datasets)
{
  for (DatasetList::iterator it = datasets.begin(); it != datasets.end(); ++it)
  {
    preader.read_block<std::string>("Dataset", boost::bind(parse_dataset_block, boost::ref(preader), boost::ref(*it), _1));
  }
}

void data_reader::combine_region_indices(std::vector<std::string> const & validity, VertexIndexSet & combined_indices)
{
  if (validity.size() == 1)
  {
    combined_indices = region_vertex_indices_[validity[0]];
  }
  else
  {
    for (std::vector<std::string>::const_iterator it = validity.begin(); it != validity.end(); ++it)
    {
      combined_indices.insert(region_vertex_indices_[*it].begin(), region_vertex_indices_[*it].end());
    }
  }
}

bool data_reader::is_unique(std::string const & dataset_name) const
{
  return (  (partial_datasets_.find(dataset_name) == partial_datasets_.end())
         && (complete_datasets_.find(dataset_name) == complete_datasets_.end())
         );
}

std::string data_reader::generate_unique_name(std::string const & dataset_name, std::string const & filepath) const
{
  if (is_unique(dataset_name))
  {
    return dataset_name;
  }

  std::string file_stem = viennautils::filesystem::extract_stem(filepath);

  std::string dataset_stem_name = dataset_name + "_" + file_stem;
  if (is_unique(dataset_stem_name))
  {
    return dataset_stem_name;
  }

  for (unsigned int i = 2;; ++i)
  {
    std::string candidate = dataset_stem_name + "_" + boost::lexical_cast<std::string>(i);
    if (is_unique(candidate))
    {
      return candidate;
    }
  }
}

void data_reader::parse_dataset_block(primary_reader & preader, Dataset & dataset, std::string const & para)
{
  if (para != dataset.name_)
  {
    throw make_exception<parsing_error>("unexpected dataset name: " + para + " - expected name: " + dataset.name_);
  }

  try
  {
    expect(preader, "function", dataset.function_);
    std::string type;
    preader.read_attribute("type", type);
    if (type == "scalar")
    {
      expect(preader, "dimension", "1");
      dataset.dimension_ = 1;
    }
    else if (type == "vector")
    {
      preader.read_attribute("dimension", dataset.dimension_);
    }
    else
    {
      throw make_exception<parsing_error>("unexpected value for attribute: type - got value: " + type);
    }
    expect(preader, "location", "vertex");

    preader.read_array("validity", dataset.validity_);
    preader.read_block<std::vector<double>::size_type>("Values", boost::bind(parse_dataset_values_block, boost::ref(preader), boost::ref(dataset.values_), _1));
  }
  catch(parsing_error const & e)
  {
    throw make_exception<parsing_error>("while parsing dataset: " + dataset.name_ + " - " + e.what());
  }
}

void data_reader::parse_dataset_values_block(primary_reader & preader, std::vector<double> & values, std::vector<double>::size_type const & para)
{
  values.resize(para);
  for (std::vector<double>::size_type i = 0; i < values.size(); ++i)
  {
    preader.read_value(values[i]);
  }
}

} //end of namespace dfise

} //end of namespace viennautils

max_stars_repo_name: viennautils/viennautils-dev

max_issues_repo_name: viennautils/viennautils-dev

max_forks_repo_name: viennautils/viennautils-dev


text:
#ifndef PENNFAC_HPP
#define PENNFAC_HPP

#include <vector>
#include <string>
#include <boost/serialization/list.hpp>
#include <boost/serialization/set.hpp>
#include <boost/serialization/vector.hpp>

#include "arch/AACAgent.hpp"
#include "bib/Seed.hpp"
#include "bib/Utils.hpp"
#include "bib/OrnsteinUhlenbeckNoise.hpp"
#include "bib/MetropolisHasting.hpp"
#include "bib/XMLEngine.hpp"
#include "bib/IniParser.hpp"
#include "nn/MLP.hpp"

#ifndef SAASRG_SAMPLE
#define SAASRG_SAMPLE
typedef struct _sample {
  std::vector<double> s;
  std::vector<double> pure_a;
  std::vector<double> a;
  std::vector<double> next_s;
  double r;
  bool goal_reached;
  double prob;

} sample;
#endif

template<typename NN = MLP>
class OfflineCaclaAg : public arch::AACAgent<NN, arch::AgentProgOptions> {
 public:
  typedef NN PolicyImpl;
  friend class FusionOOAg;

  OfflineCaclaAg(unsigned int _nb_motors, unsigned int _nb_sensors)
    : arch::AACAgent<NN, arch::AgentProgOptions>(_nb_motors, _nb_sensors), nb_sensors(_nb_sensors), empty_action(0) {

  }

  virtual ~OfflineCaclaAg() {
    delete vnn;
    delete ann;

    delete ann_testing;
    if(batch_norm_critic != 0)
      delete vnn_testing;

    delete hidden_unit_v;
    delete hidden_unit_a;

    if(oun == nullptr)
      delete oun;
  }

  const std::vector<double>& _run(double reward, const std::vector<double>& sensors,
                                  bool learning, bool goal_reached, bool) override {

    // protect batch norm from testing data and poor data
    vector<double>* next_action = ann_testing->computeOut(sensors);
    if (last_action.get() != nullptr && learning){
      double prob = bib::Proba<double>::truncatedGaussianDensity(*last_action, *last_pure_action, noise);
      trajectory_offpol.push_back( {last_state, *last_pure_action, *last_action, sensors, reward, goal_reached, prob});
      trajectory_onpol.push_back( {last_state, *last_pure_action, *last_action, sensors, reward, goal_reached, prob});
    }

    last_pure_action.reset(new vector<double>(*next_action));
    if(learning) {
      if(gaussian_policy == 1) {
        vector<double>* randomized_action = bib::Proba<double>::multidimentionnalTruncatedGaussian(*next_action, noise);
        delete next_action;
        next_action = randomized_action;
      } else if(gaussian_policy == 2) {
        oun->step(*next_action);
      } else if(gaussian_policy == 3 && bib::Utils::rand01() < noise2) {
        vector<double>* randomized_action = bib::Proba<double>::multidimentionnalTruncatedGaussian(*next_action, noise);
        delete next_action;
        next_action = randomized_action;
      } else if(gaussian_policy == 4) {
        vector<double>* randomized_action = bib::Proba<double>::multidimentionnalTruncatedGaussian(*next_action, noise * pow(noise2, noise3 - ((double) step)));
        delete next_action;
        next_action = randomized_action;
      } else if(bib::Utils::rand01() < noise) { //e-greedy
        for (uint i = 0; i < next_action->size(); i++)
          next_action->at(i) = bib::Utils::randin(-1.f, 1.f);
      }
    }
    last_action.reset(next_action);

    last_state.clear();
    for (uint i = 0; i < sensors.size(); i++)
      last_state.push_back(sensors[i]);

    step++;

    return *next_action;
  }

  void _unique_invoke(boost::property_tree::ptree* pt, boost::program_options::variables_map*) override {
//     bib::Seed::setFixedSeedUTest();
    hidden_unit_v               = bib::to_array<uint>(pt->get<std::string>("agent.hidden_unit_v"));
    hidden_unit_a               = bib::to_array<uint>(pt->get<std::string>("agent.hidden_unit_a"));
    noise                       = pt->get<double>("agent.noise");
    gaussian_policy             = pt->get<uint>("agent.gaussian_policy");
    number_fitted_iteration     = pt->get<uint>("agent.number_fitted_iteration");
    stoch_iter_actor            = pt->get<uint>("agent.stoch_iter_actor");
    stoch_iter_critic           = pt->get<uint>("agent.stoch_iter_critic");
    batch_norm_actor            = pt->get<uint>("agent.batch_norm_actor");
    batch_norm_critic           = pt->get<uint>("agent.batch_norm_critic");
    actor_output_layer_type     = pt->get<uint>("agent.actor_output_layer_type");
    hidden_layer_type           = pt->get<uint>("agent.hidden_layer_type");
    alpha_a                     = pt->get<double>("agent.alpha_a");
    alpha_v                     = pt->get<double>("agent.alpha_v");
    lambda                      = pt->get<double>("agent.lambda");
    momentum                    = pt->get<uint>("agent.momentum");
    beta_target                 = pt->get<double>("agent.beta_target");
    conserve_beta               = pt->get<bool>("agent.conserve_beta");
    disable_trust_region        = pt->get<bool>("agent.disable_trust_region");
    disable_cac                 = pt->get<bool>("agent.disable_cac");
    gae                         = false;
    update_each_episode         = pt->get<uint>("agent.update_each_episode");
    nb_offpolicy_trajectories   = pt->get<uint>("agent.nb_offpolicy_trajectories");
    offpol_actor                = pt->get<bool>("agent.offpol_actor");

    if(gaussian_policy == 2){
      double oun_theta = pt->get<double>("agent.noise2");
      double oun_dt = pt->get<double>("agent.noise3");
      oun = new bib::OrnsteinUhlenbeckNoise<double>(this->nb_motors, noise, oun_theta, oun_dt);
    } else if (gaussian_policy == 3){
      noise2 = pt->get<double>("agent.noise2");
    } else if (gaussian_policy == 4){
      noise2 = pt->get<double>("agent.noise2");
      noise3 = pt->get<double>("agent.noise3");
    }

    if(lambda >= 0.)
      gae = pt->get<bool>("agent.gae");

    if(lambda >=0. && batch_norm_critic != 0){
      LOG_DEBUG("to be done!");
      exit(1);
    }

    ann = new NN(nb_sensors, *hidden_unit_a, this->nb_motors, alpha_a, 1, hidden_layer_type, actor_output_layer_type, batch_norm_actor, true, momentum);

    vnn = new NN(nb_sensors, nb_sensors, *hidden_unit_v, alpha_v, 1, -1, hidden_layer_type, batch_norm_critic, false, momentum);

    ann_testing = new NN(*ann, false, ::caffe::Phase::TEST);
    if(batch_norm_critic != 0)
      vnn_testing = new NN(*vnn, false, ::caffe::Phase::TEST);

    bestever_score = std::numeric_limits<double>::lowest();
  }

  void _start_episode(const std::vector<double>& sensors, bool) override {
    last_state.clear();
    for (uint i = 0; i < sensors.size(); i++)
      last_state.push_back(sensors[i]);

    last_action = nullptr;
    last_pure_action = nullptr;

    step = 0;
    if(gaussian_policy == 2)
      oun->reset();

    double* weights = new double[ann->number_of_parameters(false)];
    ann->copyWeightsTo(weights, false);
    ann_testing->copyWeightsFrom(weights, false);
    delete[] weights;
  }

  void update_critic() {

    if (trajectory_offpol.size() > 0) {
      std::vector<double> all_states(trajectory_offpol.size() * nb_sensors);
      std::vector<double> all_next_states(trajectory_offpol.size() * nb_sensors);

      std::vector<double> all_is(trajectory_offpol.size());
      int li=0;
      for (auto it : trajectory_offpol) {
          std::copy(it.s.begin(), it.s.end(), all_states.begin() + li * nb_sensors);
          std::copy(it.next_s.begin(), it.next_s.end(), all_next_states.begin() + li * nb_sensors);
          li++;
      }
      li=0;
      std::vector<double>* current_pol;
      if(batch_norm_actor != 0 ) {
        ann_testing->increase_batchsize(trajectory_offpol.size());
        current_pol = ann_testing->computeOutBatch(all_states);
      } else {
        ann->increase_batchsize(trajectory_offpol.size());
        current_pol = ann->computeOutBatch(all_states);
      }
      for (auto it : trajectory_offpol) {
        all_is[li] = bib::Proba<double>::truncatedGaussianDensity(it.a, current_pol->data(), noise, li * this->nb_motors) / it.prob;
        li++;
      }
//       bib::Logger::PRINT_ELEMENTS(all_is);

      //remove trace of old policy
      auto iter = [&]() {
        std::vector<double> v_target(trajectory_offpol.size());
        decltype(vnn_testing->computeOutVFBatch(all_next_states, empty_action)) all_nextV;
        if(batch_norm_critic != 0) {
          double* weights = new double[vnn->number_of_parameters(false)];
          vnn->copyWeightsTo(weights, false);
          vnn_testing->copyWeightsFrom(weights, false);
          delete[] weights;
          all_nextV = vnn_testing->computeOutVFBatch(all_next_states, empty_action);
        } else
          all_nextV = vnn->computeOutVFBatch(all_next_states, empty_action);

        int li=0;
        for (auto it : trajectory_offpol) {
          double target = it.r;
          if (!it.goal_reached) {
            double nextV = all_nextV->at(li);
            target += this->gamma * nextV;
          }

          v_target[li] = target;
          li++;
        }

        ASSERT((uint)li == trajectory_offpol.size(), "");
        if(lambda < 0.f && batch_norm_critic == 0)
          vnn->learn_batch(all_states, empty_action, v_target, stoch_iter_critic);
        else if(lambda < 0.f) {
          for(uint sia = 0; sia < stoch_iter_critic; sia++){
            delete vnn->computeOutVFBatch(all_states, empty_action);
            {
              double* weights = new double[vnn->number_of_parameters(false)];
              vnn->copyWeightsTo(weights, false);
              vnn_testing->copyWeightsFrom(weights, false);
              delete[] weights;
            }
            auto all_V = vnn_testing->computeOutVFBatch(all_states, empty_action);

            const auto q_values_blob = vnn->getNN()->blob_by_name(MLP::q_values_blob_name);
            double* q_values_diff = q_values_blob->mutable_cpu_diff();
            uint i=0;
            double s = trajectory_offpol.size();
            for (auto it : trajectory_offpol){
              q_values_diff[i] = (all_V->at(i)-v_target[i])/s;
              i++;
            }
            vnn->critic_backward();
            vnn->updateFisher(trajectory_offpol.size());
            vnn->regularize();
            vnn->getSolver()->ApplyUpdate();
            vnn->getSolver()->set_iter(vnn->getSolver()->iter() + 1);
            delete all_V;
          }
        }
        else {
          auto all_V = vnn->computeOutVFBatch(all_states, empty_action);
          std::vector<double> deltas(trajectory_offpol.size());
//
//        Simple computation for lambda return
//
//          bib::Logger::PRINT_ELEMENTS(*all_V, "V0 ");
          li=0;
          for (auto it : trajectory_offpol) {
            deltas[li] = v_target[li] - all_V->at(li);
            deltas[li] *= std::min(pbar,  all_is[li]);
            ++li;
          }
//          bib::Logger::PRINT_ELEMENTS(deltas, "deltas ");

          std::vector<double> diff(trajectory_offpol.size());
          li=trajectory_offpol.size() - 1;
          double prev_delta = 0.;
          int index_ep = trajectory_end_points_offpol.size() - 1;
          for (auto it : trajectory_offpol) {
            if (index_ep >= 0 && trajectory_end_points_offpol[index_ep] - 1 == li){
                prev_delta = 0.;
                index_ep--;
            }
            diff[li] = deltas[li] + prev_delta * std::min(cbar, all_is[li]);
            prev_delta = this->gamma * lambda * diff[li];
            --li;
          }
          ASSERT(diff[trajectory_offpol.size() -1] == deltas[trajectory_offpol.size() -1], "pb lambda");

// //           comment following lines to compare with the other formula
          li=0;
          for (auto it : trajectory_offpol) {
            diff[li] = diff[li] + all_V->at(li);
            ++li;
          }

//          bib::Logger::PRINT_ELEMENTS(diff, "target ");
          vnn->learn_batch(all_states, empty_action, diff, stoch_iter_critic);

          delete all_V;
        }

        delete all_nextV;
      };

      for(uint i=0; i<number_fitted_iteration; i++)
        iter();

      delete current_pol;
    }
  }

  void end_episode(bool learning) override {
//     LOG_FILE("policy_exploration", ann->hash());
    if(!learning) {
      return;
    }

    //learning phase
    trajectory_end_points_offpol.push_back(trajectory_offpol.size());
    trajectory_end_points_onpol.push_back(trajectory_onpol.size());
    if (episode % update_each_episode != 0)
      return;

    //adapt off-pol buffer
    while(trajectory_end_points_offpol.size() > nb_offpolicy_trajectories) {
      int first_traj_size = trajectory_end_points_offpol[0];
      trajectory_end_points_offpol.pop_front();
      for (int i=0;i<first_traj_size;i++)
        trajectory_offpol.pop_front();
      for (uint i=0;i< trajectory_end_points_offpol.size(); i++)
        trajectory_end_points_offpol[i] -= first_traj_size;
    }

    if(trajectory_offpol.size() > 0){
      vnn->increase_batchsize(trajectory_offpol.size());
      if(batch_norm_critic != 0)
        vnn_testing->increase_batchsize(trajectory_offpol.size());
    }

    update_critic();

    std::deque<sample>* traj = &trajectory_onpol;
    std::deque<int>* traj_end_points = &trajectory_end_points_onpol;
    if (offpol_actor) {
      traj = &trajectory_offpol;
      traj_end_points = &trajectory_end_points_offpol;
    }

    if (traj->size() > 0) {
      const std::vector<bool> disable_back_ac(this->nb_motors, true);
      std::vector<double> deltas(traj->size());

      std::vector<double> all_states(traj->size() * nb_sensors);
      std::vector<double> all_next_states(traj->size() * nb_sensors);
      std::vector<double> all_is(traj->size(), 1.f);
      int li=0;
      for (auto it : *traj) {
        std::copy(it.s.begin(), it.s.end(), all_states.begin() + li * nb_sensors);
        std::copy(it.next_s.begin(), it.next_s.end(), all_next_states.begin() + li * nb_sensors);
        li++;
      }

      decltype(vnn->computeOutVFBatch(all_next_states, empty_action)) all_nextV, all_mine;
      if(batch_norm_critic != 0)
      {
        double* weights = new double[vnn->number_of_parameters(false)];
        vnn->copyWeightsTo(weights, false);
        vnn_testing->copyWeightsFrom(weights, false);
        delete[] weights;
        vnn->increase_batchsize(traj->size());
        all_nextV = vnn_testing->computeOutVFBatch(all_next_states, empty_action);
        all_mine = vnn_testing->computeOutVFBatch(all_states, empty_action);
      } else {
        vnn->increase_batchsize(traj->size());
        all_nextV = vnn->computeOutVFBatch(all_next_states, empty_action);
        all_mine = vnn->computeOutVFBatch(all_states, empty_action);
      }

      std::vector<double>* current_pol;
      if (offpol_actor) {
        if(batch_norm_actor != 0 ) {
          ann_testing->increase_batchsize(trajectory_offpol.size());
          current_pol = ann_testing->computeOutBatch(all_states);
        } else {
          ann->increase_batchsize(trajectory_offpol.size());
          current_pol = ann->computeOutBatch(all_states);
        }
      }

      li=0;
      for (auto it : *traj) {
        sample sm = it;
        double v_target = sm.r;
        if (!sm.goal_reached) {
          double nextV = all_nextV->at(li);
          v_target += this->gamma * nextV;
        }

        deltas[li] = v_target - all_mine->at(li);
        ++li;
      }

      li=0;
      if(offpol_actor) {
        for (auto it : *traj) {
            all_is[li] = bib::Proba<double>::truncatedGaussianDensity(it.a, current_pol->data(), noise, li * this->nb_motors) / it.prob;
            deltas[li] *= std::min(pbar,  all_is[li]);
            ++li;
        }
        delete current_pol;
      }

      if(gae){
        //
        //        Simple computation for lambda return
        //
        std::vector<double> diff(traj->size());
        li=traj->size() - 1;
        double prev_delta = 0.;
        int index_ep = traj_end_points->size() - 1;
        for (auto it : *traj) {
          if (index_ep >= 0 && traj_end_points->at(index_ep) - 1 == li){
              prev_delta = 0.;
              index_ep--;
          }
          diff[li] = deltas[li] + prev_delta * std::min(cbar, all_is[li]);
          prev_delta = this->gamma * lambda * diff[li];
          --li;
        }
        ASSERT(diff[traj->size() -1] == deltas[traj->size() -1], "pb lambda");

        li=0;
        for (auto it : *traj){
//           diff[li] = diff[li] + all_V->at(li);
          deltas[li] = diff[li];
          ++li;
        }
      }

      uint n=0;
      posdelta_mean=0.f;
      std::vector<double> sensors((traj->size() + trajectory_onpol.size()) * nb_sensors);
      std::vector<double> actions((traj->size() + trajectory_onpol.size()) * this->nb_motors);
      std::vector<bool> disable_back(traj->size() * this->nb_motors, false);
      std::vector<double> deltas_blob(traj->size() * this->nb_motors, 1.f);

      li=0;
      //cacla cost
      for(auto it = traj->begin(); it != traj->end() ; ++it) {
        sample sm = *it;

        std::copy(it->s.begin(), it->s.end(), sensors.begin() + li * nb_sensors);
        std::copy(it->a.begin(), it->a.end(), actions.begin() + li * this->nb_motors);
        if(deltas[li] > 0.) {
          posdelta_mean += deltas[li];
          n++;
        } else {
          std::copy(disable_back_ac.begin(), disable_back_ac.end(), disable_back.begin() + li * this->nb_motors);
        }
        if(!disable_cac)
            std::fill(deltas_blob.begin() + li * this->nb_motors, deltas_blob.begin() + (li+1) * this->nb_motors, deltas[li]);
        li++;
      }
      //penalty cost
      for(auto it = trajectory_onpol.begin(); it != trajectory_onpol.end() ; ++it) {
        sample sm = *it;

        std::copy(it->s.begin(), it->s.end(), sensors.begin() + li * nb_sensors);
        std::copy(it->pure_a.begin(), it->pure_a.end(), actions.begin() + li * this->nb_motors);
        li++;
      }

      ratio_valid_advantage = ((float)n) / ((float) traj->size());
      posdelta_mean = posdelta_mean / ((float) traj->size());
      int size_cost_cacla=traj->size()*this->nb_motors;

      double beta=0.0001f;
      mean_beta=0.f;
      if(conserve_beta)
        beta=conserved_beta;
      mean_beta += beta;

      if(n > 0) {
        for(uint sia = 0; sia < stoch_iter_actor; sia++){
          ann->increase_batchsize(traj->size() + trajectory_onpol.size());
          //learn BN
          auto ac_out = ann->computeOutBatch(sensors);
          if(batch_norm_actor != 0) {
            //re-compute ac_out with BN as testing
            double* weights = new double[ann->number_of_parameters(false)];
            ann->copyWeightsTo(weights, false);
            ann_testing->copyWeightsFrom(weights, false);
            delete[] weights;
            delete ac_out;
            ann_testing->increase_batchsize(traj->size() + trajectory_onpol.size());
            ac_out = ann_testing->computeOutBatch(sensors);
          }
          ann->ZeroGradParameters();

          number_effective_actor_update = sia;
          if(disable_trust_region)
              beta=0.f;
          else if (sia > 0) {
            //compute deter distance(pi, pi_old)
            double l2distance = 0.;
            for(uint i=size_cost_cacla;i<actions.size();i++) {
                double x = actions[i] - ac_out->at(i);
                l2distance += x*x;
            }
            l2distance = std::sqrt(l2distance/((double) trajectory_onpol.size()*this->nb_motors));

            if (l2distance < beta_target/1.5)
                beta = beta/2.;
            else if (l2distance > beta_target*1.5)
                beta = beta*2.;

            beta=std::max(std::min((double)20.f, beta), (double) 0.01f);
            mean_beta += beta;
            conserved_l2dist = l2distance;
            //LOG_DEBUG(std::setprecision(7) << l2distance << " " << beta << " " << beta_target << " " << sia);
          }

          const auto actor_actions_blob = ann->getNN()->blob_by_name(MLP::actions_blob_name);
          auto ac_diff = actor_actions_blob->mutable_cpu_diff();

          for(int i=0; i<actor_actions_blob->count(); i++) {
            double x = actions[i] - ac_out->at(i);
            if(i < size_cost_cacla) {
                if(disable_back[i])
                    ac_diff[i] = 0.00000000f;
                else
                    ac_diff[i] = -x * deltas_blob[i];
            } else
                ac_diff[i] = -x * beta;
          }
          ann->actor_backward();
          ann->updateFisher(n);
          ann->regularize();
          ann->getSolver()->ApplyUpdate();
          ann->getSolver()->set_iter(ann->getSolver()->iter() + 1);
          delete ac_out;
        }
      } else if(batch_norm_actor != 0){
        ann->increase_batchsize(trajectory_onpol.size());
        delete ann->computeOutBatch(sensors);
      }

      conserved_beta = beta;
      mean_beta /= (double) number_effective_actor_update;

      delete all_nextV;
      delete all_mine;

      if(batch_norm_actor != 0)
        ann_testing->increase_batchsize(1);
    }

    nb_sample_update = traj->size();
    trajectory_onpol.clear();
    trajectory_end_points_onpol.clear();
  }

  void end_instance(bool learning) override {
    if(learning)
      episode++;
  }

  void save(const std::string& path, bool savebest, bool learning) override {
    if(savebest) {
      if(!learning && this->sum_weighted_reward >= bestever_score) {
        bestever_score = this->sum_weighted_reward;
        ann->save(path+".actor");
      }
    } else {
      ann->save(path+".actor");
      vnn->save(path+".critic");
    }
  }

  void save_run() override {
    ann->save("continue.actor");
    vnn->save("continue.critic");
    struct algo_state st = {episode};
    bib::XMLEngine::save(st, "algo_state", "continue.algo_state.data");
  }

  void load(const std::string& path) override {
    ann->load(path+".actor");
    vnn->load(path+".critic");
  }

  void load_previous_run() override {
    ann->load("continue.actor");
    vnn->load("continue.critic");
    auto p3 = bib::XMLEngine::load<struct algo_state>("algo_state", "continue.algo_state.data");
    episode = p3->episode;
    delete p3;
  }

  double criticEval(const std::vector<double>&, const std::vector<double>&) override {
    LOG_INFO("not implemented");
    return 0;
  }

  arch::Policy<NN>* getCopyCurrentPolicy() override {
//         return new arch::Policy<MLP>(new MLP(*ann) , gaussian_policy ? arch::policy_type::GAUSSIAN : arch::policy_type::GREEDY, noise, decision_each);
    return nullptr;
  }

 protected:
  void _display(std::ostream& out) const override {
    out << std::setw(12) << std::fixed << std::setprecision(10) << this->sum_weighted_reward/this->gamma << " " << this->sum_reward <<
        " " << std::setw(8) << std::fixed << std::setprecision(5) << vnn->error() << " " << noise << " " << nb_sample_update <<
          " " << std::setprecision(3) << ratio_valid_advantage << " " << vnn->weight_l1_norm() << " " << ann->weight_l1_norm(true);
  }

//clear all; close all; wndw = 10; X=load('0.learning.data'); X=filter(ones(wndw,1)/wndw, 1, X); startx=0; starty=800; width=350; height=350; figure('position',[startx,starty,width,height]); plot(X(:,3), "linewidth", 2); xlabel('learning episode', "fontsize", 16); ylabel('sum rewards', "fontsize", 16); startx+=width; figure('position',[startx,starty,width,height]); plot(X(:,9), "linewidth", 2); xlabel('learning episode', "fontsize", 16); ylabel('beta', "fontsize", 16); startx+=width; figure('position',[startx,starty,width,height]) ; plot(X(:,8), "linewidth", 2); xlabel('learning episode', "fontsize", 16); ylabel('valid adv', "fontsize", 16); ylim([0, 1]); startx+=width; figure('position',[startx,starty,width,height]) ; plot(X(:,11), "linewidth", 2); hold on; plot(X(:,12), "linewidth", 2, "color", "red"); legend("critic", "actor"); xlabel('learning episode', "fontsize", 16); ylabel('||\theta||_1', "fontsize", 16); startx+=width; figure('position',[startx,starty,width,height]) ; plot(X(:,10), "linewidth", 2); xlabel('learning episode', "fontsize", 16); ylabel('||\mu_{old}-\mu||_2', "fontsize", 16); startx+=width; figure('position',[startx,starty,width,height]) ; plot(X(:,14), "linewidth", 2); xlabel('learning episode', "fontsize", 16); ylabel('effective actor. upd.', "fontsize", 16);
  void _dump(std::ostream& out) const override {
    out << std::setw(25) << std::fixed << std::setprecision(22) << this->sum_weighted_reward/this->gamma << " " <<
    this->sum_reward << " " << std::setw(8) << std::fixed << std::setprecision(5) << vnn->error() << " " <<
    nb_sample_update << " " << std::setprecision(3) << ratio_valid_advantage << " " << std::setprecision(10) <<
    mean_beta << " " << conserved_l2dist << " " << std::setprecision(3) << vnn->weight_l1_norm() << " " <<
    ann->weight_l1_norm(true) << " " << std::setprecision(6)  << posdelta_mean << " " << number_effective_actor_update;
  }

 private:
  uint nb_sensors;
  uint episode = 1;
  uint step = 0;

  double noise, noise2, noise3;
  uint gaussian_policy;
  bool update_critic_first, gae, conserve_beta, disable_trust_region, disable_cac;
  uint number_fitted_iteration, stoch_iter_actor, stoch_iter_critic, offpol_actor;
  uint batch_norm_actor, batch_norm_critic, actor_output_layer_type, hidden_layer_type, momentum;
  double lambda, beta_target;
  double conserved_beta= 0.0001f;
  double mean_beta= 0.f;
  double conserved_l2dist= 0.f;
  int number_effective_actor_update = 0;

  std::shared_ptr<std::vector<double>> last_action;
  std::shared_ptr<std::vector<double>> last_pure_action;
  std::vector<double> last_state;
  double alpha_v, alpha_a;

  std::deque<sample> trajectory_offpol;
  std::deque<int> trajectory_end_points_offpol;

  std::deque<sample> trajectory_onpol;
  std::deque<int> trajectory_end_points_onpol;

  NN* ann;
  NN* vnn;
  NN* ann_testing;
  NN* vnn_testing;

  std::vector<uint>* hidden_unit_v;
  std::vector<uint>* hidden_unit_a;
  std::vector<double> empty_action; //dummy action cause c++ cannot accept null reference
  double bestever_score;
  int update_each_episode;
  bib::OrnsteinUhlenbeckNoise<double>* oun = nullptr;
  float ratio_valid_advantage=0;
  int nb_sample_update = 0;
  double posdelta_mean = 0;
  double pbar = 1;
  double cbar = 1;
  uint nb_offpolicy_trajectories;

  struct algo_state {
    uint episode;

    friend class boost::serialization::access;
    template <typename Archive>
    void serialize(Archive& ar, const unsigned int) {
      ar& BOOST_SERIALIZATION_NVP(episode);
    }
  };
};

#endif


text:
// copyright reserved by Zhihua Ban
/*!
* \file tools.hpp
* \date 2015/05/10 13:54
*
* \author Ban Zhihua
* Contact: sawpara@126.com
*
* \version 0.21
*
* \brief tools to export labeled result
*
* TODO: long description
*
* 0.21
*  : rename the class names
* \note
*/

#ifndef __TOOLS_HPP__
#define __TOOLS_HPP__

#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <inttypes.h>
using namespace std;

#include <opencv2/opencv.hpp>
using namespace cv;

//#include <Eigen/Eigen>

//////////////////////////////////////////////////////////////////////////
// better the label
class Better_Tool{
public:
  Better_Tool(){}
  ~Better_Tool(){}
  // return number of superpixels
  // labels counted from new_start
  template<class T>
  int relabel(T *labels, const size_t len, const T new_start){

    // find min and max
    std::pair<T*, T*> lab_mm = std::minmax_element(labels, labels + len);
    T lab_min = *(lab_mm.first);
    T lab_max = *(lab_mm.second);

    // construct map
    int *p_map = NULL;
    if (lab_max < 0){
      lab_max = 0;
    }

    if (lab_min < 0){
      m_map.resize(lab_max + 1 - lab_min);
      p_map = &(m_map[-lab_min]);
    }
    else{
      m_map.resize(lab_max + 1);
      p_map = &(m_map[0]);
    }
    m_map.assign(m_map.size(), -1);

    // relabel
    int new_lab = new_start;
    for (size_t i = 0; i < len; i++){
      if (p_map[labels[i]] < 0){
        p_map[labels[i]] = new_lab;
        new_lab++;
      }
      labels[i] = p_map[labels[i]];
    }
    return new_lab - new_start;
  }
private:
  vector<int32_t> m_map;
};

class Better{
public:
  // return number of superpixels
  // labels counted from new_start
  template<class T>
  static int relabel(T *labels, const size_t len, const T new_start){
    Better_Tool bt;
    return bt.relabel(labels, len, new_start);
  }
};

//class Map{
//public:
//	template<class T>
//	static void maping(T *labels, const size_t len, const Eigen::ArrayXi &mi){
//		for (size_t i = 0; i < len; i++){
//			labels[i] = mi(labels[i]);
//		}
//	}
//};

//////////////////////////////////////////////////////////////////////////
// export drawing out

class Draw_Tool{
public:
  // type = 0 : major & minor; 1 : only major; 2 : minor
  // ND   = 4 : 4-connected neighbor; 8 : 8-connected neighbor.
  template<int ND = 4, class TL>
  Mat contour(const TL* labels, const Mat &img, const int type = 1, Vec3b major = Vec3b(0, 0, 250), Vec3b minor = Vec3b(0, 100, 180)){
    int width = img.cols;
    int height = img.rows;
    Mat new_image = img.clone();
    _detect_boundary<ND>(labels, width, height);
    for (int y = 0; y < height; y++){
      for (int x = 0; x < width; x++){
        if (m_al_bound[y*width + x] == ms_major_marker && 2 != type){
          new_image.at<Vec3b>(y, x) = major;
        }
        else if (m_al_bound[y*width + x] == ms_minor_marker && 1 != type){
          new_image.at<Vec3b>(y, x) = minor;
        }
      }
    }
    return new_image;
  }

  template<class TL>
  Mat rdcolor(const TL *labels, const int &width, const int &height)
  {
    cv::Mat new_image(height, width, CV_8UC3);
    vector<TL> new_labels;
    new_labels.resize(width*height);
    for (size_t i = 0; i < new_labels.size(); i++){
      new_labels[i] = labels[i];
    }
    int num_sps = m_bt.relabel(&(new_labels[0]), new_labels.size(), 0);

    vector<vector<uchar> > colors(3, vector<uchar>(num_sps));
    for (int k = 0; k < num_sps; ++k) {
      colors[0][k] = rand() & 255;
      colors[1][k] = rand() & 255;
      colors[2][k] = rand() & 255;
    }

    for (int y = 0; y < height; y++){
      for (int x = 0; x < width; x++){
        new_image.at<Vec3b>(y, x) = Vec3b(
          colors[0][new_labels[y*width + x]],
          colors[1][new_labels[y*width + x]],
          colors[2][new_labels[y*width + x]]);
      }
    }

    return new_image;
  }

  template<class TL>
  Mat meanval(const TL *labels, const Mat &img){
    int height = img.rows;
    int width = img.cols;
    vector<TL> new_labels(height*width);
    memcpy(&(new_labels[0]), labels, sizeof(TL)*height*width);

    cv::Mat new_image = img.clone();

    int num_sp = m_bt.relabel(&(new_labels[0]), new_labels.size(), 0);

    vector<int> count_nums(num_sp);
    count_nums.assign(num_sp, 0);

    int num_samples = height*width;

    for (int i = 0; i < num_samples; i++){
      count_nums[new_labels[i]]++;
    }

    vector<Vec3i> mean_sample(num_sp);
    mean_sample.assign(num_sp, Vec3i(0, 0, 0));

    for (int y = 0; y < height; y++){
      for (int x = 0; x < width; x++){
        Vec3b pix = img.at<Vec3b>(y, x);
        mean_sample[new_labels[y*width + x]] += pix;
      }
    }

    for (int i = 0; i < num_sp; i++){
      mean_sample[i] = mean_sample[i] / count_nums[i];
    }

    for (int y = 0; y < height; y++){
      for (int x = 0; x < width; x++){
        new_image.at<Vec3b>(y, x) = mean_sample[new_labels[y*width + x]];
      }
    }

    return new_image;
  }

protected:
  template<int ND = 4, class TL>
  void _detect_boundary(const TL *labels, const int &width, const int &height){
    m_al_bound.resize(width*height);
    m_al_bound.assign(width*height, 0); // start with no boundary
    const int dx[] = { -1, 0, 1, 1, -1, 0, 1, -1 };
    const int dy[] = { -1, -1, -1, 0, 1, 1, 1, 0 };
    const int nd = 8;
    const int bg = (ND == 4 ? 1 : 0);
    const int st = (ND == 4 ? 2 : 1);
    // search for 1-st boundary
    for (int y = 0; y < height; y++){
      for (int x = 0; x < width; x++){
        int cur_label = labels[y*width + x];
        for (int dd = bg; dd < nd; dd += st){
          int xx = x + dx[dd];
          int yy = y + dy[dd];
          if (xx < 0 || yy < 0 || xx >= width || yy >= height) continue;
          if (cur_label != labels[xx + yy*width]){
            m_al_bound[y*width + x] = ms_major_marker;
            break;
          }
        }
      }
    }
    // search for 2-nd boundary
    for (int y = 0; y < height; y++){
      for (int x = 0; x < width; x++){
        if (m_al_bound[y*width + x] == ms_major_marker){
          for (int dd = bg; dd < nd; dd += st){
            int xx = x + dx[dd];
            int yy = y + dy[dd];
            if (xx < 0 || xx >= width || yy < 0 || yy >= height) continue;
            if (m_al_bound[xx + yy*width] == 0){
              m_al_bound[xx + yy*width] = ms_minor_marker;
            }
          }
        }
      }
    }
  }
private:
  // boundary of output of algorithm
  vector<uint8_t> m_al_bound;
  static const uint8_t ms_major_marker = 255;
  static const uint8_t ms_minor_marker = 180;
  Better_Tool m_bt;
};

class Draw{
public:
  // type = 0 : main_mark & sub_mark; 1 : main_mark; 2 : sub_mark
  // ND = 4, 8
  template<int ND = 4, class TL>
  static Mat contour(const TL *labels, const Mat &img, const int type = 0, Vec3b major = Vec3b(0, 0, 250), Vec3b minor = Vec3b(0, 100, 180)){
    Draw_Tool dt;
    return dt.contour<ND>(labels, img, type, major, minor);
  }

  template<class TL>
  static Mat rdcolor(const TL *labels, const int &width, const int &height){
    Draw_Tool dt;
    return dt.rdcolor(labels, width, height);
  }

  template<class TL>
  static Mat meanval(const TL *labels, const Mat &img){
    Draw_Tool dt;
    return dt.meanval(labels, img);
  }
};

#endif

max_stars_repo_name: ahban/VideoSuperpixels

max_issues_repo_name: ahban/VideoSuperpixels

max_forks_repo_name: ahban/VideoSuperpixels


text:
// Copyright 2012 Carnegie Mellon University
// Author: Mark Desnoyer (mdesnoyer@gmail.com)
//
// VisualUtilityROSParams.h
//
// Utilities to build the objects of the visual utility pipeline from
// ROS parameters.

#include "visual_utility/VisualUtilityROSParams.h"

#include <string>
#include <boost/tokenizer.hpp>
#include <boost/lexical_cast.hpp>
#include <vector>

using namespace std;
using namespace cv;

namespace visual_utility {

// Function that parses a comma separated string into a vector of some
// datatype
template <typename T>
vector<T> ParseDataList(const string& str) {
  vector<T> retval;

  typedef boost::tokenizer<boost::char_separator<char> > Tokenizer;
  Tokenizer tokenizer(str, boost::char_separator<char>(", \t\n"));

  for (Tokenizer::iterator i = tokenizer.begin(); i != tokenizer.end(); ++i) {
    try {
      retval.push_back(boost::lexical_cast<T>(*i));
    } catch (boost::bad_lexical_cast& e) {
      ROS_ERROR_STREAM("There was an issue parsing the list: "
                       << str);
    }
  }
  return retval;
}

TransformEstimator* CreateTransformEstimator(ros::NodeHandle handle) {
  int maxIterations;
  double minPrecision;
  double imageScaling;
  handle.param<int>("affine_max_iterations", maxIterations, 100);
  handle.param<double>("min_affine_precision", minPrecision, 1e-7);
  handle.param<double>("affine_scaling_factor", imageScaling, 4.0);
  return new AffineTransformEstimator(maxIterations,
                                      minPrecision,
                                      imageScaling);
}

VisualUtilityEstimator* CreateVisualUtilityEstimator(
  ros::NodeHandle handle,
  const TransformEstimator& transformEstimator) {

  double imgScaling;
  handle.param<double>("img_scaling", imgScaling, 1.0);

  string vuWrapperClass;
  handle.param<string>("vu_estimator_wrapper", vuWrapperClass, "");

  string vuEstimatorClass;
  handle.param<string>("vu_estimator", vuEstimatorClass,
                       "LABMotionVUEstimator");
  VisualUtilityEstimator* baseEstimator = NULL;
  if (vuEstimatorClass == "LABMotionVUEstimator") {
    double paretoThreshold;
    double distDecay;
    int openingSize;
    handle.param<double>("pareto_thresh", paretoThreshold, 0.03);
    handle.param<double>("dist_decay", distDecay, 2.0);
    handle.param<int>("opening_size", openingSize, 3);
    baseEstimator = new LABMotionVUEstimator(transformEstimator,
                                             paretoThreshold,
                                             distDecay,
                                             openingSize);
  } else if (vuEstimatorClass == "SpectralSaliency") {
    baseEstimator = new SpectralSaliency();
  } else if (vuEstimatorClass == "LaplacianVU") {
    int laplacianSize;
    handle.param<int>("laplacian_size", laplacianSize, 3);
    baseEstimator = new LaplacianVU(laplacianSize);
  } else if (vuEstimatorClass == "CenterSurroundHistogram") {
    string histDistType;
    handle.param<string>("hist_dist_type", histDistType, "chisq");
    string scaleStr;
    handle.param<string>("estimator_scales", scaleStr, "1.0");
    vector<double> estimatorScales = ParseDataList<double>(scaleStr);
    baseEstimator = new CenterSurroundHistogram(estimatorScales,
                                                histDistType);
  } else if (vuEstimatorClass == "Objectness") {
    baseEstimator = new Objectness();
  } else if (vuEstimatorClass == "HOGDetector") {
    string hogModelFile;
    handle.param<string>("hog_model_file", hogModelFile, "");
    bool hogDoPeople;
    handle.param<bool>("hog_do_people", hogDoPeople, true);
    bool hogDoCache;
    handle.param<bool>("hog_do_cache", hogDoCache, true);
    int winStride;
    cv::Size cvWinStride;
    if (handle.getParam("win_stride", winStride)) {
      if (vuWrapperClass == "ScaledDetectorWrapper") {
        winStride = cvRound(winStride*imgScaling);
      }
      cvWinStride = cv::Size(winStride, winStride);
    }
    baseEstimator = new HOGDetector(hogModelFile,
                                    hogDoPeople,
                                    cvWinStride,
                                    hogDoCache);
  } else if (vuEstimatorClass == "IntegralHOGDetector") {
    string hogModelFile;
    handle.param<string>("hog_model_file", hogModelFile, "");

    int winStride;
    cv::Size cvWinStride;
    handle.param<int>("win_stride", winStride, 8);
    cvWinStride = cv::Size(winStride, winStride);

    baseEstimator = new IntegralHOGDetector(hogModelFile, cvWinStride);
  } else if (vuEstimatorClass == "IntegralHOGCascade") {
    string hogModelFile;
    handle.param<string>("hog_model_file", hogModelFile, "");

    int winStride;
    cv::Size cvWinStride;
    handle.param<int>("win_stride", winStride, 8);
    cvWinStride = cv::Size(winStride, winStride);

    baseEstimator = new IntegralHOGCascade(hogModelFile, cvWinStride);
  } else if (vuEstimatorClass == "CascadeDetector") {
    string modelFile;
    handle.param<string>("cascade_model_file", modelFile, "");
    baseEstimator = new CascadeDetector(modelFile);
  }

  if (vuWrapperClass == "RelativeEntropyVUWrapper") {
    return new RelativeEntropyVUWrapper(baseEstimator);
  } else if (vuWrapperClass == "ScaledDetectorWrapper") {
    return new ScaledDetectorWrapper(baseEstimator, imgScaling);
  } else {
    return baseEstimator;
  }

  ROS_FATAL("Invalid VisualUtilityEstimator specified. "
            "Please set the vu_estimator parameters");
  return NULL;

}

VisualUtilityMosaic* CreateVisualUtilityMosaic(
  ros::NodeHandle handle,
  const TransformEstimator& transformEstimator) {
  int morphCloseSize;
  double gaussSigma;
  handle.param<int>("morph_close_size", morphCloseSize, 0);
  handle.param<double>("gauss_sigma", gaussSigma, 0.0);
  return new NullVUMosaic(morphCloseSize, gaussSigma);
}

FrameEstimator* CreateFrameEstimator(ros::NodeHandle handle) {
  int xFramesize;
  int yFramesize;
  double frameExpansion;
  string frameEstimatorClass;
  handle.param<int>("xframesize", xFramesize, 192);
  handle.param<int>("yframesize", yFramesize, 108);
  handle.param<double>("frame_expansion", frameExpansion, 1.0);
  handle.param<string>("frame_estimator", frameEstimatorClass,
                            "MaxPointConstantFramesize");
  if (frameEstimatorClass == "MaxPointConstantFramesize") {
    return new MaxPointConstantFramesize(
      Size_<int>(xFramesize, yFramesize));
  } else if (frameEstimatorClass == "RandomPointConstantFramesize") {
    return new RandomPointConstantFramesize(
      Size_<int>(xFramesize, yFramesize));
  } else if (frameEstimatorClass == "HighRelativeEntropy") {
    int minFrameArea;
    double minEntropy;
    handle.param<int>("min_frame_area", minFrameArea, 200);
    handle.param<double>("min_entropy", minEntropy, 0.2);
    return new HighRelativeEntropy(frameExpansion,
                                   minFrameArea,
                                   minEntropy);
  }

  ROS_FATAL("Invalid FrameEstimator specified. "
            "Please set the frame_estimator parameters");
  return NULL;
}

} //namespace

ext: cc


text:
#ifndef SLEIP_DYNAMIC_ARRAY_FWD_HPP_
#define SLEIP_DYNAMIC_ARRAY_FWD_HPP_

// so we can have _HAS_CXX17
//
#if __has_include(<vcruntime.h>)
#include <vcruntime.h>
#endif

#include <boost/config.hpp>

#if !__has_include(<memory_resource>) || (!BOOST_MSVC && __cplusplus < 201703L) || (BOOST_MSVC && !_HAS_CXX17)
#define SLEIP_NO_CXX17_PMR
#endif

#include <memory>
#ifndef SLEIP_NO_CXX17_PMR
#include <memory_resource>
#endif

namespace sleip
{
template <class T, class Allocator = std::allocator<T>>
struct dynamic_array;

#ifndef SLEIP_NO_CXX17_PMR
namespace pmr
{
template <class T>
using dynamic_array = ::sleip::dynamic_array<T, std::pmr::polymorphic_allocator<T>>;
} // namespace pmr
#endif

template <class T, class Allocator>
auto
operator==(dynamic_array<T, Allocator> const& lhs, dynamic_array<T, Allocator> const& rhs) -> bool;

template <class T, class Allocator>
auto
operator!=(dynamic_array<T, Allocator> const& lhs, dynamic_array<T, Allocator> const& rhs) -> bool;

template <class T, class Allocator>
auto
operator<(dynamic_array<T, Allocator> const& lhs, dynamic_array<T, Allocator> const& rhs) -> bool;

template <class T, class Allocator>
auto
operator>(dynamic_array<T, Allocator> const& lhs, dynamic_array<T, Allocator> const& rhs) -> bool;

template <class T, class Allocator>
auto
operator<=(dynamic_array<T, Allocator> const& lhs, dynamic_array<T, Allocator> const& rhs) -> bool;

template <class T, class Allocator>
auto
operator>=(dynamic_array<T, Allocator> const& lhs, dynamic_array<T, Allocator> const& rhs) -> bool;
} // namespace sleip

#endif // SLEIP_DYNAMIC_ARRAY_FWD_HPP_


text:
#include "ExpressionMatrix.hpp"
#include "SimilarPairs.hpp"
using namespace ChanZuckerberg;
using namespace ExpressionMatrix2;

#include <boost/algorithm/string.hpp>

#include <regex>

void ExpressionMatrix::removeGeneSet(
    const string& geneSetName)
{
    // Sanity check: prevent removal of the AllGenes gene set.
    if(geneSetName == "AllGenes") {
        throw runtime_error("Gene set AllGenes cannot be removed.");
    }

    // Locate the gene set to be removed.
    const auto it = geneSets.find(geneSetName);
    if(it == geneSets.end()) {
        throw runtime_error("Gene set " + geneSetName + " does not exist.");
    }
    GeneSet& geneSet = it->second;

    // Remove it from disk.
    geneSet.remove();

    // Remove it from our table of gene sets.
    geneSets.erase(it);
}

// Create a new gene set consisting of genes whose name matches a given regular expression.
bool ExpressionMatrix::createGeneSetFromRegex(const string& geneSetName, const string& regexString)
{
    // Check if a gene set with this name already exists.
    if(geneSets.find(geneSetName) != geneSets.end()) {
        return false;
    }

    // Create the regular expression we are going to match.
    const std::regex regex(regexString);

    // Create the new gene set.
    GeneSet& geneSet = geneSets[geneSetName];
    geneSet.createNew(directoryName + "/GeneSet-" + geneSetName);
    for(GeneId geneId = 0; geneId != geneCount(); geneId++) {
        const string geneName = geneNames[geneId];
        if(std::regex_match(geneName, regex)) {
            geneSet.addGene(geneId);
        }
    }
    geneSet.sort();
    return true;
}

// Create a gene set consisting of the genes with names passed in a vector.
// Names that don't correspond to valid gene names are ignored.
// Returns true if successful, false if the specified gene set already exists.
bool ExpressionMatrix::createGeneSetFromGeneNames(
    const string& geneSetName,
    const vector<string>& geneNamesVector,
    int& ignoredCount,
    int& emptyCount)
{
    // Check if a gene set with this name already exists.
    if(geneSets.find(geneSetName) != geneSets.end()) {
        return false;
    }

    // Create the new gene set.
    GeneSet& geneSet = geneSets[geneSetName];
    geneSet.createNew(directoryName + "/GeneSet-" + geneSetName);
    ignoredCount = 0;
    emptyCount = 0;
    for(const string& geneName: geneNamesVector) {
        if(geneName.empty()) {
            ++emptyCount;
            continue;
        }
        const StringId stringId = geneNames(geneName);
        if(stringId == geneNames.invalidStringId) {
            ++ignoredCount;
        } else {
            geneSet.addGene(GeneId(stringId));
        }
    }
    geneSet.sort();

    return true;
}

// Same as above, but throw an exception if any of the gene names are empty
// or do not correspond to any gene.
void ExpressionMatrix::createGeneSetFromGeneNames(
    const string& geneSetName,
    const vector<string>& geneNamesVector)
{
    // Check if a gene set with this name already exists.
    if(geneSets.find(geneSetName) != geneSets.end()) {
        throw runtime_error("Gene set " + geneSetName + " already exists.");
    }

    // Create the new gene set.
    GeneSet& geneSet = geneSets[geneSetName];
    geneSet.createNew(directoryName + "/GeneSet-" + geneSetName);
    for(const string& geneName: geneNamesVector) {
        if(geneName.empty()) {
            throw runtime_error("Empty gene name while creating gene set " + geneSetName);
        }
        const StringId stringId = geneNames(geneName);
        if(stringId == geneNames.invalidStringId) {
            throw runtime_error("Unknown gene name name " + geneName + " while creating gene set " + geneSetName);
        } else {
            geneSet.addGene(GeneId(stringId));
        }
    }
    geneSet.sort();

}

// Create a gene set consisting of the genes with ids passed in a vector.
void ExpressionMatrix::createGeneSetFromGeneIds(
    const string& geneSetName,
    const vector<GeneId>& geneIds)
{
    // Check if a gene set with this name already exists.
    if(geneSets.find(geneSetName) != geneSets.end()) {
        throw runtime_error("Gene set " + geneSetName + " already exists.");
    }

    // Create the new gene set.
    GeneSet& geneSet = geneSets[geneSetName];
    geneSet.createNew(directoryName + "/GeneSet-" + geneSetName);
    for(const GeneId& geneId: geneIds) {
        geneSet.addGene(geneId);
    }
    geneSet.sort();

}

bool ExpressionMatrix::createGeneSetIntersection(const string& inputSetsNames, const string& outputSetName)
{
    return createGeneSetIntersectionOrUnion(inputSetsNames, outputSetName, false);
}
bool ExpressionMatrix::createGeneSetUnion(const string& inputSetsNames, const string& outputSetName)
{
    return createGeneSetIntersectionOrUnion(inputSetsNames, outputSetName, true);
}
bool ExpressionMatrix::createGeneSetIntersectionOrUnion(
    const string& commaSeparatedInputSetsNames,
    const string& outputSetName,
    bool doUnion)
{
    // See if a gene set with the name of the output gene set already exists.
    if(geneSets.find(outputSetName) != geneSets.end()) {
        cout << "Gene set " << outputSetName << " already exists." << endl;
        return false;
    }

    // Parse the input gene sets.
    vector<string> inputSetsNames;
    boost::algorithm::split(inputSetsNames, commaSeparatedInputSetsNames, boost::is_any_of(","));

    // Check that all input gene sets exist.
    for(const string& inputSetName: inputSetsNames) {
        if(geneSets.find(inputSetName) == geneSets.end()) {
            cout << "gene set " << inputSetName << " does not exists." << endl;
            return false;
        }
    }

    // Compute the intersection or union.
    vector<GeneId> outputSetGenes;
    for(size_t i=0; i<inputSetsNames.size(); i++) {
        const string& inputSetName = inputSetsNames[i];
        vector<GeneId> inputSetGenes;
        geneSets[inputSetName].assertIsSorted();
        geneSets[inputSetName].getSortedGenes(inputSetGenes);
        if(i == 0) {
            outputSetGenes = inputSetGenes;
        } else {
            vector<GeneId> newOutputSetGenes;
            if(doUnion) {
                std::set_union(
                    outputSetGenes.begin(), outputSetGenes.end(),
                    inputSetGenes.begin(), inputSetGenes.end(),
                    back_inserter(newOutputSetGenes));
            } else {
                std::set_intersection(
                    outputSetGenes.begin(), outputSetGenes.end(),
                    inputSetGenes.begin(), inputSetGenes.end(),
                    back_inserter(newOutputSetGenes));
            }
            outputSetGenes.swap(newOutputSetGenes);
        }
    }

    // Store this gene set.
    GeneSet& outputGeneSet = geneSets[outputSetName];
    outputGeneSet.createNew(directoryName + "/GeneSet-" + outputSetName);
    for(const GeneId geneId: outputSetGenes) {
        outputGeneSet.addGene(geneId);
    }
    outputGeneSet.sort();
    return true;
}

bool ExpressionMatrix::createGeneSetDifference(
    const string& inputSetName0,
    const string& inputSetName1,
    const string& outputSetName)
{
    // See if a gene set with the name of the output gene set already exists.
    if(geneSets.find(outputSetName) != geneSets.end()) {
        cout << "Gene set " << outputSetName << " already exists." << endl;
        return false;
    }

    // Locate the input gene sets.
    const auto it0 = geneSets.find(inputSetName0);
    if(it0 == geneSets.end()) {
        cout << "Gene set " << inputSetName0 << " does not exists." << endl;
        return false;
    }
    GeneSet& inputSet0 = it0->second;
    inputSet0.assertIsSorted();
    const auto it1 = geneSets.find(inputSetName1);
    if(it1 == geneSets.end()) {
        cout << "Gene set " << inputSetName1 << " does not exists." << endl;
        return false;
    }
    GeneSet& inputSet1 = it1->second;
    inputSet1.assertIsSorted();

    // Compute the difference.
    vector<GeneId> inputSet0Genes;
    vector<GeneId> inputSet1Genes;
    inputSet0.getSortedGenes(inputSet0Genes);
    inputSet1.getSortedGenes(inputSet1Genes);
    vector<GeneId> outputSetGenes;
    std::set_difference(
        inputSet0Genes.begin(), inputSet0Genes.end(),
        inputSet1Genes.begin(), inputSet1Genes.end(),
        back_inserter(outputSetGenes));

    // Store this gene set.
    GeneSet& outputGeneSet = geneSets[outputSetName];
    outputGeneSet.createNew(directoryName + "/GeneSet-" + outputSetName);
    for(const GeneId geneId: outputSetGenes) {
        outputGeneSet.addGene(geneId);
    }
    outputGeneSet.sort();
    return true;
}

// Returns the names of the gene sets in the geneSets map that are identical
// to the gene set of a SimilarPairs object with the given name.
// Note that there could be zero, one, or multiple gene sets
// that satisfy this condition.
vector<string> ExpressionMatrix::geneSetNamesFromSimilarPairsName(const string& similarPairsName) const
{
    // Open the existing SimilarPairs object.
    const SimilarPairs similarPairs(directoryName + "/SimilarPairs-" + similarPairsName, true);

    // Start with no gene sets.
    vector<string> geneSetNames;

    // Loop over our map of gene sets.
    for(auto it=geneSets.begin(); it!=geneSets.end(); ++it) {
        if(it->second == similarPairs.getGeneSet()) {
            geneSetNames.push_back(it->first);
        }

    }

    // Return the names we found.
    return geneSetNames;
}


text:
#include "Dependency.h"
#include "Constants.h"
#include <boost/algorithm/string.hpp>
#include <boost/log/trivial.hpp>

const std::map<std::string,Dependency::Type> str2type = {
    {"bcomBuild",Dependency::Type::REMAKEN},
    {"thirdParties",Dependency::Type::REMAKEN},
    {"artifactory",Dependency::Type::REMAKEN},
    {"nexus",Dependency::Type::REMAKEN},
    {"github",Dependency::Type::REMAKEN},
    {"path",Dependency::Type::REMAKEN},
    {"conan",Dependency::Type::CONAN},
    {"system",Dependency::Type::SYSTEM},
    {"vcpkg",Dependency::Type::VCPKG}
};

Dependency::Type deduceType(const std::string typeStr)
{
    if (str2type.find(typeStr) != str2type.end()) {
        return str2type.at(typeStr);
    }
    return Dependency::Type::REMAKEN;
}

std::string stripEndlineChar(const std::string & str)
{
    std::string strippedStr = str;
    if (!strippedStr.empty() && strippedStr[strippedStr.size() - 1] == '\r') {
        strippedStr.erase(strippedStr.size() - 1);
    }
    return strippedStr;
}

static const std::map<std::string,std::string> identifier2repoType = {
    {"bcomBuild","artifactory"},
    {"thirdParties","artifactory"},
    {"apt-get","system"},
    {"brew","system"},
    {"choco","system"},
    {"scoop","system"},
    {"pacman","system"},
    {"pkg","system"},
    {"pkgutil","system"},
    {"yum","system"},
    {"zypper","system"}
};

std::ostream& operator<< (std::ostream& stream, const Dependency& dep)
{
    stream << dep.getName() << dep.getVersion() << '\n';
    return stream;
}

Dependency::Dependency(const std::string & rawFormat, const std::string & mainMode):m_packageChannel("stable"),m_repositoryType("github")
{
    std::vector<std::string> results, pkgInformations, repositoryInformations;
    boost::split(results, rawFormat, [](char c){return c == '|';});
    boost::split(pkgInformations, results[0], [](char c){return c == '#';});
    boost::split(repositoryInformations, results[3], [](char c){return c == '@';});
    m_packageName = pkgInformations[0];
    boost::trim(m_packageName);
    if (pkgInformations.size() >= 2) {
        m_packageChannel = pkgInformations[1];
        boost::trim(m_packageChannel);
    }
    m_version = results[1];
    boost::trim(m_version);
    m_name = results[2];
    boost::trim(m_name);
    m_identifier = repositoryInformations[0];
    boost::trim(m_identifier);
    m_repositoryType = m_identifier;
    if (identifier2repoType.find(m_identifier) != identifier2repoType.end()) {
        m_repositoryType = identifier2repoType.at(m_identifier);
    } // should lead to an error when no transcription exists ? in validate ??
    if (repositoryInformations.size() >= 2) {
        m_repositoryType = repositoryInformations[1];
        boost::trim(m_repositoryType);
        m_bHasIdentifier = !m_identifier.empty();
    }
    m_type = deduceType(m_repositoryType);

    if (m_identifier != m_repositoryType) {
        m_bHasIdentifier = !m_identifier.empty();
    }

    if (results.size() < 5) {
        if ((m_repositoryType == "conan") ||
            (m_repositoryType == "system") ||
            (m_repositoryType == "vcpkg")) {
                m_baseRepository = m_repositoryType;
        }
    }
    else {
        m_baseRepository = stripEndlineChar(results[4]);
        boost::trim(m_baseRepository);
        if ((m_baseRepository.empty()) &&
            ((m_repositoryType == "conan") ||
            (m_repositoryType == "system") ||
            (m_repositoryType == "vcpkg"))) {
                m_baseRepository = m_repositoryType;
        }
    }

    if (m_baseRepository.find("https://github") != std::string::npos) {// github url
        if ((m_repositoryType == "artifactory") &&
                ((m_identifier == "bcomBuild") ||
                (m_identifier == "thirdParties"))) { // erroneous deduction : repository type maybe a github repository
            m_repositoryType = "github";
        }
    }

    if (results.size() >= 6){
        m_mode = stripEndlineChar(results[5]);
        boost::trim(m_mode);
        if (m_mode!= "static" && m_mode!= "shared" && m_mode!= "na") {
             m_mode = mainMode;
        }
    }
    else {
        m_mode = mainMode;
        /*if (m_type == Dependency::Type::CONAN) {// before conan mode was set to na
            m_mode = "na"; // should detect supported options
        }*/
    }
    if (results.size() == 7){
        m_toolOptions = stripEndlineChar(results[6]);
        if (!m_toolOptions.empty()) {
            m_bHasOptions = true;
        }
    }
}

static const std::vector<std::string> repoValidation = {"artifactory","github","nexus","path","vcpkg","conan","system"};
static const std::vector<std::string> linkModeValidation = {"static","shared","default","na"};
static const std::map<std::string,std::vector<std::string>> unsupportedLinkModeRelations = {{"na",{"artifactory","nexus","github","path","vcpkg"}}};
static const std::vector<std::string> systemIdentifierMap = {"system","apt-get","brew","yum","choco","scoop","pkg", "pkgutil", "pacman", "zypper"};

bool Dependency::validate()
{
    if (find(repoValidation,m_repositoryType) == std::end(repoValidation)) {
        BOOST_LOG_TRIVIAL(error)<<"Dependency file error : invalid repository type : unsupported value= "<< m_repositoryType<<std::endl<<log(*this);
        return false;
    }

    if (m_baseRepository.empty()) {
        BOOST_LOG_TRIVIAL(error)<<"Dependency file error : missing repository url for "<<m_packageName<<"/"<< m_version<<" package"<<std::endl<<log(*this);
    }

    if (m_repositoryType == "system") {
        if (find(systemIdentifierMap,m_identifier) == std::end(systemIdentifierMap)) {
            BOOST_LOG_TRIVIAL(error)<<"Dependency file error : invalid tool identifier  value= "<<m_identifier<<" for "<< m_repositoryType<<" repository"<<std::endl<<log(*this);
            return false;
        }
    }
    else {
        if (find(systemIdentifierMap,m_identifier) != std::end(systemIdentifierMap)) {
            BOOST_LOG_TRIVIAL(error)<<"Dependency file error : invalid identifier value= "<<m_identifier<<" for "<< m_repositoryType<<" repository"<<std::endl<<log(*this);
            return false;
        }
    }
    if (find(linkModeValidation,m_mode) == std::end(linkModeValidation)) {
        BOOST_LOG_TRIVIAL(error)<<"Dependency file error : invalid link mode value= "<<m_mode<<std::endl<<log(*this);
        return false;
    }
    for (auto & element:unsupportedLinkModeRelations) {
        if (m_mode == element.first) {
            auto & unsupportedRepolist = element.second;
            if (find(unsupportedRepolist,m_repositoryType) != std::end(unsupportedRepolist)) {
                BOOST_LOG_TRIVIAL(error)<<"Dependency file error : invalid link mode= "<<m_mode<<" for "<< m_repositoryType<<" repository"<<std::endl<<log(*this);
                return false;
            }
        }
    }
    return true;
}

max_stars_repo_name: b-com-software-basis/remaken

max_issues_repo_name: b-com-software-basis/remaken

max_forks_repo_name: b-com-software-basis/remaken


text:
/**
 *-----------------------------------------------------------------------------
 * Title         : AXI Stream FIFO
 * ----------------------------------------------------------------------------
 * File          : Fifo.cpp
 * Author        : Ryan Herbst <rherbst@slac.stanford.edu>
 * Created       : 02/02/2018
 *-----------------------------------------------------------------------------
 * Description :
 *    AXI Stream FIFO
 *-----------------------------------------------------------------------------
 * This file is part of the rogue software platform. It is subject to
 * the license terms in the LICENSE.txt file found in the top-level directory
 * of this distribution and at:
    * https://confluence.slac.stanford.edu/display/ppareg/LICENSE.html.
 * No part of the rogue software platform, including this file, may be
 * copied, modified, propagated, or distributed except according to the terms
 * contained in the LICENSE.txt file.
 *-----------------------------------------------------------------------------
**/
#include <stdint.h>
#include <thread>
#include <memory>
#include <rogue/interfaces/stream/Master.h>
#include <rogue/interfaces/stream/Slave.h>
#include <rogue/interfaces/stream/Frame.h>
#include <rogue/interfaces/stream/FrameLock.h>
#include <rogue/interfaces/stream/FrameIterator.h>
#include <rogue/interfaces/stream/Buffer.h>
#include <rogue/interfaces/stream/Fifo.h>
#include <rogue/Logging.h>
#include <rogue/GilRelease.h>

namespace ris = rogue::interfaces::stream;

#ifndef NO_PYTHON
#define BOOST_BIND_GLOBAL_PLACEHOLDERS
#include <boost/python.hpp>
namespace bp  = boost::python;
#endif

//! Class creation
ris::FifoPtr ris::Fifo::create(uint32_t maxDepth, uint32_t trimSize, bool noCopy) {
   ris::FifoPtr p = std::make_shared<ris::Fifo>(maxDepth,trimSize,noCopy);
   return(p);
}

//! Setup class in python
void ris::Fifo::setup_python() {
#ifndef NO_PYTHON
   bp::class_<ris::Fifo, ris::FifoPtr, bp::bases<ris::Master,ris::Slave>, boost::noncopyable >("Fifo",bp::init<uint32_t,uint32_t,bool>())
      .def("size",     &Fifo::size)
      .def("dropCnt",  &Fifo::dropCnt)
      .def("clearCnt", &Fifo::clearCnt)
   ;
#endif
}

//! Creator with version constant
ris::Fifo::Fifo(uint32_t maxDepth, uint32_t trimSize, bool noCopy )
:
   ris::Master   ( ),
   ris::Slave    ( ),
   log_          ( rogue::Logging::create("stream.Fifo") ),
   maxDepth_     ( maxDepth ),
   trimSize_     ( trimSize ),
   noCopy_       ( noCopy ),
   dropFrameCnt_ ( 0 ),
   threadEn_     ( true ),
   thread_       ( new std::thread(&ris::Fifo::runThread, this) )
{
   queue_.setThold(maxDepth);

   // Set a thread name
#ifndef __MACH__
   pthread_setname_np( thread_->native_handle(), "Fifo" );
#endif
}

//! Deconstructor
ris::Fifo::~Fifo() {
   threadEn_ = false;
   rogue::GilRelease noGil;
   queue_.stop();
   thread_->join();
   delete thread_;
}

//! Return the number of elements in the Fifo
std::size_t ris::Fifo::size() {
   return queue_.size();
};

//! Return the number of dropped frames
std::size_t ris::Fifo::dropCnt() const {
   return dropFrameCnt_;
}

//! Clear counters
void ris::Fifo::clearCnt() {
   dropFrameCnt_ = 0;
}

//! Accept a frame from master
void ris::Fifo::acceptFrame ( ris::FramePtr frame ) {
   uint32_t       size;
   ris::FramePtr  nFrame;
   ris::FrameIterator src;
   ris::FrameIterator dst;

   // FIFO is full, drop frame
   if ( queue_.busy() ) {
      ++dropFrameCnt_;
      return;
   }

   rogue::GilRelease noGil;
   ris::FrameLockPtr lock = frame->lock();

   // Do we copy the frame?
   if ( noCopy_ ) nFrame = frame;
   else{

      // Get size, adjust if trim is enabled
      size = frame->getPayload();
      if ( trimSize_ != 0 && trimSize_ < size ) size = trimSize_;

      // Request a new frame to hold the data
      nFrame = reqFrame(size,true);
      nFrame->setPayload(size);

      // Get destination pointer
      src = frame->begin();
      dst = nFrame->begin();

      // Copy the frame
      ris::copyFrame(src, size, dst);
      nFrame->setError(frame->getError());
      nFrame->setChannel(frame->getChannel());
      nFrame->setFlags(frame->getFlags());
   }

   // Append to buffer
   queue_.push(nFrame);
}

//! Thread background
void ris::Fifo::runThread() {
   ris::FramePtr frame;
   log_->logThreadId();

   while(threadEn_) {
      if ( (frame = queue_.pop()) != NULL ) sendFrame(frame);
   }
}

max_stars_repo_name: slaclab/rouge

max_issues_repo_name: slaclab/rouge

max_forks_repo_name: slaclab/rouge


text:
#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/asio.hpp>
#include <boost/asio/placeholders.hpp>
#include <boost/system/error_code.hpp>
#include <boost/bind/bind.hpp>

using namespace boost::asio;
using namespace std;

class server
{
    typedef server this_type;
    typedef ip::tcp::acceptor acceptor_type;
    typedef ip::tcp::endpoint endpoint_type;
    typedef ip::tcp::socket socket_type;
    typedef ip::address address_type;
    typedef boost::shared_ptr<socket_type> sock_ptr;

private:
    io_service m_io;
    acceptor_type m_acceptor;

public:
    server() : m_acceptor(m_io, endpoint_type(ip::tcp::v4(), 6688))
    {    accept();    }

    void run(){ m_io.run();}

    void accept()
    {
        sock_ptr sock(new socket_type(m_io));
        m_acceptor.async_accept(*sock, boost::bind(&this_type::accept_handler, this, boost::asio::placeholders::error, sock));
    }

    void accept_handler(const boost::system::error_code& ec, sock_ptr sock)
    {
        if (ec)
        {    return;    }

        cout<<"Client:";
        cout<<sock->remote_endpoint().address()<<endl;
        sock->async_write_some(buffer("hello asio"), boost::bind(&this_type::write_handler, this, boost::asio::placeholders::error));
        // 发送完毕后继续监听，否则io_service将认为没有事件处理而结束运行
        accept();
    }

    void write_handler(const boost::system::error_code&ec)
    {
        cout<<"send msg complete"<<endl;
    }
};

// int main()
// {
//     try
//     {
//         cout<<"Server start."<<endl;
//         server srv;
//         srv.run();
//     }
//     catch (std::exception &e)
//     {
//         cout<<e.what()<<endl;
//     }

//     return 0;
// }

max_stars_repo_name: czjone/node

max_issues_repo_name: czjone/node

max_forks_repo_name: czjone/node


text:
#include "test/test_bitcoin.h"
#include "mempoolfeemodifier.h"
#include "uint256.h"

#include <boost/test/unit_test.hpp>

BOOST_FIXTURE_TEST_SUITE(mempoolfeemodifier_tests, BasicTestingSetup)

BOOST_AUTO_TEST_CASE(modify_fee_delta)
{
    uint256 tx(uint256S("0xfee"));
    uint256 tx2(uint256S("0xfaa"));

    MempoolFeeModifier fee;
    BOOST_CHECK_EQUAL(CAmount(0), fee.GetDelta(tx));

    fee.AddDelta(tx, CAmount(1000));
    BOOST_CHECK_EQUAL(CAmount(1000), fee.GetDelta(tx));

    fee.AddDelta(tx, CAmount(1000));
    BOOST_CHECK_EQUAL(CAmount(2000), fee.GetDelta(tx));

    fee.AddDelta(tx2, CAmount(1000));
    BOOST_CHECK_EQUAL(CAmount(2000), fee.GetDelta(tx));
    BOOST_CHECK_EQUAL(CAmount(1000), fee.GetDelta(tx2));

    fee.RemoveDelta(tx2);
    BOOST_CHECK_EQUAL(CAmount(2000), fee.GetDelta(tx));
    BOOST_CHECK_EQUAL(CAmount(0), fee.GetDelta(tx2));
}

BOOST_AUTO_TEST_SUITE_END()


text:
// Copyright 2013 National Renewable Energy Laboratory (NREL)
//
//    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.
//
// ++==++==++==++==++==++==++==++==++==++==

/* dakota_python_interface.cpp

This file implements a simple boost based python binding for the dakota library.

   Goal is to be able to simply say:

   import dakota
   dakota.run()

   and have dakota do its thing

   In actual fact we implement 3 ways:
   1) run_dakota(file) -- no args, just run it as if from the comnand line, with input file "file"
   2) run_dakota_data(file, data) -- "data" is any python object, it is passed back to your interface function
   3) run_dakota_mpi_data(file, comm, data) -- "comm" is an mpi_communicator over which the work is divided

*/

#include "string.h"

#include "dakface.hpp"

#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/numeric.hpp>
#ifdef DAKOTA_HAVE_MPI
#include <boost/mpi.hpp>
#endif

#ifdef WINDOWS
#include <windows.h>
#endif

#include <numpy/arrayobject.h>

namespace bp = boost::python;
namespace bpn = boost::python::numeric;

#define MAKE_ARGV \
  char *argv[10]; \
  int argc = 0; \
  argv[argc++] = const_cast<char*>("dakota"); \
  argv[argc++] = const_cast<char*>("-i"); \
  argv[argc++] = infile; \
  if (outfile && strlen(outfile)) { \
    argv[argc++] = const_cast<char*>("-o"); \
    argv[argc++] = outfile; \
  } \
  if (errfile && strlen(errfile)) { \
    argv[argc++] = const_cast<char*>("-e"); \
    argv[argc++] = errfile; \
  }

int run_dakota(char *infile, char *outfile, char *errfile, bp::object exc)
{
  MAKE_ARGV

  void *tmp_exc = NULL;
  if (exc)
    tmp_exc = &exc;

  return all_but_actual_main(argc, argv, tmp_exc);
}

#ifdef DAKOTA_HAVE_MPI
int run_dakota_mpi(char *infile, boost::mpi::communicator &_mpi,
#else
int run_dakota_mpi(char *infile, int &_mpi,
#endif
                   char *outfile, char *errfile, bp::object exc)
{
  MAKE_ARGV
  MPI_Comm comm = MPI_COMM_WORLD;
  if (_mpi)
    comm = _mpi;

  void *tmp_exc = NULL;
  if (exc)
    tmp_exc = &exc;

  return all_but_actual_main_mpi(argc, argv, comm, tmp_exc);
}

// When DAKOTA fails it throws an int (if the process isn't aborted).
// Normally Python model errors will have already recorded an exception.
// If not, we record one here.
void translator(const int& exc)
{
  if (!PyErr_Occurred()) {
    // No exception recorded yet.
    PyErr_SetString(PyExc_RuntimeError, "DAKOTA run failed");
  }
}

#include <boost/python.hpp>
using namespace boost::python;
BOOST_PYTHON_MODULE(pyDAKOTA)
{
  using namespace bpn;
  import_array();
  array::set_module_and_type("numpy", "ndarray");

  register_exception_translator<int>(&translator);

  def("run_dakota", run_dakota, "run dakota");
  def("run_dakota_mpi", run_dakota_mpi, "run dakota mpi");
}