Krishna
Krishna

Reputation: 1362

Unable to initialize Gecode's `IntSet` from `Vector`

As per the Gecode documentation "Modelling and Programming with Gecode", an IntVar can be created with IntSet

enter image description here

This is also confirmed by Gecode's reference for IntVar at https://www.gecode.org/doc/6.2.0/reference/classGecode_1_1IntVar.html

enter image description here

And an IntSet can be created using a vector<int> as per https://www.gecode.org/doc/6.2.0/reference/classGecode_1_1IntSet.html

enter image description here

But the following code

std::vector<int> values = {2, 4, 6, 8};
IntVar i;

Class() : i(*this, IntSet(values)) {
}

gives me an error message that looks something like this

In file included from $GECODE_HOME/include/gecode/int.hh:356,
                 from bla.cc:3:
$GECODE_HOME/include/gecode/int/int-set-1.hpp: In instantiation of ‘static void Gecode::IntSetInit<I>::init(Gecode::IntSet&, I&) [with I = std::vector<int>]’:
$GECODE_HOME/include/gecode/int/int-set-1.hpp:87:24:   required from ‘Gecode::IntSet::IntSet(I&) [with I = std::vector<int>]’
bla.cc:14:35:   required from here
$GECODE_HOME/include/gecode/int/int-set-1.hpp:61:15: error: no match for call to ‘(std::vector<int>) ()’
   61 |       while (i()) {
      |              ~^~
$GECODE_HOME/include/gecode/int/int-set-1.hpp:62:22: error: ‘class std::vector<int>’ has no member named ‘min’
   62 |         d[n].min = i.min(); d[n].max = i.max(); size += i.width();
      |                    ~~^~~
$GECODE_HOME/include/gecode/int/int-set-1.hpp:62:42: error: ‘class std::vector<int>’ has no member named ‘max’
   62 |         d[n].min = i.min(); d[n].max = i.max(); size += i.width();
      |                                        ~~^~~
$GECODE_HOME/include/gecode/int/int-set-1.hpp:62:59: error: ‘class std::vector<int>’ has no member named ‘width’
   62 |         d[n].min = i.min(); d[n].max = i.max(); size += i.width();
      |                                                         ~~^~~~~
$GECODE_HOME/include/gecode/int/int-set-1.hpp:63:14: error: no match for ‘operator++’ (operand type is ‘std::vector<int>’)
   63 |         ++n; ++i;
      |              ^~~

Full code

#include <vector>

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

using namespace Gecode;

class Class : public Space {
public:
  std::vector<int> values = {2, 4, 6, 8};
  IntVar i;

  Class() : i(*this, IntSet(values)) {

  }

  // search support
  Class(Class& s) : Space(s) {
    i.update(*this, s.i);
  }

  virtual Space* copy(void) {
    return new Class(*this);
  }

  // print solution
  void print() {
    std::cout << i << "\n";
  }
};


int main(int argc, char* argv[]) {
    Class* c = new Class;
    DFS<Class> e(c);
    delete c;
    while (Class* s = e.next()) {
      s->print(); delete s;
    }

    return 0;
}

Command to compile

g++ -std=c++20 -O3 -I $GECODE_HOME/include -L$GECODE_HOME/lib -lgecodesearch -lgecodeminimodel -lgecodeint -lgecodekernel -lgecodesupport bla.cc

What am I missing here? Do I misunderstand something?

Upvotes: 1

Views: 54

Answers (1)

3CxEZiVlQ
3CxEZiVlQ

Reputation: 38508

The vector is bound to the constructor template <typename I> IntSet::IntSet(I&), you seem to expect the specialized constructor template <> IntSet::IntSet(const std::vector<int>& r). Make the vector const: const std::vector<int> values = {2, 4, 6, 8};.

See the minimized example for your case: https://godbolt.org/z/Gd8Enjfr4

See the example with the const vector: https://godbolt.org/z/dTshKrPPb

Upvotes: 2

Related Questions