Adaptagrams
constraint.h
1 /*
2  * vim: ts=4 sw=4 et tw=0 wm=0
3  *
4  * libvpsc - A solver for the problem of Variable Placement with
5  * Separation Constraints.
6  *
7  * Copyright (C) 2005-2008 Monash University
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  * See the file LICENSE.LGPL distributed with the library.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18  *
19  * Author(s): Tim Dwyer
20 */
21 
22 
23 #ifndef VPSC_CONSTRAINT_H
24 #define VPSC_CONSTRAINT_H
25 
26 // cmath needs ::strcpy_s under MinGW so include cstring.
27 #include <cstring>
28 
29 #include <cfloat>
30 #include <iostream>
31 #include <vector>
32 #include <sstream>
33 
34 #include "libvpsc/variable.h"
35 
36 namespace vpsc {
37 
38 class Variable;
39 typedef std::vector<Variable *> Variables;
40 
45 {
46  friend std::ostream& operator <<(std::ostream &os,const Constraint &c);
47 public:
60  bool equality = false);
61  ~Constraint();
62 
68  std::string toString(void) const
69  {
70  std::stringstream stream;
71  stream << "Constraint: var(" << left->id << ") ";
72  if (gap < 0)
73  {
74  stream << "- " << -gap << " ";
75  }
76  else
77  {
78  stream << "+ " << gap << " ";
79  }
80  stream << ((equality) ? "==" : "<=");
81  stream << " var(" << right->id << ") ";
82  return stream.str();
83  }
84 
85  inline double slack(void) const
86  {
87  if (unsatisfiable)
88  {
89  return DBL_MAX;
90  }
91  if (needsScaling)
92  {
93  return right->scale * right->position() - gap -
94  left->scale * left->position();
95  }
96  COLA_ASSERT(left->scale == 1);
97  COLA_ASSERT(right->scale == 1);
98  return right->unscaledPosition() - gap - left->unscaledPosition();
99  }
100 
106  double gap;
107  double lm;
108  long timeStamp;
109  bool active;
111  const bool equality;
115  bool needsScaling;
116  void *creator;
117 };
118 
119 class CompareConstraints {
120 public:
121  bool operator() (Constraint *const &l, Constraint *const &r) const;
122 };
123 
125 typedef std::vector<Constraint*> Constraints;
126 
135  const Variables& vars, const Constraints& constraints);
136 
137 }
138 
139 #endif // VPSC_CONSTRAINT_H
A variable is comprised of an ideal position, final position and a weight.
Definition: variable.h:44
Variable * right
The right Variable.
Definition: constraint.h:104
libvpsc: Variable Placement with Separation Constraints quadratic program solver library.
Definition: assertions.h:61
double gap
The minimum or exact distance to separate the variables by.
Definition: constraint.h:106
std::vector< Variable * > Variables
A vector of pointers to Variable objects.
Definition: constraint.h:38
std::string toString(void) const
Returns a textual description of the constraint.
Definition: constraint.h:68
std::vector< Constraint * > Constraints
A vector of pointers to Constraint objects.
Definition: constraint.h:125
Constraints constraintsRemovingRedundantEqualities(const Variables &vars, const Constraints &constraints)
Given a set of variables and constraints, returns a modified set of constraints with all redundant eq...
Definition: constraint.cpp:188
A constraint determines a minimum or exact spacing required between two Variable objects.
Definition: constraint.h:44
bool unsatisfiable
Denote whether this constraint was unsatisifable (once the VPSC instance has been solved or satisfied...
Definition: constraint.h:114
Constraint(Variable *left, Variable *right, double gap, bool equality=false)
Constructs a minimum or exact spacing constraint between two Variable objects.
Definition: constraint.cpp:35
Variable * left
The left Variable.
Definition: constraint.h:102
const bool equality
Whether the separation is an exact distance or not.
Definition: constraint.h:111