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 "AlpsSolutionPool.h"
28
AlpsSolutionPool(int maxsols)29 AlpsSolutionPool::AlpsSolutionPool(int maxsols)
30 : AlpsKnowledgePool(AlpsKnowledgePoolTypeSolution),
31 maxNumSolutions_(maxsols) {
32 }
33
~AlpsSolutionPool()34 AlpsSolutionPool::~AlpsSolutionPool() {
35 if (! solutions_.empty()) {
36 clean();
37 }
38 }
39
getNumKnowledges() const40 int AlpsSolutionPool::getNumKnowledges() const {
41 return static_cast<int> (solutions_.size());
42 }
43
hasKnowledge() const44 bool AlpsSolutionPool::hasKnowledge() const {
45 return solutions_.empty() ? false : true;
46 }
47
getKnowledge() const48 std::pair<AlpsKnowledge*, double> AlpsSolutionPool::getKnowledge() const {
49 return std::make_pair(static_cast<AlpsKnowledge *>
50 (solutions_.begin()->second),
51 solutions_.begin()->first);
52 }
53
popKnowledge()54 void AlpsSolutionPool::popKnowledge() {
55 throw CoinError("Can not call popKnowledge()",
56 "popKnowledge()", "AlpsSolutionPool");
57 }
58
addKnowledge(AlpsKnowledge * sol,double priority)59 void AlpsSolutionPool::addKnowledge(AlpsKnowledge* sol, double priority) {
60 std::pair<const double, AlpsSolution*>
61 ps(priority, dynamic_cast<AlpsSolution*>(sol));
62 solutions_.insert(ps);
63 if ((maxNumSolutions_ > 0) &&
64 (static_cast<int>(solutions_.size()) > maxNumSolutions_)) {
65 std::multimap< double, AlpsSolution* >::iterator si =
66 solutions_.end();
67 --si;
68 AlpsSolution* sol = si->second;
69 solutions_.erase(si);
70 delete sol;
71 }
72 }
73
74 //todo(aykut) This looks buggy to me.
setMaxNumKnowledges(int maxsols)75 void AlpsSolutionPool::setMaxNumKnowledges(int maxsols) {
76 if (maxsols > 0) {
77 if (static_cast<int>(solutions_.size()) > maxsols) {
78 std::multimap<double, AlpsSolution*>::iterator si = solutions_.begin();
79 for (int i = 0; i < maxsols; ++i) {
80 ++si;
81 }
82 //todo(aykut) why not use solution_.begin()+maxsols
83 solutions_.erase(si, solutions_.end());
84 }
85 }
86 maxNumSolutions_ = maxsols;
87 }
88
getBestKnowledge() const89 std::pair<AlpsKnowledge*, double> AlpsSolutionPool::getBestKnowledge() const {
90 return std::make_pair(static_cast<AlpsKnowledge *>
91 (solutions_.begin()->second),
92 solutions_.begin()->first);
93 }
94
getAllKnowledges(std::vector<std::pair<AlpsKnowledge *,double>> & sols) const95 void AlpsSolutionPool::getAllKnowledges (std::vector<std::pair<AlpsKnowledge*,
96 double> >& sols) const {
97 sols.reserve(sols.size() + solutions_.size());
98 std::multimap<double, AlpsSolution*>::const_iterator si;
99 for (si = solutions_.begin(); si != solutions_.end(); ++si) {
100 sols.push_back(std::make_pair(static_cast<AlpsKnowledge *>
101 (si->second), si->first));
102 }
103 }
104
clean()105 void AlpsSolutionPool::clean() {
106 while (!solutions_.empty()) {
107 std::multimap< double, AlpsSolution* >::iterator si=solutions_.end();
108 --si;
109 AlpsSolution * sol = si->second;
110 solutions_.erase(si);
111 delete sol;
112 sol = NULL;
113 }
114 }
115