1    	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2    	/*                                                                           */
3    	/*                  This file is part of the class library                   */
4    	/*       SoPlex --- the Sequential object-oriented simPlex.                  */
5    	/*                                                                           */
6    	/*  Copyright (c) 1996-2023 Zuse Institute Berlin (ZIB)                      */
7    	/*                                                                           */
8    	/*  Licensed under the Apache License, Version 2.0 (the "License");          */
9    	/*  you may not use this file except in compliance with the License.         */
10   	/*  You may obtain a copy of the License at                                  */
11   	/*                                                                           */
12   	/*      http://www.apache.org/licenses/LICENSE-2.0                           */
13   	/*                                                                           */
14   	/*  Unless required by applicable law or agreed to in writing, software      */
15   	/*  distributed under the License is distributed on an "AS IS" BASIS,        */
16   	/*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
17   	/*  See the License for the specific language governing permissions and      */
18   	/*  limitations under the License.                                           */
19   	/*                                                                           */
20   	/*  You should have received a copy of the Apache-2.0 license                */
21   	/*  along with SoPlex; see the file LICENSE. If not email to soplex@zib.de.  */
22   	/*                                                                           */
23   	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
24   	
25   	#include <assert.h>
26   	#include <iostream>
27   	
28   	#include "soplex/spxdefines.h"
29   	#include "soplex/spxsolver.h"
30   	
31   	namespace soplex
32   	{
33   	
34   	template <class R>
35   	void SPxSolverBase<R>::qualConstraintViolation(R& maxviol, R& sumviol) const
36   	{
37   	   maxviol = 0.0;
38   	   sumviol = 0.0;
39   	
40   	   VectorBase<R> solu(this->nCols());
41   	
42   	   getPrimalSol(solu);
43   	
44   	   for(int row = 0; row < this->nRows(); ++row)
45   	   {
46   	      const SVectorBase<R>& rowvec = this->rowVector(row);
47   	
48   	      R val = 0.0;
49   	
50   	      for(int col = 0; col < rowvec.size(); ++col)
51   	         val += rowvec.value(col) * solu[rowvec.index(col)];
52   	
53   	      R viol = 0.0;
54   	
55   	      assert(this->lhs(row) <= this->rhs(row) + 1e-9);
56   	
57   	      if(val < this->lhs(row))
58   	         viol = spxAbs(val - this->lhs(row));
59   	      else if(val > this->rhs(row))
60   	         viol = spxAbs(val - this->rhs(row));
61   	
62   	      if(viol > maxviol)
63   	         maxviol = viol;
64   	
65   	      sumviol += viol;
66   	   }
67   	}
68   	
69   	template <class R>
70   	void SPxSolverBase<R>::qualBoundViolation(
71   	   R& maxviol, R& sumviol) const
72   	{
73   	   maxviol = 0.0;
74   	   sumviol = 0.0;
75   	
76   	   VectorBase<R> solu(this->nCols());
77   	
78   	   getPrimalSol(solu);
79   	
80   	   for(int col = 0; col < this->nCols(); ++col)
81   	   {
82   	      assert(this->lower(col) <= this->upper(col) + 1e-9);
83   	
84   	      R viol = 0.0;
85   	
86   	      if(solu[col] < this->lower(col))
87   	         viol = spxAbs(solu[col] - this->lower(col));
88   	      else if(solu[col] > this->upper(col))
89   	         viol = spxAbs(solu[col] - this->upper(col));
90   	
91   	      if(viol > maxviol)
92   	         maxviol = viol;
93   	
94   	      sumviol += viol;
95   	   }
96   	}
97   	
98   	template <class R>
99   	void SPxSolverBase<R>::qualSlackViolation(R& maxviol, R& sumviol) const
100  	{
101  	   maxviol = 0.0;
102  	   sumviol = 0.0;
103  	
104  	   VectorBase<R> solu(this->nCols());
105  	   VectorBase<R> slacks(this->nRows());
106  	
107  	   getPrimalSol(solu);
108  	   getSlacks(slacks);
109  	
110  	   for(int row = 0; row < this->nRows(); ++row)
111  	   {
112  	      const SVectorBase<R>& rowvec = this->rowVector(row);
113  	
114  	      R val = 0.0;
115  	
116  	      for(int col = 0; col < rowvec.size(); ++col)
117  	         val += rowvec.value(col) * solu[rowvec.index(col)];
118  	
119  	      R viol = spxAbs(val - slacks[row]);
120  	
121  	      if(viol > maxviol)
122  	         maxviol = viol;
123  	
124  	      sumviol += viol;
125  	   }
126  	}
127  	
128  	template <class R>
129  	void SPxSolverBase<R>::qualRedCostViolation(R& maxviol, R& sumviol) const
130  	{
131  	   maxviol = 0.0;
132  	   sumviol = 0.0;
133  	
134  	   int i;
135  	
136  	   // TODO:   y = c_B * B^-1  => coSolve(y, c_B)
137  	   //         redcost = c_N - yA_N
138  	   // solve system "x = e_i^T * B^-1" to get i'th row of B^-1
139  	   // VectorBase<R> y( this->nRows() );
140  	   // basis().coSolve( x, spx->unitVector( i ) );
141  	   // VectorBase<R> rdcost( this->nCols() );
142  	   if(type() == ENTER)
143  	   {
144  	      for(i = 0; i < dim(); ++i)
145  	      {
146  	         R x = coTest()[i];
147  	
148  	         if(x < 0.0)
149  	         {
150  	            sumviol -= x;
151  	
152  	            if(x < maxviol)
153  	               maxviol = x;
154  	         }
155  	      }
156  	
157  	      for(i = 0; i < coDim(); ++i)
158  	      {
159  	         R x = test()[i];
160  	
161  	         if(x < 0.0)
162  	         {
163  	            sumviol -= x;
164  	
165  	            if(x < maxviol)
166  	               maxviol = x;
167  	         }
168  	      }
169  	   }
170  	   else
171  	   {
172  	      assert(type() == LEAVE);
173  	
174  	      for(i = 0; i < dim(); ++i)
175  	      {
176  	         R x = fTest()[i];
177  	
178  	         if(x < 0.0)
179  	         {
180  	            sumviol -= x;
181  	
182  	            if(x < maxviol)
183  	               maxviol = x;
184  	         }
185  	      }
186  	   }
187  	
188  	   maxviol *= -1;
189  	}
190  	
191  	} // namespace soplex
192