From 38735936f468aeaf0f06ce42f1d3eb3d63ef37d8 Mon Sep 17 00:00:00 2001 From: miavisa Date: Wed, 23 Jan 2013 17:47:28 -0500 Subject: [PATCH 1/2] Added modintvar and modsearch --- vm/main/coreatoms-decl.hh | 3 + vm/main/coreatoms.hh | 2 + vm/main/coreinterfaces-decl.hh | 4 ++ vm/main/coremodules.hh | 2 + vm/main/modules/modintvar.hh | 103 +++++++++++++++++++++++++++++++++ vm/main/modules/modsearch.hh | 59 +++++++++++++++++++ vm/main/reifiedspace-decl.hh | 12 ++++ vm/test/csttest.cc | 4 +- 8 files changed, 187 insertions(+), 2 deletions(-) create mode 100644 vm/main/modules/modintvar.hh create mode 100644 vm/main/modules/modsearch.hh diff --git a/vm/main/coreatoms-decl.hh b/vm/main/coreatoms-decl.hh index f406181..c8fdf00 100644 --- a/vm/main/coreatoms-decl.hh +++ b/vm/main/coreatoms-decl.hh @@ -86,6 +86,9 @@ struct CoreAtoms { atom_t spaceAltRange; atom_t spaceMerged; atom_t indexOutOfBounds; + + // Distribuitor + atom_t naive; }; } diff --git a/vm/main/coreatoms.hh b/vm/main/coreatoms.hh index a7fd220..3e32413 100644 --- a/vm/main/coreatoms.hh +++ b/vm/main/coreatoms.hh @@ -74,6 +74,8 @@ void CoreAtoms::initialize(VM vm, AtomTable& atomTable) { spaceAltRange = atomTable.get(vm, MOZART_STR("spaceAltRange")); spaceMerged = atomTable.get(vm, MOZART_STR("spaceMerged")); indexOutOfBounds = atomTable.get(vm, MOZART_STR("indexOutOfBounds")); + + naive = atomTable.get(vm, MOZART_STR("naive")); } } diff --git a/vm/main/coreinterfaces-decl.hh b/vm/main/coreinterfaces-decl.hh index 001f59b..e279570 100644 --- a/vm/main/coreinterfaces-decl.hh +++ b/vm/main/coreinterfaces-decl.hh @@ -527,6 +527,10 @@ struct Interface: return false; } + Space* space(RichNode self, VM vm) { + raiseTypeError(vm, MOZART_STR("Space"), self); + } + UnstableNode askSpace(RichNode self, VM vm) { raiseTypeError(vm, MOZART_STR("Space"), self); } diff --git a/vm/main/coremodules.hh b/vm/main/coremodules.hh index a90602f..f240c41 100644 --- a/vm/main/coremodules.hh +++ b/vm/main/coremodules.hh @@ -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 diff --git a/vm/main/modules/modintvar.hh b/vm/main/modules/modintvar.hh new file mode 100644 index 0000000..47dd608 --- /dev/null +++ b/vm/main/modules/modintvar.hh @@ -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 { + public: + New(): Builtin("new") {} + + void operator()(VM vm, In min, In max, Out result) { + result = CstIntVar::build(vm,min,max); + } + }; + + class Is: public Builtin { + public: + Is(): Builtin("is") {} + + void operator()(VM vm, In var, Out result) { + result = build(vm, IntVarLike(var).isIntVarLike(vm)); + } + }; + + class Min: public Builtin { + public: + Min(): Builtin("min") {} + + void operator()(VM vm, In var, Out result) { + result = build(vm, IntVarLike(var).min(vm)); + } + }; + + class Max: public Builtin { + public: + Max(): Builtin("max") {} + + void operator()(VM vm, In var, Out result) { + result = build(vm, IntVarLike(var).max(vm)); + } + }; + + class Value: public Builtin { + public: + Value(): Builtin("value") {} + + void operator()(VM vm, In var, Out result) { + result = build(vm, IntVarLike(var).value(vm)); + } + }; + + class IsIn: public Builtin { + 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 diff --git a/vm/main/modules/modsearch.hh b/vm/main/modules/modsearch.hh new file mode 100644 index 0000000..75826b3 --- /dev/null +++ b/vm/main/modules/modsearch.hh @@ -0,0 +1,59 @@ +#ifndef __MODSEARCH_H +#define __MODSEARCH_H + +#include "../mozartcore.hh" +#include + +namespace mozart { +namespace builtins { + +/////////////////// +// Search Module // +/////////////////// + +class ModSearch: public Module { +public: + ModSearch(): Module("Search") {} + + class Distribute: public Builtin { + public: + Distribute(): Builtin("distribute") {} + + void operator()(VM vm, In b, In x) { + assert(vm->getCurrentSpace()->hasConstraintSpace()); + atom_t a = getArgument(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 { + public: + DFS(): Builtin("dfs") {} + + void operator()(VM vm, In space) { + if(SpaceLike(space).isSpace(vm)) { + Space* s = SpaceLike(space).space(vm); + if(s->hasConstraintSpace()){ + GecodeSpace& gs = s->getCstSpace(); + Gecode::DFS e(&gs); + GecodeSpace *sol = e.next(); + delete sol; + } + } + + } + + }; + +}; + +} // namespace builtins +} // namespace mozart +#endif // __MODSEARCH_H + + diff --git a/vm/main/reifiedspace-decl.hh b/vm/main/reifiedspace-decl.hh index 7bdc0f3..6bb8700 100644 --- a/vm/main/reifiedspace-decl.hh +++ b/vm/main/reifiedspace-decl.hh @@ -62,6 +62,10 @@ public: return true; } + Space* space(RichNode self, VM vm) { + return getSpace(); + } + inline UnstableNode askSpace(RichNode self, VM vm); @@ -114,6 +118,10 @@ public: return true; } + Space* space(RichNode self, VM vm) { + return vm->getCurrentSpace(); + } + inline UnstableNode askSpace(VM vm); @@ -164,6 +172,10 @@ public: return true; } + Space* space(RichNode self, VM vm) { + return vm->getCurrentSpace(); + } + inline UnstableNode askSpace(VM vm); diff --git a/vm/test/csttest.cc b/vm/test/csttest.cc index 98ccf85..5f026bc 100644 --- a/vm/test/csttest.cc +++ b/vm/test/csttest.cc @@ -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); @@ -104,4 +104,4 @@ TEST_F(CstTest, ConstraintVarInterface) { nativeint v(0); UnstableNode x = CstIntVar::build(vm,v); EXPECT_TRUE(ConstraintVar(x).assigned(vm)); -} +}*/ From 370b10a196875a1a970b8ac90dabd18e4377e2dc Mon Sep 17 00:00:00 2001 From: miavisa Date: Fri, 1 Feb 2013 19:30:07 -0500 Subject: [PATCH 2/2] Solucionado implementacion de SpaceLike en MergedSpace y FailedSpace --- vm/main/coreatoms-decl.hh | 1 + vm/main/coreatoms.hh | 1 + vm/main/modules/modsearch.hh | 5 ++++- vm/main/reifiedspace-decl.hh | 10 ++++------ vm/main/reifiedspace.hh | 8 ++++++++ vm/main/space-decl.hh | 4 ++++ 6 files changed, 22 insertions(+), 7 deletions(-) diff --git a/vm/main/coreatoms-decl.hh b/vm/main/coreatoms-decl.hh index c8fdf00..2af9702 100644 --- a/vm/main/coreatoms-decl.hh +++ b/vm/main/coreatoms-decl.hh @@ -85,6 +85,7 @@ struct CoreAtoms { atom_t spaceNoChoice; atom_t spaceAltRange; atom_t spaceMerged; + atom_t spaceFailed; atom_t indexOutOfBounds; // Distribuitor diff --git a/vm/main/coreatoms.hh b/vm/main/coreatoms.hh index 3e32413..5642c9a 100644 --- a/vm/main/coreatoms.hh +++ b/vm/main/coreatoms.hh @@ -73,6 +73,7 @@ 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")); diff --git a/vm/main/modules/modsearch.hh b/vm/main/modules/modsearch.hh index 75826b3..c789f41 100644 --- a/vm/main/modules/modsearch.hh +++ b/vm/main/modules/modsearch.hh @@ -35,13 +35,16 @@ public: public: DFS(): Builtin("dfs") {} - void operator()(VM vm, In space) { + void operator()(VM vm, In space, Out result) { if(SpaceLike(space).isSpace(vm)) { Space* s = SpaceLike(space).space(vm); if(s->hasConstraintSpace()){ GecodeSpace& gs = s->getCstSpace(); Gecode::DFS e(&gs); GecodeSpace *sol = e.next(); + Space* cs = s->clone(vm); + cs->setCstSpace(*sol); + result = ReifiedSpace::build(vm, cs); delete sol; } } diff --git a/vm/main/reifiedspace-decl.hh b/vm/main/reifiedspace-decl.hh index 6bb8700..f90fdd4 100644 --- a/vm/main/reifiedspace-decl.hh +++ b/vm/main/reifiedspace-decl.hh @@ -118,9 +118,8 @@ public: return true; } - Space* space(RichNode self, VM vm) { - return vm->getCurrentSpace(); - } + inline + Space* space(RichNode self, VM vm); inline UnstableNode askSpace(VM vm); @@ -172,9 +171,8 @@ public: return true; } - Space* space(RichNode self, VM vm) { - return vm->getCurrentSpace(); - } + inline + Space* space(RichNode self, VM vm); inline UnstableNode askSpace(VM vm); diff --git a/vm/main/reifiedspace.hh b/vm/main/reifiedspace.hh index efda5f0..5b6c00b 100644 --- a/vm/main/reifiedspace.hh +++ b/vm/main/reifiedspace.hh @@ -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); } @@ -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); } diff --git a/vm/main/space-decl.hh b/vm/main/space-decl.hh index 301f3e2..bdb4488 100644 --- a/vm/main/space-decl.hh +++ b/vm/main/space-decl.hh @@ -346,6 +346,10 @@ public: bool hasConstraintSpace() { return _cstSpace != nullptr; } + + void setCstSpace(GecodeSpace cstSpace) { + _cstSpace = &cstSpace; + } #endif // Fields