📄 model.hh
字号:
Model<Type> m1(m); m1 += v; return m1;} template<class Type>Model<Type> operator-(const Model<Type>& m, const Vector<Type>& v){ Model<Type> m1(m); m1 -= v; return m1;} template<class Type>Model<Type> operator-(const Vector<Type>& v, const Model<Type>& m){ Model<Type> m1(m); m1 -= v; return m1;} template<class Type>Model<Type> operator+(const Model<Type>& m, const Model<Type>& m1){ Model<Type> m2(m); m2 += m1; return m2;} template<class Type>Model<Type> operator-(const Model<Type>& m, const Model<Type>& m1){ Model<Type> m2(m); m2 -= m1; return m2;} template<class Type>Model<Type> operator/(const Model<Type>& m, Type c){ Model<Type> m1(m); m1 /= c; return m1;} template<class Type>Model<Type> operator*(const Model<Type>& m, Type c){ Model<Type> m1(m); m1 *= c; return m1;} template<class Type>Model<Type> operator*(Type c, const Model<Type>& m){ Model<Type> m1(m); m1 *= c; return m1;} template<class Type>Model<Type>& Model<Type>::operator=(const Model<Type>& m) { ndim = m.ndim; mp[0] = m.mp[0]; deltmod = m.deltmod; if( m.maxParam == NULL) maxParam = NULL; else { if (maxParam == NULL) maxParam = new Vector<Type>(ndim); maxParam[0] = (m.maxParam)[0]; } if( m.minParam == NULL) minParam = NULL; else { if (minParam == NULL) minParam = new Vector<Type>(ndim); minParam[0] = (m.minParam)[0]; } tp = m.tp; return *this;} template <class Type>Model<Type>::Model(const Vector<Type>& maxp, const Vector<Type>& minp, const Vector<Type>& initmod, Type deltmod){ ndim = initmod.size(); deltmod = (Type) deltmod; mp = new Vector<Type>(ndim); mp[0] = initmod; maxParam = new Vector<Type>(ndim); minParam = new Vector<Type>(ndim); maxParam[0] = maxp; minParam[0] = minp; tp = NULL;}template <class Type>Model<Type>::Model(const Vector<Type>& maxp, const Vector<Type>& minp, int (*cstraints)(Vector<Type>&)){ ndim = maxp.size(); deltmod = (Type)0; mp = new Vector<Type>(ndim); maxParam = new Vector<Type>(ndim); minParam = new Vector<Type>(ndim); maxParam[0] = maxp; minParam[0] = minp; tp = cstraints;}template <class Type>Model<Type>::Model(const Vector<Type>& maxp, const Vector<Type>& minp, const Vector<Type>& initmod){ ndim = initmod.size(); deltmod = (Type)0; mp = new Vector<Type>(ndim); mp[0] = initmod; maxParam = new Vector<Type>(ndim); minParam = new Vector<Type>(ndim); maxParam[0] = maxp; minParam[0] = minp; tp = NULL;} template <class Type>Model<Type>::Model(const Vector<Type>& maxp, const Vector<Type>& minp){ ndim = Min(maxp.size(),minp.size()); deltmod = (Type)0; mp = new Vector<Type>(ndim); maxParam = new Vector<Type>(ndim); minParam = new Vector<Type>(ndim); maxParam[0] = maxp; minParam[0] = minp; tp = NULL;}template <class Type>Model<Type>::Model(int n, int (*cstraints)(Vector<Type> &)){ ndim = n; deltmod = (Type)0; mp = new Vector<Type>(ndim); maxParam = NULL; minParam = NULL; tp = cstraints;} template <class Type>Model<Type>::Model(int n){ ndim = n; deltmod = (Type)0; mp = new Vector<Type>(ndim); maxParam = NULL; minParam = NULL; tp = NULL;} template <class Type>Model<Type>::Model(){ ndim = 1; deltmod = (Type)0; mp = new Vector<Type>(ndim); maxParam = NULL; minParam = NULL; tp = NULL;} template <class Type>Model<Type>::Model(const Vector<Type>& initmod){ ndim = initmod.size(); deltmod = (Type)0; mp = new Vector<Type>(ndim); mp[0] = initmod; maxParam = NULL; minParam = NULL; tp = NULL;} template <class Type>Model<Type>::Model(const Model<Type>& initmod){ ndim = initmod.ndim; deltmod = initmod.deltmod; mp = new Vector<Type>(ndim); mp[0] = initmod.mp[0]; if (initmod.maxParam != NULL) { maxParam = new Vector<Type>(ndim); maxParam[0] = (initmod.maxParam)[0]; } else maxParam = NULL; if (initmod.minParam != NULL) { minParam = new Vector<Type>(ndim); minParam[0] = (initmod.minParam)[0]; } else minParam = NULL; tp = initmod.tp;} template<class Type>Model<Type>::~Model(){ if (maxParam != NULL) delete maxParam; if (minParam != NULL) delete minParam; delete mp;} template<class Type> //change the way to handle boundary condition 11/17/95Model<Type> Model<Type>::update(Type alpha, Type beta, const Vector<Type>& dmod) const{ Model<Type> newmodel(*this); Vector<Type> direction(dmod); if (direction.size() != ndim) { cerr << "WARNING: the length of the direction is different from dimensions." << endl; direction.chaSize(ndim); } // check to see if the new model is out of the upper bound Type diff=beta; int inout, icount=0; newmodel.mp[0] = alpha * mp[0] + diff * direction; if (tp != NULL) { inout = (*tp)(newmodel.mp[0]); if (inout != 1) { diff += inout*diff/2; icount ++; newmodel.mp[0] = alpha * mp[0] + diff * direction; inout = (*tp)(newmodel.mp[0]); while (inout != 1 || icount <= 5) { if (icount > 10) { newmodel.mp[0] = mp[0]; return newmodel; } diff += inout*diff/2; icount ++; newmodel.mp[0] = alpha*mp[0] + diff*direction; inout = (*tp)(newmodel.mp[0]); } } } if ((maxParam != NULL) || (minParam != NULL)) { for (int i=0; i<ndim; i++){ if ((diff = newmodel.mp[0][i]-maxParam[0][i])>0) newmodel.mp[0][i] = maxParam[0][i]; if ((diff = newmodel.mp[0][i]-minParam[0][i])<0) newmodel.mp[0][i] = minParam[0][i]; } } return newmodel; } template<class Type>Type Model<Type>::oneMax(int i) const { if (maxParam != NULL) return (*maxParam)[i]; else { cerr << "No boundary specified!"<< endl; return -999; }} template<class Type>Type Model<Type>::oneMin(int i) const { if (minParam != NULL) return (*minParam)[i]; else { cerr << "No boundary specified!"<< endl; return -999; }} template<class Type>Type Model<Type>::range(int i) { if (minParam != NULL && maxParam != NULL) return (maxParam[0][i]-minParam[0][i]); else { cerr << "No boundary specified!"<< endl; return -999; }}template<class Type>ostream& operator <<(ostream& os, const Model<Type>& d){ os << d.mp[0] << endl; return os;}template<class Type>istream& operator >>(istream& is, Model<Type>& d){ is >> d.mp[0]; return is;}template<class Type>int operator ==(const Model<Type>& m1, const Model<Type>& m2){ if (m1.mp[0] != m2.mp[0]) return 0; if (m1.maxParam != NULL && m1.maxParam[0] != m2.maxParam[0]) return 0; if (m1.minParam != NULL && m1.minParam[0] != m2.minParam[0]) return 0; if (m1.tp != NULL && m2.tp != NULL && m1.tp[0] != m2.tp[0]) return 0; if (m1.deltmod != m2.deltm) return 0; return 1;}template<class Type>int operator !=(const Model<Type>& m1, const Model<Type>& m2){ if (m1.mp[0] != m2.mp[0]) return 1; if (m1.maxParam != NULL && m1.maxParam[0] != m2.maxParam[0]) return 1; if (m1.minParam != NULL && m1.minParam[0] != m2.minParam[0]) return 1; if (m1.tp != NULL && m2.tp != NULL && m1.tp[0] != m2.tp[0]) return 1; if (m1.deltm != m2.delm) return 1; return 0;}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -