this repo has no description
1/**** , [ QCOPPlus.hh ], 2 Copyright (c) 2009 Universite d'Orleans - Jeremie Vautard 3 4 Permission is hereby granted, free of charge, to any person obtaining a copy 5 of this software and associated documentation files (the "Software"), to deal 6 in the Software without restriction, including without limitation the rights 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 copies of the Software, and to permit persons to whom the Software is 9 furnished to do so, subject to the following conditions: 10 11 The above copyright notice and this permission notice shall be included in 12 all copies or substantial portions of the Software. 13 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 THE SOFTWARE. 21 *************************************************************************/ 22 23#ifndef __QECODE_QCOP__ 24#define __QECODE_QCOP__ 25 26#include "myspace.hh" 27#include "vartype.hh" 28#include "OptVar.hh" 29#include <vector> 30 31 32using namespace std; 33using namespace Gecode; 34class QECODE_VTABLE_EXPORT Qcop { 35private: 36 struct Opts { 37 vector<OptVar*> vars; // Several Optvars in universal scopes, one in existential ones 38 int opt_type; // Used in existential scopes : 0 for ANY, 1 for MIN, 2 for MAX. 39 }; 40 int* nvar; 41 int n; 42 int nbSpaces; 43// void** v; 44 VarType* type_of_v; 45 46 bool* Quantifiers; 47 MySpace** rules; 48 MySpace* goal; 49 Opts* optim; 50 int* nbVarBySpace; 51 int* whichSpaceOwns; 52 bool* varInitialised; 53 54 int currentDeclareSpace; 55 Qcop() {} 56public: 57 forceinline QECODE_EXPORT int nv() {return n;} 58 59 /** \brief Default constructor for a restricted-quantified space 60 * 61 * This is the first step for building a QCSP+/QCOP+ problem. The prefix is defined here. 62 * @param ns The number of scopes in the prefix. 63 * @param quant Array of booleans which contains the quantifier of every scope (true for universal, false for existential). 64 * @param nv Array of integer which contains the number of variables by scope. 65 */ 66 QECODE_EXPORT Qcop(int ns, bool* quant,int* nv); 67 QECODE_EXPORT ~Qcop(); 68 69 /** \brief Defines an integer variable in the quantified space 70 * 71 * Defines an integer variable in the quantifies space using a fully declared domain. 72 * @param var Number of the variable to be defined. 73 * @param dom The initial domain of the variable. 74 */ 75 QECODE_EXPORT void QIntVar(int var,int min,int max); 76 77 /** \brief Defines an integer variable in the quantified space 78 * 79 * Defines an integer variable in the quantifies space using a fully declared domain. 80 * @param var Number of the variable to be defined. 81 * @param dom The initial domain of the variable. 82 */ 83 QECODE_EXPORT void QIntVar(int var,IntSet dom); 84 85 /** \brief Defines a boolean variable in the quantified space 86 * 87 * Defines a boolean variable with a full initial domain in the quantified space. 88 * @param var Number of the variable to be defined. 89 */ 90 QECODE_EXPORT void QBoolVar(int var); 91 92 /** \brief Returns the current declarating space 93 * 94 * Return the space we are currently declarating constraints in. nextScope() is used to set the nextspace as the current one. 95 */ 96 QECODE_EXPORT MySpace* space(); 97 98 /** \brief Returns an integer variable from the space we are currently declaring 99 * 100 * Returns an integer variable from the cpace we are currently declaring. Will abort if the variable is not integer. 101 * @param n The number of the variable to return. 102 */ 103 QECODE_EXPORT IntVar var(int n); 104 105 /** \brief Returns a boolean variable from the space we are currently declaring 106 * 107 * Returns a boolean variable from the space we are currently declaring. Will abort if the variable is not boolean. 108 * @param n The number of the variable to return. 109 */ 110 QECODE_EXPORT BoolVar bvar(int n); 111 112 /** \brief Switch to the next space for constraint declaration 113 * 114 * Switches to the next space for constraint declaration. var, bvar and space methods will now refer to the next rule space (or to the goal space if the current space was the last rule one). 115 * Returns the new space number, or -1 if it was called while there was no next space to declare constraints in. 116 */ 117 QECODE_EXPORT int nextScope(); 118 119 /** \brief Finalizes the object construction 120 * 121 * Finalizes the restricted-quantified space construction. Must be invoked once all the variable and all the constraints have been declared, and before the search is performed on this space. 122 * calling this method is not mandatory, although it is recommanded to besure that the problem have been well built. 123 * For the existential scopes on which an optimization condition have not been defined yet, this method will post a "Any" optimization condition (i.e. do not optimize). 124 */ 125 QECODE_EXPORT void makeStructure(); 126 127 /** \brief returns an aggregate of the problem 128 * 129 * Creates an aggregate at a given universal scope. This aggregate is an optimization variable that an existential scope can use for optimizing. 130 * @param scope the scope where this aggregate will be defined. Must be an unversal scope 131 * @param opt The optimization variable we want to aggregate. There must not be any universal scope between an agregate and thisoptimization variable. 132 * @param agg The aggregator function that will be used for this aggregate. 133 */ 134 QECODE_EXPORT OptVar* getAggregate(int scope, OptVar* opt, Aggregator* agg); 135 136 /** returns an existential optimization variable 137 * 138 * Creates an optimization variable that will have the value of an existential variable defined in the problem. 139 * @param var the number of the existential variable that must be considered 140 */ 141 QECODE_EXPORT OptVar* getExistential(int var); 142 143 /** set an optimization condition on an existential scope 144 * 145 * set an optimization condition on a given existential scope of the problem. An optimizaiton condition is composed of an optimization variable (aggregate or existential variable), and of an 146 * optimization type. 147 * @param scope the scope on which the optimization condition is posted. Must be existential. 148 * @param optType the optimization type of the condision we post. 0 is for ANY, 1 is for MIN, 2 is for MAX. 149 * @param var the optimization variable to be minimized/maximized 150 */ 151 QECODE_EXPORT void optimize(int scope, int optType, OptVar* var); 152 // QECODE_EXPORT void print(); 153 154 QECODE_EXPORT forceinline bool qt_of_var(int v) { return Quantifiers[whichSpaceOwns[v]];} ///< returns uantifier of variable 'v' 155 QECODE_EXPORT forceinline bool quantification(int scope) {return Quantifiers[scope];} ///< returns quantifier of scope 'scope' 156 QECODE_EXPORT int spaces(); ///< returns the number of scopes of the problem 157 QECODE_EXPORT forceinline int nbVarInScope(int scope) {return nbVarBySpace[scope];}///< returns the number of variables present in scope 'scope' 158 QECODE_EXPORT MySpace* getSpace(int scope);///< returns a copy of the Gecode::Space corresponding to the 'scope'-th restricted quantifier of the problem 159 QECODE_EXPORT MySpace* getGoal(); 160 QECODE_EXPORT int getOptType(int scope); ///< returns the optimization type of the 'scope'-th scope of the problem 161 QECODE_EXPORT OptVar* getOptVar(int scope);///< returns the optimization variable of the 'scope'-th scope of the problem 162 QECODE_EXPORT Qcop* clone(); ///< makes a copy of the quantified problem 163 QECODE_EXPORT forceinline int whoOwns(int var) {return whichSpaceOwns[var];} 164}; 165 166#endif