00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef _ROBOT_CONTROL_KINEMATICS_BETAFORMCONSTRAINTS_
00026 #define _ROBOT_CONTROL_KINEMATICS_BETAFORMCONSTRAINTS_
00027
00028 #include <robot/control/kinematics/kinematics>
00029
00030 #include <sstream>
00031
00032 #include <base/Named>
00033 #include <robot/control/kinematics/Optimizer>
00034
00035
00036 namespace robot {
00037 namespace control {
00038 namespace kinematics {
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062 class BetaFormConstraints : public Optimizer::Constraints
00063 {
00064 public:
00065
00066 class BetaFormConstraint : public base::ReferencedObject, public base::Named
00067 {
00068 public:
00069 BetaFormConstraint() : Named(className()), id(0) {}
00070 BetaFormConstraint(const BetaFormConstraint& bc) : Named(bc), id(bc.id), beta(bc.beta) {}
00071 BetaFormConstraint(const Vector& beta) : id(0), beta(beta) { setName(className()); }
00072
00073 virtual String className() const { return String("BetaFormConstraint"); }
00074
00075 virtual Int getID() const { return id; }
00076 virtual void setID(Int id) { this->id = id; }
00077
00078 virtual Vector getBeta() const { return beta; }
00079
00080 virtual String toString() const { return String(); }
00081
00082 protected:
00083 Int id;
00084 Vector beta;
00085 };
00086
00087
00088 BetaFormConstraints()
00089 : numBetas(0), nonholonomicConstraint(false) {}
00090
00091 virtual String className() const { return String("BetaFormConstraints"); }
00092
00093 virtual ConstraintsType getConstraintsType() const { return Linear; }
00094 virtual ConstraintType getConstraintType(Int constraint) const { return Equality; }
00095 virtual Int numConstraints() const { return numBetas+(nonholonomicConstraint?1:0); }
00096 virtual Int numEqualityConstraints() const { return numConstraints(); }
00097 virtual Int numInequalityConstraints() const { return 0; }
00098
00099 virtual Real evaluate(Int constraint, const Vector& x, const Vector& a) const
00100 {
00101 Assert( constraint < numConstraints() );
00102 const Vector& t(a);
00103 if (constraint < numBetas) {
00104 Assert(t.size() == betas.size1());
00105 return (inner_prod(matrixColumn(betas,constraint),t) - 1.0);
00106 }
00107 else {
00108 Assert(t.size() == alpha.size());
00109 return inner_prod(alpha, t);
00110 }
00111 }
00112
00113 virtual Vector evaluate(const Vector& x, const Vector& a) const
00114 {
00115 const Vector& t(a);
00116 Vector C(numConstraints());
00117 for(Int c=0; c<numBetas; c++)
00118 C[c] = (inner_prod(matrixColumn(betas,c),t) - 1.0);
00119 if (nonholonomicConstraint)
00120 C[numConstraints()-1] = inner_prod(alpha, t);
00121 return C;
00122 }
00123
00124
00125 void addConstraint(ref<BetaFormConstraint> constraint)
00126 {
00127 constraints.push_back(constraint);
00128
00129 Vector beta(constraint->getBeta());
00130
00131
00132 if (numBetas == 0) {
00133 betas.resize(beta.size(),16);
00134 }
00135 else {
00136 if (numBetas == betas.size2()) {
00137
00138 Matrix newbetas(betas.size1(), betas.size2()*2);
00139 matrixRange(newbetas,Range(0,betas.size1()),Range(0,numBetas)) = betas;
00140 betas = newbetas;
00141 }
00142 }
00143
00144 Assert( beta.size() == betas.size1() );
00145 matrixColumn(betas,numBetas) = beta;
00146 ++numBetas;
00147 }
00148
00149 void setAlphaConstraint(const Vector& alpha)
00150 {
00151 nonholonomicConstraint = true;
00152 this->alpha.reset(alpha);
00153 }
00154
00155 bool isAlphaConstraint() const { return nonholonomicConstraint; }
00156
00157
00158 void clear() { betas.resize(0,0); numBetas=0; constraints.clear(); nonholonomicConstraint = false; }
00159 ref<BetaFormConstraint> getConstraint(Int i) { return constraints[i]; }
00160 Vector getBeta(Int i) const { return matrixColumn(betas,i); }
00161
00162 Vector getAlpha() const { return nonholonomicConstraint?alpha:Vector(); }
00163
00164
00165
00166 Matrix getBetas() const
00167 {
00168 Matrix allbetas(betas.size1(), numBetas);
00169 allbetas = matrixRange(betas,Range(0,betas.size1()), Range(0,numBetas));
00170 return allbetas;
00171 }
00172
00173 Int numBetaConstraints() const { return numBetas; }
00174
00175
00176 String toString() const
00177 {
00178 std::ostringstream str;
00179 if (nonholonomicConstraint)
00180 str << "non-holonomic alpha=" << alpha << "\n";
00181 for(Int i=0; i<numBetaConstraints(); i++) {
00182 ref<BetaFormConstraint> bfc(constraints[i]);
00183 str << bfc->getName() << "[id:" << bfc->getID() << "]: " << bfc->toString() << "\n";
00184 }
00185 return str.str();
00186 }
00187
00188 protected:
00189 typedef array< ref<BetaFormConstraint> > ConstraintArray;
00190
00191 Int numBetas;
00192 ConstraintArray constraints;
00193 Matrix betas;
00194 bool nonholonomicConstraint;
00195 Vector alpha;
00196 };
00197
00198
00199 inline std::ostream& operator<<(std::ostream& out, const BetaFormConstraints::BetaFormConstraint& bc)
00200 { out << bc.toString(); return out; }
00201
00202 inline std::ostream& operator<<(std::ostream& out, const BetaFormConstraints& bcs)
00203 { out << bcs.toString(); return out; }
00204
00205
00206
00207 }
00208 }
00209 }
00210
00211 #endif
00212