<commit_before>#include <algorithm>
#include <cmath>

#include "DcoConicConstraint.hpp"
#include "DcoModel.hpp"

class OsiRowCut;

#define EPS1 1e-5

/** Default constructor. */
DcoConicConstraint::DcoConicConstraint() {
  members_ = 0;
  size_ = 0;
  // cone is lorentz cone by default.
  type_ = OSI_QUAD;
}

/** Useful constructor. */
DcoConicConstraint::DcoConicConstraint(int size, OsiLorentzConeType type, int const * members) {
  size_ = size;
  type_ = type;
  members_ = new int[size];
  std::copy(members, members+size, members_);
}

/** Destructor. */
DcoConicConstraint::~DcoConicConstraint() {
  if (members_) {
    delete[] members_;
  }
}

/** Copy constructor. */
DcoConicConstraint::DcoConicConstraint(const DcoConicConstraint & rhs) {
  setData(rhs.getSize(), rhs.getType(), rhs.getMembers());
}

double DcoConicConstraint::infeasibility(BcpsModel * m,
					 int & preferredWay) const {
  DcoModel * model = dynamic_cast<DcoModel*>(m);
  double const * sol = model->getLpSolution();
  double * par_sol = new double[size_];
  for (int i=0; i<size_; ++i) {
    par_sol[i] = sol[members_[i]];
  }
  double infeas;
  if (type_==OSI_QUAD) {
    double term1 = par_sol[0];
    double term2 = std::inner_product(par_sol+1, par_sol+size_,
				      par_sol+1, 0.0);
    term2 = sqrt(term2);
    infeas = term2-term1;
    if (infeas<EPS1) {
      infeas = 0.0;
    }
  }
  else if (type_==OSI_RQUAD) {
    double term1 = 2.0*par_sol[0]*par_sol[1];
    double term2 = std::inner_product(par_sol+2, par_sol+size_,
				      par_sol+2, 0.0);
    infeas = term2-term1;
    if (infeas<EPS1) {
      infeas = 0.0;
    }
  }
  else {
    std::cerr << "Unknown cone type!" << std::endl;
    throw std::exception();
  }
  return infeas;
}

/** Pack Dco part into an encoded object. */
AlpsReturnStatus DcoConicConstraint::encodeDco(AlpsEncoded *encoded) {
  std::cerr << "Not implemented yet!" << std::endl;
  throw std::exception();
}

/** Unpack Dco part from a encode object. */
AlpsReturnStatus DcoConicConstraint::decodeDco(AlpsEncoded &encoded) {<commit_msg>memory bug fixed.<commit_after>#include <algorithm>
#include <cmath>

#include "DcoConicConstraint.hpp"
#include "DcoModel.hpp"

class OsiRowCut;

#define EPS1 1e-5

/** Default constructor. */
DcoConicConstraint::DcoConicConstraint() {
  members_ = 0;
  size_ = 0;
  // cone is lorentz cone by default.
  type_ = OSI_QUAD;
}

/** Useful constructor. */
DcoConicConstraint::DcoConicConstraint(int size, OsiLorentzConeType type, int const * members) {
  size_ = size;
  type_ = type;
  members_ = new int[size];
  std::copy(members, members+size, members_);
}

/** Destructor. */
DcoConicConstraint::~DcoConicConstraint() {
  if (members_) {
    delete[] members_;
  }
}

/** Copy constructor. */
DcoConicConstraint::DcoConicConstraint(const DcoConicConstraint & rhs) {
  setData(rhs.getSize(), rhs.getType(), rhs.getMembers());
}

double DcoConicConstraint::infeasibility(BcpsModel * m,
					 int & preferredWay) const {
  DcoModel * model = dynamic_cast<DcoModel*>(m);
  double const * sol = model->getLpSolution();
  double * par_sol = new double[size_];
  for (int i=0; i<size_; ++i) {
    par_sol[i] = sol[members_[i]];
  }
  double infeas;
  if (type_==OSI_QUAD) {
    double term1 = par_sol[0];
    double term2 = std::inner_product(par_sol+1, par_sol+size_,
				      par_sol+1, 0.0);
    term2 = sqrt(term2);
    infeas = term2-term1;
    if (infeas<EPS1) {
      infeas = 0.0;
    }
  }
  else if (type_==OSI_RQUAD) {
    double term1 = 2.0*par_sol[0]*par_sol[1];
    double term2 = std::inner_product(par_sol+2, par_sol+size_,
				      par_sol+2, 0.0);
    infeas = term2-term1;
    if (infeas<EPS1) {
      infeas = 0.0;
    }
  }
  else {
    std::cerr << "Unknown cone type!" << std::endl;
    throw std::exception();
  }
  delete[] par_sol;
  return infeas;
}

/** Pack Dco part into an encoded object. */
AlpsReturnStatus DcoConicConstraint::encodeDco(AlpsEncoded *encoded) {
  std::cerr << "Not implemented yet!" << std::endl;
  throw std::exception();
}

/** Unpack Dco part from a encode object. */
AlpsReturnStatus DcoConicConstraint::decodeDco(AlpsEncoded &encoded) {
{'fraction_non_alphanumeric': 0.14836223506743737, 'fraction_numerical': 0.016859344894026976, 'mean_word_length': 3.84032634032634, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5294363', 'n_tokens_mistral': 1615, 'n_tokens_neox': 1546, 'n_words': 326}