1 /*===========================================================================*
2  * This file is part of the Discrete Conic Optimization (DisCO) Solver.      *
3  *                                                                           *
4  * DisCO is distributed under the Eclipse Public License as part of the      *
5  * COIN-OR repository (http://www.coin-or.org).                              *
6  *                                                                           *
7  * Authors:                                                                  *
8  *          Aykut Bulut, Lehigh University                                   *
9  *          Yan Xu, Lehigh University                                        *
10  *          Ted Ralphs, Lehigh University                                    *
11  *                                                                           *
12  * Copyright (C) 2001-2018, Lehigh University, Aykut Bulut, Yan Xu, and      *
13  *                          Ted Ralphs.                                      *
14  * All Rights Reserved.                                                      *
15  *===========================================================================*/
16 
17 
18 #include "DcoLinearConstraint.hpp"
19 
20 #include <CoinHelperFunctions.hpp>
21 #include <OsiRowCut.hpp>
22 
DcoLinearConstraint()23 DcoLinearConstraint::DcoLinearConstraint() {
24   size_ = 0;
25   indices_ = NULL;
26   values_ = NULL;
27 }
28 
DcoLinearConstraint(int size,int const * indices,double const * values,double lb,double ub)29 DcoLinearConstraint::DcoLinearConstraint(int size, int const * indices,
30                                          double const * values, double lb,
31                                          double ub):
32   DcoConstraint(lb, ub) {
33   size_ = size;
34   indices_ = new int[size];
35   std::copy(indices, indices+size, indices_);
36   values_ = new double[size];
37   std::copy(values, values+size, values_);
38 }
39 
DcoLinearConstraint(DcoLinearConstraint const & other)40 DcoLinearConstraint::DcoLinearConstraint(DcoLinearConstraint const & other):
41   DcoConstraint(other) {
42   size_ = other.getSize();
43   // set indices
44   indices_ = new int[size_];
45   int const * other_indices = other.getIndices();
46   std::copy(other_indices, other_indices+size_, indices_);
47   // set values
48   values_ = new double[size_];
49   double const * other_values = other.getValues();
50   std::copy(other_values, other_values+size_, values_);
51 }
52 
53 DcoLinearConstraint &
operator =(DcoLinearConstraint const & rhs)54 DcoLinearConstraint::operator=(DcoLinearConstraint const & rhs) {
55   size_ = rhs.getSize();
56   // set indices
57   indices_ = new int[size_];
58   int const * rhs_indices = rhs.getIndices();
59   std::copy(rhs_indices, rhs_indices+size_, indices_);
60   // set values
61   values_ = new double[size_];
62   double const * rhs_values = rhs.getValues();
63   std::copy(rhs_values, rhs_values+size_, values_);
64   return *this;
65 }
66 
~DcoLinearConstraint()67 DcoLinearConstraint::~DcoLinearConstraint() {
68   if (indices_) {
69     delete[] indices_;
70   }
71   if (values_) {
72     delete[] values_;
73   }
74 }
75 
getSize() const76 int DcoLinearConstraint::getSize() const {
77   return size_;
78 }
79 
getIndices() const80 int const * DcoLinearConstraint::getIndices() const {
81   return indices_;
82 }
83 
getValues() const84 double const * DcoLinearConstraint::getValues() const {
85   return values_;
86 }
87 
88 /// Create a OsiRowCut based on this constraint.
createOsiRowCut(DcoModel * model) const89 OsiRowCut * DcoLinearConstraint::createOsiRowCut(DcoModel * model) const {
90   double lower = CoinMax(getLbHard(), getLbSoft());
91   double upper = CoinMin(getUbHard(), getUbSoft());
92   OsiRowCut * cut = new OsiRowCut;
93   if (!cut) {
94     // Out of memory.
95     model->dcoMessageHandler_->message(DISCO_OUT_OF_MEMORY,
96                                             *(model->dcoMessages_))
97       << __FILE__ << __LINE__ << CoinMessageEol;
98     throw CoinError("Out of memory", "createOsiRowCut", "DcoConstraint");
99   }
100   assert(size_ > 0);
101   cut->setLb(lower);
102   cut->setUb(upper);
103   cut->setRow(size_, indices_, values_);
104   return cut;
105 }
106 
infeasibility(BcpsModel * m,int & preferredWay) const107 double DcoLinearConstraint::infeasibility(BcpsModel * m,
108                                           int & preferredWay) const {
109   std::cerr << "Not implemented!" << std::endl;
110   throw std::exception();
111   return 0.0;
112 }
113 
114 
115 /// Encode this to an AlpsEncoded object.
encode(AlpsEncoded * encoded)116 AlpsReturnStatus DcoLinearConstraint::encode(AlpsEncoded * encoded) {
117   std::cerr << "Not implemented, "
118             << "file: " <<  __FILE__
119             << "line: " << __LINE__
120             << std::endl;
121   throw std::exception();
122   return AlpsReturnStatusOk;
123 }
124 
125 /// Decode a given AlpsEncoded object to an AlpsKnowledge object and return a
126 /// pointer to it.
decode(AlpsEncoded & encoded) const127 AlpsKnowledge * DcoLinearConstraint::decode(AlpsEncoded & encoded) const {
128   std::cerr << "Not implemented, "
129             << "file: " <<  __FILE__
130             << "line: " << __LINE__
131             << std::endl;
132   throw std::exception();
133   DcoLinearConstraint * con = new DcoLinearConstraint();
134   // if (status!=AlpsReturnStatusOk) {
135   //   std::cerr << "Unexpected decode status, "
136   //             << "file: " <<  __FILE__
137   //             << "line: " << __LINE__
138   //             << std::endl;
139   //   throw std::exception();
140   // }
141   return con;
142 }
143 
144 // todo(aykut) this should be a pure virtual function in Alps level
145 // we can overload this function here due to cv-qualifier.
146 /// Decode a given AlpsEncoded object into self.
decodeToSelf(AlpsEncoded & encoded)147 AlpsReturnStatus DcoLinearConstraint::decodeToSelf(AlpsEncoded & encoded) {
148   std::cerr << "Not implemented, "
149             << "file: " <<  __FILE__
150             << "line: " << __LINE__
151             << std::endl;
152   throw std::exception();
153   return AlpsReturnStatusOk;
154 }
155