MISolver.cc 18.2 KB
Newer Older
Henrik Zimmer's avatar
Henrik Zimmer committed
1
2
/*===========================================================================*\
 *                                                                           *
Henrik Zimmer's avatar
Henrik Zimmer committed
3
 *                               CoMISo                                      *
Henrik Zimmer's avatar
Henrik Zimmer committed
4
 *      Copyright (C) 2008-2009 by Computer Graphics Group, RWTH Aachen      *
Henrik Zimmer's avatar
Henrik Zimmer committed
5
 *                           www.rwth-graphics.de                            *
Henrik Zimmer's avatar
Henrik Zimmer committed
6
 *                                                                           *
Henrik Zimmer's avatar
Henrik Zimmer committed
7
8
 *---------------------------------------------------------------------------* 
 *  This file is part of CoMISo.                                             *
Henrik Zimmer's avatar
Henrik Zimmer committed
9
 *                                                                           *
Henrik Zimmer's avatar
Henrik Zimmer committed
10
11
12
13
 *  CoMISo is free software: you can redistribute it and/or modify           *
 *  it under the terms of the GNU General Public License as published by     *
 *  the Free Software Foundation, either version 3 of the License, or        *
 *  (at your option) any later version.                                      *
Henrik Zimmer's avatar
Henrik Zimmer committed
14
15
16
17
 *                                                                           *
 *  CoMISo is distributed in the hope that it will be useful,                *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
Henrik Zimmer's avatar
Henrik Zimmer committed
18
 *  GNU General Public License for more details.                             *
Henrik Zimmer's avatar
Henrik Zimmer committed
19
 *                                                                           *
Henrik Zimmer's avatar
Henrik Zimmer committed
20
21
 *  You should have received a copy of the GNU General Public License        *
 *  along with CoMISo.  If not, see <http://www.gnu.org/licenses/>.          *
Henrik Zimmer's avatar
Henrik Zimmer committed
22
 *                                                                           *
Henrik Zimmer's avatar
Henrik Zimmer committed
23
\*===========================================================================*/ 
Henrik Zimmer's avatar
Henrik Zimmer committed
24
25


David Bommes's avatar
David Bommes committed
26
#include <CoMISo/Config/config.hh>
27
28
#include "MISolver.hh"

David Bommes's avatar
changed    
David Bommes committed
29
#if(COMISO_QT4_AVAILABLE)
30
#include <CoMISo/QtWidgets/MISolverDialogUI.hh>
31
32
#endif

David Bommes's avatar
David Bommes committed
33
34
35
36
#if COMISO_GUROBI_AVAILABLE
  #include <gurobi_c++.h>
#endif

David Bommes's avatar
David Bommes committed
37
38
#include <CoMISo/Utils/StopWatch.hh>

39
40
41
#include <gmm/gmm.h>
#include <float.h>

42
43
44
// hack for testing only
#include "SparseQRSolver.hh"
#include "UMFPACKSolver.hh"
David Bommes's avatar
David Bommes committed
45

46
47
#define ROUND(x) ((x)<0?int((x)-0.5):int((x)+0.5))

48
namespace COMISO {
49
50
51
52
53
54
55
56



// Constructor
MISolver::MISolver() 
{
  // default parameters
  initial_full_solution_ = true;
David Bommes's avatar
David Bommes committed
57
  iter_full_solution_    = true;
58
59
  final_full_solution_   = true;

David Bommes's avatar
David Bommes committed
60
61
62
  direct_rounding_   = false;
  no_rounding_       = false;
  multiple_rounding_ = true;
David Bommes's avatar
David Bommes committed
63
  gurobi_rounding_   = false;
David Bommes's avatar
David Bommes committed
64
  cplex_rounding_    = false;
65

David Bommes's avatar
David Bommes committed
66
  max_local_iters_ = 100000;
67
  max_local_error_ = 1e-3;
David Bommes's avatar
David Bommes committed
68
  max_cg_iters_    = 50;
69
  max_cg_error_    = 1e-3;
David Bommes's avatar
David Bommes committed
70
71
72

  multiple_rounding_threshold_ = 0.5;

David Bommes's avatar
David Bommes committed
73
74
  gurobi_max_time_ = 60;

75
76
  noisy_ = 0;
  stats_ = true;
77
78

  use_constraint_reordering_ = true;
79
80
}

David Bommes's avatar
David Bommes committed
81

82
83
84
//-----------------------------------------------------------------------------


David Bommes's avatar
David Bommes committed
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
void 
MISolver::solve_no_rounding( 
    CSCMatrix& _A, 
    Vecd&      _x, 
    Vecd&      _rhs )
{
  chol_.calc_system_gmm(_A);
  chol_.solve(_x, _rhs);
}


//-----------------------------------------------------------------------------


void 
100
101
102
103
104
105
106
107
108
109
110
111
MISolver::resolve(
    Vecd&      _x,
    Vecd&      _rhs )
{
  chol_.solve(_x, _rhs);
}


//-----------------------------------------------------------------------------


void
David Bommes's avatar
David Bommes committed
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
MISolver::solve_direct_rounding( 
    CSCMatrix& _A, 
    Vecd&      _x, 
    Vecd&      _rhs, 
    Veci&      _to_round)
{
  Veci to_round(_to_round);
  // copy to round vector and make it unique
  std::sort(to_round.begin(), to_round.end());
  Veci::iterator last_unique;
  last_unique = std::unique(to_round.begin(), to_round.end());
  int r = last_unique - to_round.begin();
  to_round.resize( r);

  // initalize old indices
  Veci old_idx(_rhs.size());
  for(unsigned int i=0; i<old_idx.size(); ++i)
    old_idx[i] = i;
  chol_.calc_system_gmm(_A);
  chol_.solve(_x, _rhs);

133
134
135
136
  // check solver performance (only for testing!!!)
  {
    StopWatch sw;

David Bommes's avatar
David Bommes committed
137
138
    const bool enable_performance_test = false;

139
    // performance comparison code
David Bommes's avatar
changed    
David Bommes committed
140
#if(COMISO_SUITESPARSE_SPQR_AVAILABLE)
David Bommes's avatar
David Bommes committed
141
    if(enable_performance_test)
142
143
144
145
146
147
148
149
150
151
152
153
154
    {
      sw.start();
      COMISO::SparseQRSolver spqr;
      spqr.calc_system_gmm(_A);
      std::cerr << "SparseQR factor took: " << sw.stop()/1000.0 << "s\n";
      Vecd x2(_x);
      sw.start();
      spqr.solve(x2,_rhs);
      std::cerr << "SparseQR solve took: " << sw.stop()/1000.0 << "s\n";
      Vecd res(_x);
      gmm::add(_x,gmm::scaled(x2,-1.0),res);
      std::cerr << "DIFFERENCE IN RESULT: " << gmm::vect_norm2(res) << std::endl;
    }
David Bommes's avatar
changed    
David Bommes committed
155
#endif
156
157

    // performance comparison code
David Bommes's avatar
David Bommes committed
158
    if(enable_performance_test)
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
    {
      sw.start();
      COMISO::UMFPACKSolver umf;
      umf.calc_system_gmm(_A);
      std::cerr << "UMFPack factor took: " << sw.stop()/1000.0 << "s\n";
      Vecd x3(_x);
      sw.start();
      umf.solve(x3,_rhs);
      std::cerr << "UMFPack solve took: " << sw.stop()/1000.0 << "s\n";
      Vecd res2(_x);
      gmm::add(_x,gmm::scaled(x3,-1.0),res2);
      std::cerr << "UMFPACK DIFFERENCE IN RESULT: " << gmm::vect_norm2(res2) << std::endl;
    }

    // performance comparison code
David Bommes's avatar
David Bommes committed
174
    if(enable_performance_test)
175
176
177
178
179
180
181
182
183
184
185
186
187
188
    {
      sw.start();
      COMISO::CholmodSolver chol;
      chol.calc_system_gmm(_A);
      std::cerr << "Choldmod factor took: " << sw.stop()/1000.0 << "s\n";
      Vecd x4(_x);
      sw.start();
      chol.solve(x4,_rhs);
      std::cerr << "Choldmod solve took: " << sw.stop()/1000.0 << "s\n";
      Vecd res(_x);
      gmm::add(_x,gmm::scaled(x4,-1.0),res);
      std::cerr << "DIFFERENCE IN RESULT: " << gmm::vect_norm2(res) << std::endl;
    }
  }
David Bommes's avatar
David Bommes committed
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243

  // round and eliminate variables
  Vecui elim_i;
  Vecd  elim_v;
  for( unsigned int i=0; i < to_round.size(); ++i)
  {
    _x[to_round[i]] = ROUND(_x[to_round[i]]);
    elim_i.push_back(to_round[i]);
    elim_v.push_back(_x[to_round[i]]);
    // update old idx
    old_idx[to_round[i]] = -1;
  }

  Veci::iterator new_end = std::remove( old_idx.begin(), old_idx.end(), -1);
  old_idx.resize( new_end-old_idx.begin());
  // eliminate vars from linear system
  Vecd xr(_x);
  COMISO_GMM::eliminate_csc_vars2( elim_i, elim_v, _A, xr, _rhs);

  // std::cerr << "size A: " << gmm::mat_nrows(_A) << " " << gmm::mat_ncols(_A) 
  // 	    << std::endl;
  // std::cerr << "size x  : " << xr.size() << std::endl;
  // std::cerr << "size rhs: " << _rhs.size() << std::endl;

  // final full solution
  if( gmm::mat_ncols( _A) > 0)
  {
    //    chol_.update_system_gmm(_A);
    chol_.calc_system_gmm(_A);
    chol_.solve( xr, _rhs);
  }

  // store solution values to result vector
  for(unsigned int i=0; i<old_idx.size(); ++i)
  {
    _x[ old_idx[i] ] = xr[i];
  }
}


//-----------------------------------------------------------------------------


void 
MISolver::solve_iterative( 
    CSCMatrix& _A, 
    Vecd&      _x, 
    Vecd&      _rhs, 
    Veci&      _to_round,
    bool       _fixed_order )
{
  // StopWatch
  COMISO::StopWatch sw;
  double time_search_next_integer = 0;

244
  // some statistics
David Bommes's avatar
David Bommes committed
245
246
247
248
249
250
  n_local_ = 0;
  n_cg_    = 0;
  n_full_  = 0;

  // reset cholmod step flag
  cholmod_step_done_ = false;
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268

  Veci to_round(_to_round);
  // if the order is not fixed, uniquify the indices
  if( !_fixed_order)
  {
    // copy to round vector and make it unique
    std::sort(to_round.begin(), to_round.end());
    Veci::iterator last_unique;
    last_unique = std::unique(to_round.begin(), to_round.end());
    int r = last_unique - to_round.begin();
    to_round.resize( r);
  }

  // initalize old indices
  Veci old_idx(_rhs.size());
  for(unsigned int i=0; i<old_idx.size(); ++i)
    old_idx[i] = i;

David Bommes's avatar
David Bommes committed
269
  if( initial_full_solution_)
270
271
  {
    if( noisy_ > 2) std::cerr << "initial full solution" << std::endl;
David Bommes's avatar
David Bommes committed
272
273
    chol_.calc_system_gmm(_A);
    chol_.solve(_x, _rhs);
274

David Bommes's avatar
David Bommes committed
275
    cholmod_step_done_ = true;
276

David Bommes's avatar
David Bommes committed
277
278
    ++n_full_;
  }
279
280
281
282
283
284
285
286
287
288
289
290
291
292

  // neighbors for local optimization
  Vecui neigh_i;

  // Vector for reduced solution
  Vecd xr(_x);

  // loop until solution computed
  for(unsigned int i=0; i<to_round.size(); ++i)
  {
    if( noisy_ > 0)
    {
      std::cerr << "Integer DOF's left: " << to_round.size()-(i+1) << " ";
      if( noisy_ > 1)
David Bommes's avatar
David Bommes committed
293
        std::cerr << "residuum_norm: " << COMISO_GMM::residuum_norm( _A, xr, _rhs) << std::endl;
294
295
296
297
298
    }

    // index to eliminate
    unsigned int i_best = 0;

David Bommes's avatar
David Bommes committed
299
300
301
    // position in round vector
    unsigned int tr_best = 0;

302
303
304
305
306
307
    if( _fixed_order ) // if order is fixed, simply get next index from to_round
    {
      i_best = to_round[i];
    }
    else               // else search for best rounding candidate
    {
David Bommes's avatar
David Bommes committed
308
      sw.start();
309
310
311
312
313
314
315
316
317
318
      // find index yielding smallest rounding error
      double       r_best = FLT_MAX;
      for(unsigned int j=0; j<to_round.size(); ++j)
      {
        if( to_round[j] != -1)
        {
          int cur_idx = to_round[j];
          double rnd_error = fabs( ROUND(xr[cur_idx]) - xr[cur_idx]);
          if( rnd_error < r_best)
          {
David Bommes's avatar
David Bommes committed
319
320
321
            i_best  = cur_idx;
            r_best  = rnd_error;
	    tr_best = j;
322
323
324
          }
        }
      }
David Bommes's avatar
David Bommes committed
325
      time_search_next_integer += sw.stop();
326
327
328
329
330
331
332
333
334
335
336
337
    }

    // store rounded value
    double rnd_x = ROUND(xr[i_best]);
    _x[ old_idx[i_best] ] = rnd_x;

    // compute neighbors
    neigh_i.clear();
    Col col = gmm::mat_const_col(_A, i_best);
    ColIter it  = gmm::vect_const_begin( col);
    ColIter ite = gmm::vect_const_end  ( col);
    for(; it!=ite; ++it)
David Bommes's avatar
David Bommes committed
338
      if(it.index() != i_best)
339
340
341
        neigh_i.push_back(it.index());

    // eliminate var
David Bommes's avatar
David Bommes committed
342
343
    COMISO_GMM::fix_var_csc_symmetric(i_best, rnd_x, _A, xr, _rhs);
    to_round[tr_best] = -1;
344

David Bommes's avatar
David Bommes committed
345
346
347
348
    // 3-stage update of solution w.r.t. roundings
    // local GS / CG / SparseCholesky
    update_solution( _A, xr, _rhs, neigh_i);
  }
349

David Bommes's avatar
David Bommes committed
350
351
352
353
  // final full solution?
  if( final_full_solution_)
  {
    if( noisy_ > 2) std::cerr << "final full solution" << std::endl;
354

David Bommes's avatar
David Bommes committed
355
    if( gmm::mat_ncols( _A) > 0)
356
    {
David Bommes's avatar
David Bommes committed
357
358
359
360
      if(cholmod_step_done_)
	chol_.update_system_gmm(_A);
      else
	chol_.calc_system_gmm(_A);
361

David Bommes's avatar
David Bommes committed
362
363
      chol_.solve( xr, _rhs);
      ++n_full_;
364
    }
David Bommes's avatar
David Bommes committed
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
  }

  // store solution values to result vector
  for(unsigned int i=0; i<old_idx.size(); ++i)
  {
    _x[ old_idx[i] ] = xr[i];
  }

  // output statistics
  if( stats_)
  {
    std::cerr << "\t" << __FUNCTION__ << " *** Statistics of MiSo Solver ***\n";
    std::cerr << "\t\t Number of CG    iterations  = " << n_cg_ << std::endl;
    std::cerr << "\t\t Number of LOCAL iterations  = " << n_local_ << std::endl;
    std::cerr << "\t\t Number of FULL  iterations  = " << n_full_ << std::endl;
    std::cerr << "\t\t Number of ROUNDING          = " << _to_round.size() << std::endl;
    std::cerr << "\t\t time searching next integer = " << time_search_next_integer / 1000.0 <<"s\n";
    std::cerr << std::endl;
  }
}
385
386
387



David Bommes's avatar
David Bommes committed
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
//-----------------------------------------------------------------------------


void 
MISolver::update_solution( 
    CSCMatrix& _A, 
    Vecd&      _x, 
    Vecd&      _rhs, 
    Vecui&     _neigh_i )
{
  // set to not converged
  bool converged = false;

  // compute new solution
  if(max_local_iters_ > 0)
  {
    if( noisy_ > 2)std::cerr << "use local iteration ";

    int    n_its     = max_local_iters_;
    double tolerance = max_local_error_;
    converged = siter_.gauss_seidel_local(_A, _x, _rhs, _neigh_i, n_its, tolerance);

    ++n_local_;
  }


  // conjugate gradient
  if( !converged && max_cg_iters_ > 0)
  {
    if( noisy_ > 2) std::cerr << ", cg ";

    int max_cg_iters = max_cg_iters_;
    double tolerance = max_cg_error_;
    converged = siter_.conjugate_gradient(_A, _x,_rhs, max_cg_iters, tolerance);

    if( noisy_ > 3) 
      std::cerr << "( converged " << converged << " "
		<< " iters " << max_cg_iters   << " "
		<< " res_norm " << tolerance << std::endl;
    ++n_cg_;
  }

  if(!converged && iter_full_solution_)
  {
    if( noisy_ > 2)std::cerr << ", full ";

    if( gmm::mat_ncols( _A) > 0)
    {
      if(cholmod_step_done_)
	chol_.update_system_gmm(_A);
      else
439
      {
David Bommes's avatar
David Bommes committed
440
441
	chol_.calc_system_gmm(_A);
	cholmod_step_done_ = true;
442
      }
David Bommes's avatar
David Bommes committed
443
444
445
      chol_.solve(_x,_rhs);

      ++n_full_;
446
    }
David Bommes's avatar
David Bommes committed
447
448
449
450
451
452
  }

  if( noisy_ > 2)std::cerr << std::endl;
}

//-----------------------------------------------------------------------------
453

David Bommes's avatar
David Bommes committed
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507

void 
MISolver::solve_multiple_rounding( 
    CSCMatrix& _A, 
    Vecd&      _x, 
    Vecd&      _rhs, 
    Veci&      _to_round )
{
  // StopWatch
  COMISO::StopWatch sw;
  double time_search_next_integer = 0;

  // some statistics
  n_local_ = 0;
  n_cg_    = 0;
  n_full_  = 0;

  // reset cholmod step flag
  cholmod_step_done_ = false;

  Veci to_round(_to_round);
  // copy to round vector and make it unique
  std::sort(to_round.begin(), to_round.end());
  Veci::iterator last_unique;
  last_unique = std::unique(to_round.begin(), to_round.end());
  int r = last_unique - to_round.begin();
  to_round.resize( r);

  // initalize old indices
  Veci old_idx(_rhs.size());
  for(unsigned int i=0; i<old_idx.size(); ++i)
    old_idx[i] = i;

  if( initial_full_solution_)
  {
    if( noisy_ > 2) std::cerr << "initial full solution" << std::endl;
    chol_.calc_system_gmm(_A);
    chol_.solve(_x, _rhs);

    cholmod_step_done_ = true;

    ++n_full_;
  }

  // neighbors for local optimization
  Vecui neigh_i;

  // Vector for reduced solution
  Vecd xr(_x);

  // loop until solution computed
  for(unsigned int i=0; i<to_round.size(); ++i)
  {
    if( noisy_ > 0)
508
    {
David Bommes's avatar
David Bommes committed
509
510
511
512
513
514
515
516
517
518
519
520
      std::cerr << "Integer DOF's left: " << to_round.size()-(i+1) << " ";
      if( noisy_ > 1)
        std::cerr << "residuum_norm: " << COMISO_GMM::residuum_norm( _A, xr, _rhs) << std::endl;
    }

    // position in round vector
    std::vector<int> tr_best;

    sw.start();

    RoundingSet rset;
    rset.set_threshold(multiple_rounding_threshold_);
521

David Bommes's avatar
David Bommes committed
522
523
524
525
    // find index yielding smallest rounding error
    for(unsigned int j=0; j<to_round.size(); ++j)
    {
      if( to_round[j] != -1)
526
      {
David Bommes's avatar
David Bommes committed
527
528
	int cur_idx = to_round[j];
	double rnd_error = fabs( ROUND(xr[cur_idx]) - xr[cur_idx]);
529

David Bommes's avatar
David Bommes committed
530
	rset.add(j, rnd_error);
531
532
533
      }
    }

David Bommes's avatar
David Bommes committed
534
535
536
537
538
    rset.get_ids( tr_best);

    time_search_next_integer += sw.stop();
  
    // nothing more to do?
Jan Möbius's avatar
Jan Möbius committed
539
    if( tr_best.empty() )
David Bommes's avatar
David Bommes committed
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
      break;

    if( noisy_ > 5)
      std::cerr << "round " << tr_best.size() << " variables simultaneously\n";

    // clear neigh for local update
    neigh_i.clear();

    for(unsigned int j = 0; j<tr_best.size(); ++j)
    {
      int i_cur = to_round[tr_best[j]];

      // store rounded value
      double rnd_x = ROUND(xr[i_cur]);
      _x[ old_idx[i_cur] ] = rnd_x;

      // compute neighbors
      Col col = gmm::mat_const_col(_A, i_cur);
      ColIter it  = gmm::vect_const_begin( col);
      ColIter ite = gmm::vect_const_end  ( col);
      for(; it!=ite; ++it)
	if(it.index() != (unsigned int)i_cur)
	  neigh_i.push_back(it.index());

      // eliminate var
      COMISO_GMM::fix_var_csc_symmetric( i_cur, rnd_x, _A, xr, _rhs);
      to_round[tr_best[j]] = -1;
    }

    // 3-stage update of solution w.r.t. roundings
    // local GS / CG / SparseCholesky
    update_solution( _A, xr, _rhs, neigh_i);
572
573
574
  }

  // final full solution?
David Bommes's avatar
David Bommes committed
575
  if( final_full_solution_)
576
577
578
579
580
  {
    if( noisy_ > 2) std::cerr << "final full solution" << std::endl;

    if( gmm::mat_ncols( _A) > 0)
    {
David Bommes's avatar
David Bommes committed
581
582
583
584
585
586
587
      if(cholmod_step_done_)
	chol_.update_system_gmm(_A);
      else
	chol_.calc_system_gmm(_A);

      chol_.solve( xr, _rhs);
      ++n_full_;
588
589
590
591
592
593
594
595
596
597
598
599
600
    }
  }

  // store solution values to result vector
  for(unsigned int i=0; i<old_idx.size(); ++i)
  {
    _x[ old_idx[i] ] = xr[i];
  }

  // output statistics
  if( stats_)
  {
    std::cerr << "\t" << __FUNCTION__ << " *** Statistics of MiSo Solver ***\n";
David Bommes's avatar
David Bommes committed
601
602
603
604
605
    std::cerr << "\t\t Number of CG    iterations  = " << n_cg_ << std::endl;
    std::cerr << "\t\t Number of LOCAL iterations  = " << n_local_ << std::endl;
    std::cerr << "\t\t Number of FULL  iterations  = " << n_full_ << std::endl;
    std::cerr << "\t\t Number of ROUNDING          = " << _to_round.size() << std::endl;
    std::cerr << "\t\t time searching next integer = " << time_search_next_integer / 1000.0 <<"s\n";
606
607
608
609
610
    std::cerr << std::endl;
  }
}


David Bommes's avatar
David Bommes committed
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
//-----------------------------------------------------------------------------


void
MISolver::solve_gurobi(
    CSCMatrix& _A,
    Vecd&      _x,
    Vecd&      _rhs,
    Veci&      _to_round)
{
#if COMISO_GUROBI_AVAILABLE

  // get round-indices in set
  std::set<int> to_round;
  for(unsigned int i=0; i<_to_round.size();++i)
    to_round.insert(_to_round[i]);

  try {
    GRBEnv env = GRBEnv();

    GRBModel model = GRBModel(env);

    // set time limite
    model.getEnv().set(GRB_DoubleParam_TimeLimit, gurobi_max_time_);

    unsigned int n = _rhs.size();

    // 1. allocate variables
    std::vector<GRBVar> vars;
    for( unsigned int i=0; i<n; ++i)
      if( to_round.count(i))
        vars.push_back( model.addVar(-GRB_INFINITY, GRB_INFINITY, 0.0, GRB_INTEGER));
      else
        vars.push_back( model.addVar(-GRB_INFINITY, GRB_INFINITY, 0.0, GRB_CONTINUOUS));

    // Integrate new variables
    model.update();

    // 2. setup_energy

    // build objective function from linear system E = x^tAx - 2x^t*rhs
    GRBQuadExpr objective;

    for(unsigned int i=0; i<_A.nc; ++i)
      for(unsigned int j=_A.jc[i]; j<_A.jc[i+1]; ++j)
      {
        objective += _A.pr[j]*vars[_A.ir[j]]*vars[i];
      }
    for(unsigned int i=0; i<n; ++i)
      objective -= 2*_rhs[i]*vars[i];

//    _A.jc[c+1]
//    _A.pr[write]
//    _A.ir[write]
//    _A.nc
//    _A.nr

    // minimize
    model.set(GRB_IntAttr_ModelSense, 1);
    model.setObjective(objective);

    // 4. solve
    model.optimize();

    // 5. store result
    _x.resize(n);
    for(unsigned int i=0; i<n; ++i)
      _x[i] = vars[i].get(GRB_DoubleAttr_X);

    std::cout << "GUROBI objective: " << model.get(GRB_DoubleAttr_ObjVal) << std::endl;

  }
Jan Möbius's avatar
Jan Möbius committed
683
  catch(GRBException& e)
David Bommes's avatar
David Bommes committed
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
  {
    std::cout << "Error code = " << e.getErrorCode() << std::endl;
    std::cout << e.getMessage() << std::endl;
  }
  catch(...)
  {
    std::cout << "Exception during optimization" << std::endl;
  }

#else
  std::cerr << "GUROBI solver is not available, please install it..." << std::endl;
#endif
}


699
700
701
702
703
704
705
//----------------------------------------------------------------------------


void 
MISolver::
show_options_dialog()
{
David Bommes's avatar
changed    
David Bommes committed
706
#if(COMISO_QT4_AVAILABLE)
707
708
  MISolverDialog* pd = new MISolverDialog(*this);
  pd->exec();
David Bommes's avatar
changed    
David Bommes committed
709
710
#else
  std::cerr << "Warning: Qt not available to show solver dialog!!!" << std::endl;
711
712
713
714
#endif
}


715
// end namespace COMISO
716
}// ----------------------