source: trunk/Clp/src/MyMessageHandler.cpp @ 2470

Last change on this file since 2470 was 2385, checked in by unxusr, 11 months ago

formatting

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.9 KB
Line 
1/* $Id: MyMessageHandler.cpp 2385 2019-01-06 19:43:06Z stefan $ */
2// Copyright (C) 2003, International Business Machines
3// Corporation and others.  All Rights Reserved.
4// This code is licensed under the terms of the Eclipse Public License (EPL).
5
6#if defined(_MSC_VER)
7// move into CoinPragma.hpp ?
8#pragma warning(disable : 4503)
9#endif
10
11#include <cstdio>
12
13#include "CoinPragma.hpp"
14#include "ClpSimplex.hpp"
15#include "ClpNonLinearCost.hpp"
16#include "MyMessageHandler.hpp"
17#include "ClpMessage.hpp"
18
19//#############################################################################
20// Constructors / Destructor / Assignment
21//#############################################################################
22
23//-------------------------------------------------------------------
24// Default Constructor
25//-------------------------------------------------------------------
26MyMessageHandler::MyMessageHandler()
27  : CoinMessageHandler()
28  , model_(NULL)
29  , feasibleExtremePoints_()
30  , iterationNumber_(-1)
31{
32}
33
34//-------------------------------------------------------------------
35// Copy constructor
36//-------------------------------------------------------------------
37MyMessageHandler::MyMessageHandler(const MyMessageHandler &rhs)
38  : CoinMessageHandler(rhs)
39  , model_(rhs.model_)
40  , feasibleExtremePoints_(rhs.feasibleExtremePoints_)
41  , iterationNumber_(rhs.iterationNumber_)
42{
43}
44
45MyMessageHandler::MyMessageHandler(const CoinMessageHandler &rhs)
46  : CoinMessageHandler(rhs)
47  , model_(NULL)
48  , feasibleExtremePoints_()
49  , iterationNumber_(-1)
50{
51}
52
53// Constructor with pointer to model
54MyMessageHandler::MyMessageHandler(ClpSimplex *model,
55  FILE * /*userPointer*/)
56  : CoinMessageHandler()
57  , model_(model)
58  , feasibleExtremePoints_()
59  , iterationNumber_(-1)
60{
61}
62
63//-------------------------------------------------------------------
64// Destructor
65//-------------------------------------------------------------------
66MyMessageHandler::~MyMessageHandler()
67{
68}
69
70//----------------------------------------------------------------
71// Assignment operator
72//-------------------------------------------------------------------
73MyMessageHandler &
74MyMessageHandler::operator=(const MyMessageHandler &rhs)
75{
76  if (this != &rhs) {
77    CoinMessageHandler::operator=(rhs);
78    model_ = rhs.model_;
79    feasibleExtremePoints_ = rhs.feasibleExtremePoints_;
80    iterationNumber_ = rhs.iterationNumber_;
81  }
82  return *this;
83}
84//-------------------------------------------------------------------
85// Clone
86//-------------------------------------------------------------------
87CoinMessageHandler *MyMessageHandler::clone() const
88{
89  return new MyMessageHandler(*this);
90}
91
92int MyMessageHandler::print()
93{
94  if (currentSource() == "Clp") {
95    if (currentMessage().externalNumber() == 102) {
96      printf("There are %d primal infeasibilities\n",
97        model_->nonLinearCost()->numberInfeasibilities());
98      // Feasibility
99      if (!model_->nonLinearCost()->numberInfeasibilities()) {
100        // Column solution
101        int numberColumns = model_->numberColumns();
102        const double *solution = model_->solutionRegion(1);
103
104        // Create vector to contain solution
105        StdVectorDouble feasibleExtremePoint;
106
107        const double *objective = model_->objective();
108        double objectiveValue = 0;
109
110        if (!model_->columnScale()) {
111          // No scaling
112          for (int i = 0; i < numberColumns; i++) {
113            feasibleExtremePoint.push_back(solution[i]);
114            objectiveValue += solution[i] * objective[i];
115          }
116        } else {
117          // scaled
118          const double *columnScale = model_->columnScale();
119          for (int i = 0; i < numberColumns; i++) {
120            feasibleExtremePoint.push_back(solution[i] * columnScale[i]);
121            objectiveValue += solution[i] * objective[i] * columnScale[i];
122          }
123        }
124        std::cout << "Objective " << objectiveValue << std::endl;
125        // Save solution
126        feasibleExtremePoints_.push_front(feasibleExtremePoint);
127
128        // Want maximum of 10 solutions, so if more then 10 get rid of oldest
129        size_t numExtremePointsSaved = feasibleExtremePoints_.size();
130        if (numExtremePointsSaved >= 10) {
131          feasibleExtremePoints_.pop_back();
132          assert(feasibleExtremePoints_.size() == numExtremePointsSaved - 1);
133        };
134      }
135      return 0; // skip printing
136    }
137  }
138
139  // If one wants access to the message text,
140  // it is available using method messageBuffer().
141  // For example, one could code:
142  // std::cout <<messageBuffer() <<std::endl;
143
144  return CoinMessageHandler::print();
145}
146const ClpSimplex *
147MyMessageHandler::model() const
148{
149  return model_;
150}
151void MyMessageHandler::setModel(ClpSimplex *model)
152{
153  model_ = model;
154}
155
156const std::deque< StdVectorDouble > &MyMessageHandler::getFeasibleExtremePoints() const
157{
158  return feasibleExtremePoints_;
159}
160void MyMessageHandler::clearFeasibleExtremePoints()
161{
162  feasibleExtremePoints_.clear();
163}
164
165/* vi: softtabstop=2 shiftwidth=2 expandtab tabstop=2
166*/
Note: See TracBrowser for help on using the repository browser.