Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added modintvar and modsearch #2

Open
wants to merge 2 commits into
base: task/cstintvar
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions vm/main/coreatoms-decl.hh
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ struct CoreAtoms {
atom_t spaceNoChoice;
atom_t spaceAltRange;
atom_t spaceMerged;
atom_t spaceFailed;
atom_t indexOutOfBounds;

// Distribuitor
atom_t naive;
};

}
Expand Down
3 changes: 3 additions & 0 deletions vm/main/coreatoms.hh
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ void CoreAtoms::initialize(VM vm, AtomTable& atomTable) {
spaceNoChoice = atomTable.get(vm, MOZART_STR("spaceNoChoice"));
spaceAltRange = atomTable.get(vm, MOZART_STR("spaceAltRange"));
spaceMerged = atomTable.get(vm, MOZART_STR("spaceMerged"));
spaceFailed = atomTable.get(vm, MOZART_STR("spaceFailed"));
indexOutOfBounds = atomTable.get(vm, MOZART_STR("indexOutOfBounds"));

naive = atomTable.get(vm, MOZART_STR("naive"));
}

}
Expand Down
4 changes: 4 additions & 0 deletions vm/main/coreinterfaces-decl.hh
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,10 @@ struct Interface<SpaceLike>:
return false;
}

Space* space(RichNode self, VM vm) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this operation should be part of a new interface only implemented by reified space. In that way the default behavior is only in the interface and only reified space has a proper impl.

raiseTypeError(vm, MOZART_STR("Space"), self);
}

UnstableNode askSpace(RichNode self, VM vm) {
raiseTypeError(vm, MOZART_STR("Space"), self);
}
Expand Down
2 changes: 2 additions & 0 deletions vm/main/coremodules.hh
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,7 @@
#include "modules/modproperty.hh"
#include "modules/modforeignpointer.hh"
#include "modules/modreflection.hh"
#include "modules/modintvar.hh"
#include "modules/modsearch.hh"

#endif // __COREMODULES_H
103 changes: 103 additions & 0 deletions vm/main/modules/modintvar.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// Copyright © 2011, Université catholique de Louvain
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

#ifndef __MODINT_H
#define __MODINT_H

#include "../mozartcore.hh"

#ifndef MOZART_GENERATOR

namespace mozart {

namespace builtins {

////////////////
// Int module //
////////////////

class ModIntVar: public Module {
public:
ModIntVar(): Module("IntVar") {}

class New: public Builtin<New> {
public:
New(): Builtin("new") {}

void operator()(VM vm, In min, In max, Out result) {
result = CstIntVar::build(vm,min,max);
}
};

class Is: public Builtin<Is> {
public:
Is(): Builtin("is") {}

void operator()(VM vm, In var, Out result) {
result = build(vm, IntVarLike(var).isIntVarLike(vm));
}
};

class Min: public Builtin<Min> {
public:
Min(): Builtin("min") {}

void operator()(VM vm, In var, Out result) {
result = build(vm, IntVarLike(var).min(vm));
}
};

class Max: public Builtin<Max> {
public:
Max(): Builtin("max") {}

void operator()(VM vm, In var, Out result) {
result = build(vm, IntVarLike(var).max(vm));
}
};

class Value: public Builtin<Value> {
public:
Value(): Builtin("value") {}

void operator()(VM vm, In var, Out result) {
result = build(vm, IntVarLike(var).value(vm));
}
};

class IsIn: public Builtin<IsIn> {
public:
IsIn(): Builtin("isIn") {}

void operator()(VM vm, In var, In n, Out result) {
result = build(vm, IntVarLike(var).isIn(vm,n));
}
}
};
}
}

#endif // MOZART_GENERATOR

#endif // __MODINT_H
62 changes: 62 additions & 0 deletions vm/main/modules/modsearch.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#ifndef __MODSEARCH_H
#define __MODSEARCH_H

#include "../mozartcore.hh"
#include <gecode/search.hh>

namespace mozart {
namespace builtins {

///////////////////
// Search Module //
///////////////////

class ModSearch: public Module {
public:
ModSearch(): Module("Search") {}

class Distribute: public Builtin<Distribute> {
public:
Distribute(): Builtin("distribute") {}

void operator()(VM vm, In b, In x) {
assert(vm->getCurrentSpace()->hasConstraintSpace());
atom_t a = getArgument<atom_t>(vm,b,MOZART_STR("Atom"));
if (a == vm->coreatoms.naive){
GecodeSpace& home = vm->getCurrentSpace()->getCstSpace();
Gecode::IntVar& vx = IntVarLike(x).intVar(vm);
Gecode::branch(home, vx, Gecode::INT_VAL_MIN);
}
}

};

class DFS: public Builtin<DFS> {
public:
DFS(): Builtin("dfs") {}

void operator()(VM vm, In space, Out result) {
if(SpaceLike(space).isSpace(vm)) {
Space* s = SpaceLike(space).space(vm);
if(s->hasConstraintSpace()){
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to make this check inside the code of the interface and return a reference to the Gecode space directly?

GecodeSpace& gs = s->getCstSpace();
Gecode::DFS<GecodeSpace> e(&gs);
GecodeSpace *sol = e.next();
Space* cs = s->clone(vm);
cs->setCstSpace(*sol);
result = ReifiedSpace::build(vm, cs);
delete sol;
}
}

}

};

};

} // namespace builtins
} // namespace mozart
#endif // __MODSEARCH_H


10 changes: 10 additions & 0 deletions vm/main/reifiedspace-decl.hh
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ public:
return true;
}

Space* space(RichNode self, VM vm) {
return getSpace();
}

inline
UnstableNode askSpace(RichNode self, VM vm);

Expand Down Expand Up @@ -114,6 +118,9 @@ public:
return true;
}

inline
Space* space(RichNode self, VM vm);

inline
UnstableNode askSpace(VM vm);

Expand Down Expand Up @@ -164,6 +171,9 @@ public:
return true;
}

inline
Space* space(RichNode self, VM vm);

inline
UnstableNode askSpace(VM vm);

Expand Down
8 changes: 8 additions & 0 deletions vm/main/reifiedspace.hh
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,10 @@ void ReifiedSpace::killSpace(RichNode self, VM vm) {
void FailedSpace::create(unit_t& self, VM vm, GR gr, FailedSpace from) {
}

Space* FailedSpace::space(RichNode self, VM vm) {
raise(vm, vm->coreatoms.spaceFailed);
}

UnstableNode FailedSpace::askSpace(VM vm) {
return Atom::build(vm, vm->coreatoms.failed);
}
Expand Down Expand Up @@ -271,6 +275,10 @@ void FailedSpace::killSpace(VM vm) {
void MergedSpace::create(unit_t& self, VM vm, GR gr, MergedSpace from) {
}

Space* MergedSpace::space(RichNode self, VM vm) {
raise(vm, vm->coreatoms.spaceMerged);
}

UnstableNode MergedSpace::askSpace(VM vm) {
return Atom::build(vm, vm->coreatoms.merged);
}
Expand Down
4 changes: 4 additions & 0 deletions vm/main/space-decl.hh
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,10 @@ public:
bool hasConstraintSpace() {
return _cstSpace != nullptr;
}

void setCstSpace(GecodeSpace cstSpace) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain the rationale behind this operation?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is because in DFS builtin the output should be a Space, but the output of the search engine is a GecodeSpace, then i need create a new Space and assign the GecodeSpace obtained in the search engine.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

El domingo, 3 de febrero de 2013 a las 9:47, miavisa escribió:

In vm/main/space-decl.hh:

@@ -346,6 +346,10 @@ public: > bool hasConstraintSpace() { > return _cstSpace != nullptr; > } > + > + void setCstSpace(GecodeSpace cstSpace) {
This is because in DFS builtin the output should be a Space, but the output of the search engine is a GecodeSpace, then i need create a new Space and assign the GecodeSpace obtained in the search engine.

That is the work of Space.merge and the behavior should be encapsulated there. I would go for a constructor that takes a gecode space and do everything inside. Having such method is kind of problematic because it can be used for other purposes not part of the design.

Think about the following: the life time of any gecode space depends on a mozart space. By providing such a method you can alter that behavior and end up replacing gecode spaces which is something we want to prevent by design.

_cstSpace = &cstSpace;
}
#endif

// Fields
Expand Down
4 changes: 2 additions & 2 deletions vm/test/csttest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ TEST_F(CstTest, ConstraintVarInterface) {
EXPECT_TRUE(ConstraintVar(x).assigned(vm));
}

TEST_F(CstTest, CstIntVarIntVarLikeInterface) {
/*TEST_F(CstTest, CstIntVarIntVarLikeInterface) {
nativeint v(0);
UnstableNode x = CstIntVar::build(vm,v);

Expand All @@ -104,4 +104,4 @@ TEST_F(CstTest, ConstraintVarInterface) {
nativeint v(0);
UnstableNode x = CstIntVar::build(vm,v);
EXPECT_TRUE(ConstraintVar(x).assigned(vm));
}
}*/