<commit_before>/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */

#include <gecode/driver.hh>
#include <gecode/int.hh>
#include <gecode/minimodel.hh>

using namespace Gecode;

//
// g++ -g simple_dependency.cpp -lgecodesupport -lgecodekernel -lgecodeint -lgecodesearch -lgecodedriver -lpthread -ldl   -lstdc++ -o simple_dependency
//


class SimpleDependency : public MinimizeScript {
protected:
  static const int PKG_COUNT = 10;
  
  IntVarArray package_versions;
  IntArgs disabled_package_weights; 
  BoolVarArray disabled_package_variables;
  IntVar total_disabled;
public:
  /// Actual model
  SimpleDependency(const Options& opt) : 
    package_versions(*this, PKG_COUNT, -1, 10), 
    disabled_package_variables(*this, PKG_COUNT, 0, 1),
    total_disabled(*this, 0, 10*PKG_COUNT)  
  {

    // Add version constraints for pkg 0
    AddVersionConstraint(0, 0, 1, 1, 5);
    AddVersionConstraint(0, 1, 1, 2, 5);
    //    AddVersionConstraint(0,
 
    IntArgs package_weights(PKG_COUNT, 10, 10, 10, 10, 10,  10, 10, 10, 10, 10 );

    std::cout << "Package versions:           " << package_versions << std::endl;
    std::cout << "Disabled package variables: " << disabled_package_variables << std::endl;
    std::cout << "Package weights             " << package_weights << std::endl;
    std::cout << "Total disabled:             " << total_disabled << std::endl;

    linear(*this, disabled_package_variables, IRT_EQ, total_disabled);
    
    branch(*this, package_versions, INT_VAR_SIZE_MIN, INT_VAL_MAX);
    branch(*this, disabled_package_variables, INT_VAR_SIZE_MIN, INT_VAL_MAX);
    branch(*this, total_disabled, INT_VAL_MIN);
  }

  bool AddVersionConstraint(int packageId, int version, 
			    int dependentPackageId, int minDependentVersion, int maxDependentVersion) 
  {
    BoolVar version_match(*this, 0, 1);
    BoolVar depend_match(*this, 0, 1);
    BoolVar predicated_depend_match(*this, 0, 1);
    
    std::cout << "Add VC for " << packageId << " @ " << version << " depPkg " << dependentPackageId;
    std::cout << " [ " << minDependentVersion << ", " << maxDependentVersion << " ]" << std::endl;
    std::cout.flush();

    //version_flags << version_match;
    // Constrain pred to reify package @ version
    rel(*this, package_versions[packageId], IRT_EQ, version, version_match);
    // Add the predicated version constraints imposed on dependent package
    dom(*this, package_versions[dependentPackageId], minDependentVersion, maxDependentVersion, depend_match);
    // disabled_package_variables[dependentPackageId] OR depend_match <=> version_match
    
    rel(*this, disabled_package_variables[dependentPackageId], BOT_OR, depend_match, predicated_depend_match);
    rel(*this, version_match, BOT_IMP, predicated_depend_match, 1);  
  }
  
  
  /// Print solution
  virtual void
  print(std::ostream& os) const {
    os << "\t" << package_versions << std::endl;
    os << "\t" << disabled_package_variables << std::endl;
    os << "\t" << total_disabled << std::endl;<commit_msg>Replicate our failing constraint system in standalone code.<commit_after>/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */

#include <gecode/driver.hh>
#include <gecode/int.hh>
#include <gecode/minimodel.hh>

using namespace Gecode;

//
// g++ -g simple_dependency.cpp -lgecodesupport -lgecodekernel -lgecodeint -lgecodesearch -lgecodedriver -lpthread -ldl   -lstdc++ -o simple_dependency
//


class SimpleDependency : public MinimizeScript {
protected:
  static const int PKG_COUNT = 11;
  
  IntVarArray package_versions;
  IntArgs disabled_package_weights; 
  BoolVarArray disabled_package_variables;
  IntVar total_disabled;
public:
  /// Actual model
  SimpleDependency(const Options& opt) : 
    package_versions(*this, PKG_COUNT, -1, 10), 
    disabled_package_variables(*this, PKG_COUNT, 0, 1),
    total_disabled(*this, 0, 10*PKG_COUNT)  
  {
    Problem2();

    branch(*this, package_versions, INT_VAR_SIZE_MIN, INT_VAL_MAX);
    branch(*this, disabled_package_variables, INT_VAR_SIZE_MIN, INT_VAL_MIN);
    branch(*this, total_disabled, INT_VAL_MIN);
  }

  void Problem1() {
    // Add version constraints for pkg 0
    dom(*this, package_versions[0], -1, 0);
    dom(*this, package_versions[1], -1, 0);
    dom(*this, package_versions[2], -1, 0);
    dom(*this, package_versions[3], -1, 1);
    dom(*this, package_versions[4], -1, 0);
    dom(*this, package_versions[5], -1, 0);
    dom(*this, package_versions[6], -1, 2);
    dom(*this, package_versions[7], -1, 0);
    dom(*this, package_versions[8], -1, 0);
    dom(*this, package_versions[9], -1, -1);
    dom(*this, package_versions[10], 0, 0);

    AddVersionConstraint(0, 0, 1, 0, 1);
    AddVersionConstraint(2, 0, 1, 0, 0);
    AddVersionConstraint(1, 0, 3, 0, 1);
    AddVersionConstraint(1, 0, 4, 0, 0);
    AddVersionConstraint(1, 1, 3, 1, 1);
    AddVersionConstraint(1, 1, 5, 0, 0);
    AddVersionConstraint(7, 0, 3, -2, -2);
    AddVersionConstraint(8, 0, 9, -2, -2);
    AddVersionConstraint(10, 0, 7, 0, 0);
 
    IntArgs package_weights(PKG_COUNT, 10, 10, 10, 10, 10,  10, 10, 10, 10, 10 );

    std::cout << "Package versions:           " << package_versions << std::endl;
    std::cout << "Disabled package variables: " << disabled_package_variables << std::endl;
    std::cout << "Package weights             " << package_weights << std::endl;
    std::cout << "Total disabled:             " << total_disabled << std::endl;

    linear(*this, disabled_package_variables, IRT_EQ, total_disabled);
  }


  void Problem2() {
    // Add version constraints for pkg 0
    dom(*this, package_versions[0], -1, 0);
    dom(*this, package_versions[1], -1, 0);
    dom(*this, package_versions[2], -1, 0);
    dom(*this, package_versions[3], -1, 1);
    dom(*this, package_versions[4], -1, 0);
    dom(*this, package_versions[5], -1, 0);
    dom(*this, package_versions[6], -1, 2);
    dom(*this, package_versions[7], -1, 0);
    dom(*this, package_versions[8], -1, 0);
    dom(*this, package_versions[9], -1, -1);
    dom(*this, package_versions[10], 0, 0);

    AddVersionConstraint(0, 0, 1, 0, 1);
    AddVersionConstraint(2, 0, 1, 0, 0);
    AddVersionConstraint(1, 0, 3, 0, 1);
    AddVersionConstraint(1, 0, 4, 0, 0);
    AddVersionConstraint(1, 1, 3, 1, 1);
    AddVersionConstraint(1, 1, 5, 0, 0);
    AddVersionConstraint(7, 0, 3, -2, -2);
    AddVersionConstraint(8, 0, 9, -2, -2);
    AddVersionConstraint(10, 0, 7, 0, 0);
 
    IntArgs package_weights(PKG_COUNT, 10, 10, 10, 10, 10,  10, 10, 10, 10, 10 );

    std::cout << "Package versions:           " << package_versions << std::endl;
    std::cout << "Disabled package variables: " << disabled_package_variables << std::endl;
    std::cout << "Package weights             " << package_weights << std::endl;
    std::cout << "Total disabled:             " << total_disabled << std::endl;

    linear(*this, package_weights, disabled_package_variables, IRT_EQ, total_disabled);
  }


  bool AddVersionConstraint(int packageId, int version, 
			    int dependentPackageId, int minDependentVersion, int maxDependentVersion) 
  {
    BoolVar version_match(*this, 0, 1);
    BoolVar depend_match(*this, 0, 1);
    BoolVar predicated_depend_match(*this, 0, 1);
    
    std::cout << "Add VC for " << packageId << " @ " << version << " depPkg " << dependentPackageId;
    std::cout << " [ " << minDependentVersion << ", " << maxDependentVersion << " ]" << std::endl;
    std::cout.flush();

    //version_flags << version_match;
    // Constrain pred to reify package @ version
    rel(*this, package_versions[packageId], IRT_EQ, version, version_match);
    // Add the predicated version constraints imposed on dependent package
    dom(*this, package_versions[dependentPackageId], minDependentVersion, maxDependentVersion, depend_match);
    // disabled_package_variables[dependentPackageId] OR depend_match <=> version_match
    
    rel(*this, disabled_package_variables[dependentPackageId], BOT_OR, depend_match, predicated_depend_match);
    rel(*this, version_match, BOT_IMP, predicated_depend_match, 1);  
  }
  
  
  /// Print solution
  virtual void
  print(std::ostream& os) const {
    os << "\t" << package_versions << std::endl;
    os << "\t" << disabled_package_variables << std::endl;
    os << "\t" << total_disabled << std::endl;
{'fraction_non_alphanumeric': 0.1462009362621534, 'fraction_numerical': 0.03276917536910335, 'mean_word_length': 3.84137129575828, 'pattern_counts': {'":': 0, '<': 171, '<?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': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2992555', 'n_tokens_mistral': 2951, 'n_tokens_neox': 2683, 'n_words': 672}