1 /*===========================================================================*
2  * This file is part of the Abstract Library for Parallel Search (ALPS).     *
3  *                                                                           *
4  * ALPS is distributed under the Eclipse Public License as part of the       *
5  * COIN-OR repository (http://www.coin-or.org).                              *
6  *                                                                           *
7  * Authors:                                                                  *
8  *                                                                           *
9  *          Yan Xu, Lehigh University                                        *
10  *          Aykut Bulut, Lehigh University                                   *
11  *          Ted Ralphs, Lehigh University                                    *
12  *                                                                           *
13  * Conceptual Design:                                                        *
14  *                                                                           *
15  *          Yan Xu, Lehigh University                                        *
16  *          Ted Ralphs, Lehigh University                                    *
17  *          Laszlo Ladanyi, IBM T.J. Watson Research Center                  *
18  *          Matthew Saltzman, Clemson University                             *
19  *                                                                           *
20  *                                                                           *
21  * Copyright (C) 2001-2019, Lehigh University, Yan Xu, Aykut Bulut, and      *
22  *                          Ted Ralphs.                                      *
23  * All Rights Reserved.                                                      *
24  *===========================================================================*/
25 
26 
27 #include "AlpsKnowledgeBroker.h"
28 #include "AlpsModel.h"
29 
30 //##############################################################################
31 
32 
33 /** Write out parameters. */
34 void
writeParameters(std::ostream & outstream) const35 AlpsModel::writeParameters(std::ostream& outstream) const
36 {
37     AlpsPar_->writeToStream(outstream);
38 }
39 
40 //##############################################################################
41 
42 /** Read in Alps parameters. */
43 void
readParameters(const int argnum,const char * const * arglist)44 AlpsModel::readParameters(const int argnum, const char * const * arglist)
45 {
46     AlpsPar_->readFromArglist(argnum, arglist);
47 }
48 
49 //##############################################################################
50 
51 //todo(aykut) Shouldn't this method be a member of either the broker or subtree classes?
52 //todo(aykut) this should have const modifier
53 void
nodeLog(AlpsTreeNode * node,bool force)54 AlpsModel::nodeLog(AlpsTreeNode *node, bool force)
55 {
56     int nodeInterval =
57         broker_->getModel()->AlpsPar()->entry(AlpsParams::nodeLogInterval);
58 
59     int numNodesProcessed = broker_->getNumNodesProcessed();
60     int numNodesPartial  = broker_->getNumNodesPartial();
61 
62     AlpsTreeNode *bestNode = NULL;
63 
64     if ( (broker_->getProcType() != AlpsProcessTypeMaster) &&
65          (broker_->getProcType() != AlpsProcessTypeSerial) ) {
66         return;
67     }
68 
69     if ( (broker_->getMsgLevel() > 1) &&
70          ( force ||
71            (numNodesProcessed % nodeInterval == 0) ) ) {
72 
73         double feasBound = ALPS_OBJ_MAX, relBound = ALPS_OBJ_MAX;
74 
75         if (broker_->getNumKnowledges(AlpsKnowledgeTypeSolution) > 0) {
76             feasBound = (broker_->getBestKnowledge(AlpsKnowledgeTypeSolution)).second;
77         }
78 
79         bestNode = broker_->getBestNode();
80 
81         if (bestNode) {
82             relBound = bestNode->getQuality();
83         }
84 
85         //Take into account pregnant nodes (processed but not branched)
86         broker_->messageHandler()->
87             message(ALPS_S_NODE_COUNT, broker_->messages())
88             << numNodesProcessed
89             << numNodesPartial
90             << broker_->updateNumNodesLeft() - numNodesPartial
91             << relBound
92             << feasBound
93             << CoinMessageEol;
94     }
95 }
96 
97 /// Pack AlpsPar_ into a given encode object.
encode(AlpsEncoded * encoded) const98 AlpsReturnStatus AlpsModel::encode(AlpsEncoded * encoded) const {
99   AlpsPar_->pack(*encoded);
100   return AlpsReturnStatusOk;
101 }
102 
103 /// Decode the given AlpsEncoded object into this.
decodeToSelf(AlpsEncoded & encoded)104 AlpsReturnStatus AlpsModel::decodeToSelf(AlpsEncoded & encoded) {
105   AlpsPar_->unpack(encoded);
106   return AlpsReturnStatusOk;
107 }
108