Borderbasix

cubature.hpp
Go to the documentation of this file.
1 #ifndef BORDERBASIX_CUBATURE_HPP
2 #define BORDERBASIX_CUBATURE_HPP
3 #include <map>
4 #include <cstdlib>
5 #include <borderbasix/mdebug.hpp>
6 #include <borderbasix/mpoly.hpp>
10 #include <tensor-decomposition/Decomposer.hpp>
11 #define MMX_DEBUG_COUT
12 
13 #define TMPL template<class T>
14 
23 template<class T>
24 struct cubature {
25 
29 
30  cubature();
31 
33  void set_input(const Polynomial &p);
34 
37  void obj_def(int i) { m_init_p=i;}
38 
40  T& obj_mat(int i, int j) { return m_P[m_s*i+j]; }
41 
42  void run(int k, list<Polynomial> list2);
43 
44  void set_threshold(double eps) {m_eps_reconstr=eps;}
45 
47  void set_sdpsolver(solversdp* slv) {m_slvsdp=slv;}
48 
50  void set_degree (int deg) {m_degree=deg;}
51 
53  void set_numeq (int g) {m_numeq=g;}
54 
56  void set_numneq (int h) {m_numneq=h;}
57 
58  void set_verbose (int v) {m_verbose=v;}
59 
60 
61 private:
62 
63  void init_rnd_obj(unsigned s);
64  void init_idt_obj(unsigned s);
65 
66 
67  Polynomial m_input;
68  solversdp* m_slvsdp;
69  T* m_P;
70  unsigned m_s;
71  int m_init_p;
72  double m_eps_reconstr;
73  int m_verbose;
74 
75 
76 public:
77  std::vector<T> weight;
78  std::vector<T> point;
79  unsigned dim;
80  int m_degree;
81  int m_numeq;
82  int m_numneq;
83  T& P_ini(int i, int j);
84 
85 };
86 
87 TMPL
89  m_eps_reconstr(1e-5), m_verbose(0) {
90 }
91 
92 TMPL
93 void cubature<T>::init_rnd_obj(unsigned s) {
94  T* tmp=new T[s*s];
95  m_P=new T[s*s];
96  m_s=s;
97 
98  for(unsigned j=0;j<s*s;j++) {
99  m_P[j]=(T)0.;
100  tmp[j]=double(rand())/RAND_MAX;
101  //std::cout<<"tmp"<<tmp[j]<<std::endl;
102  }
103 
104 
105  for(unsigned k=0;k<s;k++)
106  for(unsigned l=0;l<s;l++)
107  for(unsigned n=0;n<s;n++)
108  m_P[k*s+l] += tmp[k*s+n]*tmp[l*s+n];
109 
110 }
111 
112 TMPL
113 void cubature<T>::init_idt_obj(unsigned s) {
114 
115  m_P=new T[s*s];
116  m_s=s;
117 
118  for(unsigned k=0;k<s;k++)
119  for(unsigned l=0;l<s;l++) {
120  if(k==l)
121  m_P[s*k+l]=(T)1.;
122  else
123  m_P[s*k+l]=(T)0.;
124  }
125 }
126 
127 
128 TMPL
130  m_input = p;
131 }
132 
133 TMPL
134 void cubature<T>::run(int k, list<Polynomial> list2) {
135 
136  std::map<Monomial,T> coeff;
137  std::map<Monomial,int> mat;
138 
139  Monomial m;
140 
141 
142  mdebug()<<"Initial moments:"<<m_input;
143  for(Polynomial::const_iterator it= m_input.begin();it!= m_input.end();it++) {
144  m =*it;
145  m.SetCoeff(1);
146  coeff[m]=it->GetCoeff();
147  }
148 
149 
150  int i = 0, j = 0, l = 0, i2,j2,contneq=0,l2=0,dim2i=0,dim2j=0;
151  int c = 0;
152  int dimbloq,u;
153  //int d = Degree(this->m_input);
154  this->dim = leading_var(this->m_input)+1;
155  //mdebug()<<"d"<<d<<"dim"<<this->dim;
156  mdebug()<<"nv:"<<this->dim;
157 
158  std::vector<Monomial> new_mon;
159 
160  Polynomial moments,mnq;
161 
162  Polynomial lm2,lm = matrix_of<Polynomial>::choose(this->dim,k);
163 
164 
165 
166  mdebug()<<"lm:"<<lm<<"lm.size()"<<lm.size();
167 
168  // Initialize matrix P with random coefficients and
169  // the objective function <X, P^t P, X> = trace(P X P^t).
170  if(this->m_init_p==0)
171  this->init_rnd_obj(lm.size());
172  else
173  this->init_idt_obj(lm.size());
174 
175 
176  std::map<Monomial,int>::const_iterator idx;
177 
178  mdebug()<<"after initialize matrix P";
179 
180  // Hankel matrix indexed by the monomials of lm
181  for(Polynomial::const_iterator it= lm.begin();it!= lm.end();it++) {
182  j=i;
183  for(Polynomial::const_iterator jt= it; jt!= lm.end();jt++) {
184  m=(*it)*(*jt);
185  //std::cout<<"mon"<<m<<std::endl;
186  // If degree <=d then coefficients in M0.
187  if(Degree(m)<=m_degree) {
188  m_slvsdp->coeff(0,i,j)=coeff[m];
189  } else {
190 
191  idx=mat.find(m);
192  if(idx!=mat.end()) {
193  l = idx->second+1;
194  } else {
195  mat[m]=c;
196  new_mon.push_back(m);
197  c++;
198  l=c;
199  }
200 
201  m_slvsdp->coeff(l,i,j)=(T)1;
202 
203  m_slvsdp->obj_coeff(l)+=obj_mat(i,j);
204 
205  if(i!=j) {
206  // // m_slvsdp->coeff(l,j,i)=(T)1;
207  m_slvsdp->obj_coeff(l)+=obj_mat(j,i);
208  }
209  }
210 
211  mdebug()<<"l"<<l<<",i:"<<i<<",j:"<<j<<",value"<<m_slvsdp->coeff(l,i,j);
212  j++;
213  }
214  i++;
215  }
216 
217  // Dimension of the Hankel matrix
218  m_slvsdp->dim()=lm.size();
219 
220 
221  //Localizing Matrices
222  dim2i=lm.size();
223  dim2j=lm.size();
224  if (m_numneq !=0)
225  for(list<Polynomial>::const_iterator itneq=list2.begin();itneq!=list2.end();itneq++)
226  {
227  contneq++;
228  std::cout<<k<<Degree(*itneq)<<std::endl;
229  dimbloq=(2*k-Degree(*itneq))/2;
230 
231  //dimbloq=u-floor(Degree(*itneq)/2);
232  std::cout<<"dimbloq"<<dimbloq<<std::endl;
233  lm2=matrix_of<Polynomial>::choose(this->dim,dimbloq);
234  //std::cout<<"lm2"<<lm2<<std::endl;
235  m_slvsdp->dim(contneq)=lm2.size();
236  i2=0;
237  j2=0;
238  for(Polynomial::const_iterator itneq2= lm2.begin();itneq2!= lm2.end();itneq2++) {
239  j2=i2;
240  for(Polynomial::const_iterator jtneq2= itneq2; jtneq2!= lm2.end();jtneq2++) {
241  mnq=(*itneq)*(*itneq2)*(*jtneq2);
242  for(Polynomial::const_iterator itmnq= mnq.begin(); itmnq!= mnq.end();itmnq++) {
243 
244  if(Degree(*itmnq)<=m_degree) {
245  m_slvsdp->coeff(0,contneq,i2,j2)+=(itmnq->GetCoeff())*coeff[*itmnq];
246  } else {
247 
248 
249  l2 = mat[*itmnq]+1;
250  m_slvsdp->coeff(l2,contneq,i2,j2)=itmnq->GetCoeff();
251 
252 
253  }
254 
255 
256  }
257  mdebug()<<"l"<<l2<<"bloque"<<contneq<<",i:"<<i2<<",j:"<<j2<<",value"<<m_slvsdp->coeff(l2,contneq,i2,j2);
258  j2++;
259  }
260  i2++;
261 
262  }
263  std::cout<<"dim2i"<<dim2i<<std::endl;
264  std::cout<<"lm2 size"<<lm2.size()<<std::endl;
265  dim2i+=lm2.size();
266  dim2j+=lm2.size();
267  }
268 
269 
270 
271  mdebug()<<"After hankel matrices construction";
272 
273  ;
274 
275  // Solve min <X,P^t P> s.t. X = M0 + y1 M1 + ...
276  m_slvsdp->run();
277 
278 
279  // Construct the extended moment sequence from the solution
280  // of the SDP problem.
281  moments=m_input;
282 
283 
284  for(unsigned i=0;i<m_slvsdp->obj_size();i++) {
285  new_mon[i].SetCoeff(m_slvsdp->output[i]);
286  moments+=new_mon[i];
287 
288  }
289 
290 
291  mdebug()<<"Extended moments:"<<moments;
292 
293  mmx::Decomposer<Polynomial> decomp(moments,this->m_eps_reconstr,this->m_verbose,-1,-1,this->m_s+1,0);
294  decomp.main_algo();
295 
296 
297  // Get the weights
298  weight.resize(decomp.lambdas.size());
299  for(unsigned i=0;i<decomp.lambdas.size();i++)
300  weight[i] = decomp.lambdasopt[i].real();
301 
302 
303  // Get the points
304 
305  point.resize(decomp.lambdasopt.size()*this->dim);
306  for(unsigned i=0;i<decomp.lambdasopt.size();i++) {
307  for(unsigned j=0;j<decomp.linear_forms.nbrow();j++)
308  point[i*this->dim+j]=decomp.linear_forms(j,i).real();
309 
310 
311  }
312 
313  m_slvsdp->clear();
314 
315 }
316 
317 #undef TMPL
318 #endif// BORDERBASIX_CUBATURE_HPP
The solversdp_csdp struct The solversdp interface class for csdp.
Definition: solversdp_csdp.h:9
T & P_ini(int i, int j)
int size() const
Definition: MPoly.hpp:107
Multivariate polynomials.
Definition: MPoly.hpp:28
iterator end()
Definition: MPoly.hpp:102
void set_numneq(int h)
Set the number of equations defining the integration domain.
Definition: cubature.hpp:56
Monom< double, dynamicexp<'x'> > Monomial
Definition: cubature.hpp:26
void set_sdpsolver(solversdp *slv)
Set the SDP solver to be used.
Definition: cubature.hpp:47
cubature()
Definition: cubature.hpp:88
MSKint32t MSKCONST MSKint32t * dim
Definition: mosek.h:2407
void run(int k, list< Polynomial > list2)
Definition: cubature.hpp:134
MSKconetypee MSKrealt MSKint32t MSKint32t j
Definition: mosek.h:2421
Class for computing cubature formula.
Definition: cubature.hpp:24
void set_input(const Polynomial &p)
Set the input sequence of moments.
Definition: cubature.hpp:129
static Polynomial choose(unsigned n, unsigned d)
Definition: matrix_of.hpp:10
void set_degree(int deg)
Set the minimal degree of the cubature.
Definition: cubature.hpp:50
Definition: mdebug.hpp:11
int m_numneq
Definition: cubature.hpp:82
MSKint32t k
Definition: mosek.h:2713
MSKint64t idx
Definition: mosek.h:4022
C coeff
Definition: Monom.hpp:29
int leading_var(const MPoly< R, O > &P)
Definition: MPoly.hpp:423
#define TMPL
Definition: cubature.hpp:13
void obj_def(int i)
Definition: cubature.hpp:37
dynamicexp< X, E >::degree_t Degree(const dynamicexp< X, E > &t)
Definition: dynamicexp.hpp:91
void set_numeq(int g)
Set the number of equations defining the integration domain.
Definition: cubature.hpp:53
void set_verbose(int v)
Definition: cubature.hpp:58
MSKrealt * c
Definition: mosek.h:2678
MPoly< std::vector< Monomial >, Ordering > Polynomial
Definition: cubature.hpp:28
MSKint32t MSKint32t MSKint32t i
Definition: mosek.h:2278
Dlex< Monomial > Ordering
Definition: cubature.hpp:27
std::vector< Monomial >::const_iterator const_iterator
Definition: MPoly.hpp:35
std::vector< T > weight
Definition: cubature.hpp:77
unsigned dim
Definition: cubature.hpp:79
Definition: Dlex.hpp:19
void set_threshold(double eps)
Definition: cubature.hpp:44
int m_degree
Definition: cubature.hpp:80
void SetCoeff(const C &c)
Definition: Monom.hpp:68
Multivariate monomials.
Definition: Monom.hpp:21
iterator begin()
Definition: MPoly.hpp:97
The solversdp struct.
Definition: solversdp.h:26
T & obj_mat(int i, int j)
Coefficient of the objective matrix.
Definition: cubature.hpp:40
int m_numeq
Definition: cubature.hpp:81
std::vector< T > point
Definition: cubature.hpp:78
Home  |  Download & InstallContributions