main.cc 4.82 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*===========================================================================*\
 *                                                                           *
 *                               CoMISo                                      *
 *      Copyright (C) 2008-2009 by Computer Graphics Group, RWTH Aachen      *
 *                           www.rwth-graphics.de                            *
 *                                                                           *
 *---------------------------------------------------------------------------* 
 *  This file is part of CoMISo.                                             *
 *                                                                           *
 *  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.                                      *
 *                                                                           *
 *  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            *
 *  GNU General Public License for more details.                             *
 *                                                                           *
 *  You should have received a copy of the GNU General Public License        *
 *  along with CoMISo.  If not, see <http://www.gnu.org/licenses/>.          *
 *                                                                           *
\*===========================================================================*/ 

#include <CoMISo/Config/config.hh>

#include <CoMISo/Utils/StopWatch.hh>
#include <vector>
#include <CoMISo/NSolver/LeastSquaresProblem.hh>
#include <CoMISo/NSolver/LinearConstraint.hh>
#include <CoMISo/NSolver/NPDerivativeChecker.hh>
#include <CoMISo/NSolver/CPLEXSolver.hh>
33
#include <CoMISo/NSolver/IPOPTSolver.hh>
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

// solve least squares problem for x=1, y=2 and x-2y+z = 1
// with hard constraints x =-3, z>=3, z^2 >= x^2+y^2


// Example main
int main(void)
{
  std::cout << "---------- 1) Problem description..." << std::endl;
  std::cout << "Least squares terms: x=1, y=2 and x-2y+z = 1" << std::endl;
  std::cout << "Constraints        : x =-3, z>=3, z^2 >= x^2+y^2" << std::endl;

  std::cout << "---------- 1) Get an instance of a LeastSquaresProblem..." << std::endl;
  // number of unknowns
  const int n = 3;
  COMISO::LeastSquaresProblem lsqp(n);

  // term0
  COMISO::LinearConstraint::SVectorNC coeffs0(n);
  coeffs0.coeffRef(0) = 1.0;
  COMISO::LinearConstraint term0(coeffs0,-1.0,COMISO::NConstraintInterface::NC_EQUAL);
  lsqp.add_term(&term0);

    // term1
  COMISO::LinearConstraint::SVectorNC coeffs1(n);
  coeffs1.coeffRef(1) = 1.0;
  COMISO::LinearConstraint term1(coeffs1,-2.0,COMISO::NConstraintInterface::NC_EQUAL);
  lsqp.add_term(&term1);
  
    // term2
  COMISO::LinearConstraint::SVectorNC coeffs2(n);
  coeffs2.coeffRef(0) =  1.0;
  coeffs2.coeffRef(1) = -2.0;
  coeffs2.coeffRef(2) =  1.0;
  COMISO::LinearConstraint term2(coeffs2,-1.0,COMISO::NConstraintInterface::NC_EQUAL);
  lsqp.add_term(&term2);
  
  std::cout << "---------- 2) set up constraints" << std::endl;

  // set x = -3.0
  COMISO::LinearConstraint lc;
David Bommes's avatar
David Bommes committed
75
  lc.resize(n);
76
77
78
79
80
81
82
83
84
85
  lc.coeffs().coeffRef(0) = 1.0;
  lc.b()                  = 3.0;

  // set z>=3 (cone constraint requires that z>=0 !!!)
  COMISO::BoundConstraint bc(2,3,3,COMISO::NConstraintInterface::NC_GREATER_EQUAL);

  // set z^2 >= x^2+y^2
  COMISO::ConeConstraint cc;
  cc.resize(3);
  cc.i() = 2;
David Bommes's avatar
David Bommes committed
86
87
88
89
90
  cc.c() = 4.0;
  cc.Q()(0,0) = 2.0;
  cc.Q()(1,1) = 4.0;
  cc.Q()(0,1) = 1.0;
  cc.Q()(1,0) = 1.0;
91
92
93
94
95
96
97

  // fill constraint vector
  std::vector<COMISO::NConstraintInterface*> constraints;
  constraints.push_back(&lc);
  constraints.push_back(&bc);
  constraints.push_back(&cc);

98
99
100
101
102
// check if CPLEX solver available in current configuration
#if( COMISO_CPLEX_AVAILABLE)
  std::cout << "---------- 3) Solve with CPLEX solver... " << std::endl;

  COMISO::CPLEXSolver cplx;
103
104
105
  cplx.solve(&lsqp, constraints);
#endif

David Bommes's avatar
David Bommes committed
106
107
108
109
  std::cout << "---------- 4) Print solution CPLEX..." << std::endl;
  for( int i=0; i<n; ++i)
    std::cerr << "x_" << i << " = " << lsqp.x()[i] << std::endl;

110
111
  // check if IPOPT solver available in current configuration
#if( COMISO_IPOPT_AVAILABLE)
David Bommes's avatar
David Bommes committed
112
  std::cout << "---------- 5) Solve with IPOPT solver... " << std::endl;
113
114
115
116
117
118

  COMISO::IPOPTSolver ipopt;
  ipopt.app().Options()->SetStringValue("derivative_test", "second-order");
  ipopt.solve(&lsqp, constraints);
#endif

David Bommes's avatar
David Bommes committed
119
  std::cout << "---------- 6) Print solution..." << std::endl;
120
121
122
123
124
125
  for( int i=0; i<n; ++i)
    std::cerr << "x_" << i << " = " << lsqp.x()[i] << std::endl;
  
  return 0;
}