1 #ifndef __MINLCA_MINLCA_OPTIMISATION_SA_GREEDY_HH
2 #define __MINLCA_MINLCA_OPTIMISATION_SA_GREEDY_HH
8 #include <lemon/connectivity.h>
20 template<
template<
typename>
class Greedy,
typename Graph = lemon::SmartGraph>
22 :
public MinLCA<Graph>,
private lemon::BfsVisitor<Graph>
24 TEMPLATE_GRAPH_TYPEDEFS(Graph);
33 friend class lemon::BfsVisit<Graph, SAorder>;
71 _order.reserve(lemon::countNodes(g));
72 _r =
new lemon::Random(seed);
92 void setRandom(lemon::Random *r,
bool deleteRandom =
false)
99 _delete_random = deleteRandom;
113 if (
_sa !=
nullptr) {
122 lemon::BfsVisit<Graph, SAorder> bfs(this->
_g, *
this);
125 IntNodeMap cc(this->
_g);
126 int numCc = lemon::connectedComponents(this->
_g, cc);
127 std::vector<bool> withNode(numCc,
false);
129 for (NodeIt v(this->
_g); v != lemon::INVALID; ++v) {
130 if (not withNode[cc[v]]) {
132 withNode[cc[v]] =
true;
139 for (NodeIt v(this->
_g); v != lemon::INVALID; ++v) {
143 int n = lemon::countNodes(this->
_g);
145 for (
int i = 0; i < n; ++i) {
146 std::swap(_order[i], _order[_r->integer(i, n)]);
166 for (NodeIt v(this->
_g); v != lemon::INVALID; ++v) {
190 typedef Greedy<Graph> Base;
191 std::vector<Node> _order;
198 const std::vector<Node> &initial
210 return -
static_cast<double>(this->
lcaValue());
213 return -std::numeric_limits<double>::infinity();
222 for (
int i = idx; i < _order.size(); ++i) {
223 this->paintVertex(_order[i]);
240 for (
int i = 0; i < idx; ++i) {
241 this->
setColour(_order[i], c[_order[i]]);
251 int n = _order.size();
252 int u = _sa->
_r->integer(n - 1);
253 int v = u + 1 + _sa->
_r->integer(n - u - 1);
254 std::swap(neighbour->_order[u], neighbour->_order[v]);
256 neighbour->
run(this->
_c, u);
263 void process(
const Node &v)
long long lcaValue() const
Value (cost) of the current solution.
GreedyNodeSA(SAorder *sa, const std::vector< Node > &initial)
Constructor.
GreedyNodeSA * _result
Pointer to the node obtained with simulated annealing.
bool _delete_random
Delete the random number generator in destructor.
virtual void init()
Reset the data structures of the algorithm.
virtual void setColour(const Node &v, int vColour)
Set the colour of a vertex.
Generic simulated annealing algorithm.
MinLCAStatus
Possible status for MinLCA solution.
const Graph & _g
A const reference to the graph we want to arrange.
GreedyNodeSA * randomNeighbour()
Get a random neighbour.
double energy()
Energy of the node.
void setRandom(lemon::Random *r, bool deleteRandom=false)
Colouring _c
The vertex colouring.
virtual void init()
Initialise data structures. Do not use.
Solution has been found after running.
std::vector< Node > _order
Initial vertex order.
Default namespace Default namespace for MinLCA algorithms.
Class defining the node for the simulated annealing search changing the colours.
Defines the base class for MinLCA algorithms.
int _max_k
Maximum number of colours allowed.
lemon::Random * _r
Pointer to the random number generator used.
virtual MinLCAStatus run()
Run the algorithm.
Base class for MinLCA algorithms.
~MinLCAVertexOrderSA()
Destructor.
MinLCAStatus run(const typename BaseLCA::Colouring &c, int idx)
Run the algorithm.
Solution has not been found after running.
void setSolutionNotFound()
Auxiliary function to set solution status to not found.
virtual MinLCAStatus run(int idx=0)
Run the algorithm.
virtual void init(int steps, int stIter, int k, double lambda, bool initial=false)
Reset the data structures of the algorithm.
IntNodeMap Colouring
Type for graph colourings.
Description of generic simulated annealing algorithm.
SAorder * _sa
Pointer to the parent class containing the problem data.
MinLCAStatus status() const
Status of the solution.
int maxK()
Maximum number of allowed colours.
void setSolutionFound()
Auxiliary function to set solution status to found.
MinLCAVertexOrderSA(const Graph &g, int maxK=0, int seed=0)
Constructor.
utils::SimulatedAnnealing< GreedyNodeSA > * _sa
Pointer to the simulated annealing algorithm.
Class defining a local search approach doing greedy recolourings.