diff options
-rw-r--r-- | impl/Complete.hpp | 2 | ||||
-rw-r--r-- | impl/Expression.hpp | 25 | ||||
-rw-r--r-- | impl/Operator.hpp | 23 | ||||
-rw-r--r-- | impl/Variable.hpp | 4 | ||||
-rw-r--r-- | impl/main.cpp | 47 | ||||
-rw-r--r-- | impl/systems/basic-range | 2 |
6 files changed, 78 insertions, 25 deletions
diff --git a/impl/Complete.hpp b/impl/Complete.hpp index 1cf40af..64b850a 100644 --- a/impl/Complete.hpp +++ b/impl/Complete.hpp @@ -69,7 +69,7 @@ struct Complete { } } bool operator>(const Complete& other) const { - return !(*this < other || *this == other); + return other < *this; } bool operator==(const Complete& other) const { if (_infinity) { diff --git a/impl/Expression.hpp b/impl/Expression.hpp index d3dfcf3..3c84d30 100644 --- a/impl/Expression.hpp +++ b/impl/Expression.hpp @@ -24,6 +24,10 @@ struct Expression { virtual T operator() (const VariableAssignment<T>& assignment) const { return (*_operator)(_arguments, assignment); } + + template<typename Z> + friend std::ostream& operator<<(std::ostream&, const Expression<Z>&); + protected: Operator<T>* _operator; std::vector< Expression<T>* > _arguments; @@ -52,16 +56,33 @@ struct MaxExpression : public Expression<T> { std::pair<T, unsigned int> best = std::pair<T, unsigned int>(-infinity<T>(), 0); for (unsigned int i = 0, size = Expression<T>::_arguments.size(); i < size; ++i) { T value = strat(*Expression<T>::_arguments[i], rho); - if (value > best.first) + if (best.first < value) best = std::pair<T, unsigned int>(value, i); } - std::cerr << "Best strategy: (" << best.first << ", " << best.second << ")" << std::endl << std::endl; + std::cerr << "Best strategy: (" << best.first << ", " << best.second << ")" << std::endl; return best; } private: unsigned int _id; }; +template<typename T> +std::ostream& operator<<(std::ostream& cout, const Expression<T>& expr) { + if (expr._arguments.size() == 0) { + cout << expr._operator->op_name; + } else { + cout << expr._operator->op_name << "("; + for (typename std::vector<Expression<T>*>::const_iterator it = expr._arguments.begin(); + it != expr._arguments.end(); + ++it) { + cout << (it == expr._arguments.begin() ? "" : ", "); + cout << **it; + } + cout << ")"; + } + return cout; +} + #include "Variable.hpp" #include "MaxStrategy.hpp" diff --git a/impl/Operator.hpp b/impl/Operator.hpp index 5f7cd1e..87e4eea 100644 --- a/impl/Operator.hpp +++ b/impl/Operator.hpp @@ -21,18 +21,21 @@ struct Expression; template<typename T> struct Operator { + Operator(const std::string& name) : op_name(name) { } virtual ~Operator() { } virtual T operator() (const std::vector< Expression<T>* >& args, const VariableAssignment<T>& rho) const = 0; + const std::string op_name; }; template<typename T> struct Maximum : public Operator<T> { + Maximum() : Operator<T>("max") { } virtual T operator() (const std::vector< Expression<T>* >& args, const VariableAssignment<T>& assignment) const { T value = -infinity<T>(); for (typename std::vector< Expression<T>* >::const_iterator it = args.begin(); it != args.end(); ++it) { T temporary = (**it)(assignment); - value = (temporary > value ? temporary : value); + value = (value < temporary ? value : temporary); //if (value == infinity<T>()) break; } return value; @@ -40,6 +43,7 @@ struct Maximum : public Operator<T> { }; template<typename T> struct Minimum : public Operator<T> { + Minimum() : Operator<T>("min") { } virtual T operator() (const std::vector< Expression<T>* >& args, const VariableAssignment<T>& assignment) const { T value = infinity<T>(); for (typename std::vector< Expression<T>* >::const_iterator it = args.begin(); @@ -55,8 +59,8 @@ struct Minimum : public Operator<T> { template<typename T> struct Constant : public Operator<T> { - Constant(const T& val) - : _value(val) { } + Constant(const std::string& value, const T& val) + : Operator<T>(value), _value(val) { } T operator() (const std::vector< Expression<T>* >& args, const VariableAssignment<T>& ass) const { return _value; } @@ -66,6 +70,7 @@ struct Constant : public Operator<T> { template<typename T> struct Addition: public Operator<T> { + Addition() : Operator<T>("add") { } T operator() (const std::vector< Expression<T>* >& args, const VariableAssignment<T>& ass) const { T sum = (*args[0])(ass); for (unsigned int i = 1, size = args.size(); i < size; ++i) { @@ -76,18 +81,17 @@ struct Addition: public Operator<T> { }; template<typename T> -struct Subtraction: public Operator<T> { +struct Negate: public Operator<T> { + Negate() : Operator<T>("neg") { } T operator() (const std::vector< Expression<T>* >& args, const VariableAssignment<T>& ass) const { - T sum = (*args[0])(ass); - for (unsigned int i = 1, size = args.size(); i < size; ++i) { - sum -= (*args[i])(ass); - } - return sum; + // assert(args.size() == 1); + return -(*args[0])(ass); } }; template<typename T> struct Comma: public Operator<T> { + Comma() : Operator<T>("comma") { } T operator() (const std::vector< Expression<T>* >& args, const VariableAssignment<T>& ass) const { if ((*args[0])(ass) == -infinity<T>()) { std::cout << "Comma - neg inf" << std::endl; @@ -101,6 +105,7 @@ struct Comma: public Operator<T> { template<typename T> struct Guard: public Operator<T> { + Guard() : Operator<T>("guard") { } T operator() (const std::vector< Expression<T>* >& args, const VariableAssignment<T>& ass) const { if ((*args[0])(ass) < (*args[1])(ass)) { return -infinity<T>(); diff --git a/impl/Variable.hpp b/impl/Variable.hpp index a174ea8..35a3991 100644 --- a/impl/Variable.hpp +++ b/impl/Variable.hpp @@ -7,9 +7,9 @@ template<typename T> struct Variable : public Operator<T> { Variable(unsigned int id, const std::string& name) - : _id(id), _name(name) { } + : Operator<T>(name), _id(id), _name(name) { } Variable(const Variable& other) - : _id(other._id) { } + : Operator<T>(name), _id(other._id) { } unsigned int id() const { return _id; } diff --git a/impl/main.cpp b/impl/main.cpp index 21be64b..2006565 100644 --- a/impl/main.cpp +++ b/impl/main.cpp @@ -26,9 +26,21 @@ std::pair<Expression<T>*, Expression<T>*> treeToExpression(pANTLR3_BASE_TREE nod // leaf node - variable if (num == 0) { - return std::pair<Expression<T>*, Expression<T>*>( - system.newExpression(system.newVariable(name + "_l")), - system.newExpression(system.newVariable(name + "_u"))); + if (negative) { + vector<Expression<T>*> firstArgs; + firstArgs.push_back(system.newExpression(system.newVariable(name + "_u"))); + vector<Expression<T>*> secondArgs; + secondArgs.push_back(system.newExpression(system.newVariable(name + "_l"))); + return std::pair<Expression<T>*, Expression<T>*>( + system.newExpression(new Negate<T>(), + firstArgs), + system.newExpression(new Negate<T>(), + secondArgs)); + } else { + return std::pair<Expression<T>*, Expression<T>*>( + system.newExpression(system.newVariable(name + "_l")), + system.newExpression(system.newVariable(name + "_u"))); + } } // a range itself @@ -43,7 +55,7 @@ std::pair<Expression<T>*, Expression<T>*> treeToExpression(pANTLR3_BASE_TREE nod if (!(firstStream >> firstValue)) { throw "Invalid number."; } - if (negative) firstValue = -firstValue; + string firstChildName = childName; childNode = (pANTLR3_BASE_TREE) node->getChild(node, 1); childName = (char*) childNode->getText(childNode)->chars; @@ -51,11 +63,22 @@ std::pair<Expression<T>*, Expression<T>*> treeToExpression(pANTLR3_BASE_TREE nod if (!(secondStream >> secondValue)) { throw "Invalid number."; } - if (negative) secondValue = -secondValue; + string secondChildName = childName; + + if (negative) { + T temp = firstValue; + string tempname = firstChildName; + + firstValue = -secondValue; + firstChildName = "-" + secondChildName; + + secondValue = -temp; + secondChildName = "-" + tempname; + } return std::pair<Expression<T>*, Expression<T>*>( - system.newExpression(new Constant<T>(firstValue)), - system.newExpression(new Constant<T>(secondValue))); + system.newExpression(new Constant<T>(firstChildName, firstValue)), + system.newExpression(new Constant<T>(secondChildName, secondValue))); } if (name == "-") { @@ -77,8 +100,8 @@ std::pair<Expression<T>*, Expression<T>*> treeToExpression(pANTLR3_BASE_TREE nod if (name == "-") { //if (firstArgs.size() == 1) { // secondArgs.size() == firstArgs.size() return std::pair<Expression<T>*, Expression<T>*>( - secondArgs[0], - firstArgs[0]); + firstArgs[0], + secondArgs[0]); } if (name == "max") { @@ -132,15 +155,17 @@ void treeToSystem(pANTLR3_BASE_TREE node, EquationSystem<T>& system) { std::pair<Expression<T>*, Expression<T>*> exprs = treeToExpression(exprNode, system); var = system.newVariable(varName + "_l"); - args.push_back(system.newExpression(new Constant<T>(-infinity<T>()))); + args.push_back(system.newExpression(new Constant<T>("-inf", -infinity<T>()))); args.push_back(exprs.first); system[*var] = system.newMaxExpression(args); + std::cout << var->op_name << " = " << *system[*var] << std::endl; args.clear(); var = system.newVariable(varName + "_u"); - args.push_back(system.newExpression(new Constant<T>(-infinity<T>()))); + args.push_back(system.newExpression(new Constant<T>("-inf", -infinity<T>()))); args.push_back(exprs.second); system[*var] = system.newMaxExpression(args); + std::cout << var->op_name << " = " << *system[*var] << std::endl; } } diff --git a/impl/systems/basic-range b/impl/systems/basic-range new file mode 100644 index 0000000..c6bf8fa --- /dev/null +++ b/impl/systems/basic-range @@ -0,0 +1,2 @@ +x = [0, 1] +y = -[0, 1] |