1 #ifndef __MINLCA_MINLCA_OPTIMISATION_SA_COLOURS_HH
2 #define __MINLCA_MINLCA_OPTIMISATION_SA_COLOURS_HH
18 template<
typename Graph = lemon::SmartGraph>
21 TEMPLATE_GRAPH_TYPEDEFS(Graph);
66 _r =
new lemon::Random(seed);
79 if (_result !=
nullptr) {
98 _delete_random = deleteRandom;
105 virtual void init(
int steps,
int stIter,
int k,
double lambda)
109 if (
_sa !=
nullptr) {
115 if (_result !=
nullptr) {
123 if (_result !=
nullptr) {
127 _result =
new NodeSA(
this, c);
136 _result =
_sa->run();
138 for (NodeIt v(this->
_g); v != lemon::INVALID; ++v) {
160 TEMPLATE_GRAPH_TYPEDEFS(Graph);
182 for (NodeIt v(this->
_g); v != lemon::INVALID; ++v) {
191 for (NodeIt v(this->
_g); v != lemon::INVALID; ++v) {
196 inline void init(
const std::vector<int> &oldCounter)
198 _counter = oldCounter;
207 for (NodeIt v(this->
_g); v != lemon::INVALID; ++v) {
227 for (IncEdgeIt e(
_g, v); e != lemon::INVALID; ++e) {
228 Node u =
_g.oppositeNode(v, e);
230 if (this->
_c[u] == this->
_c[v]) {
248 for (NodeIt v(
_g); v != lemon::INVALID; ++v) {
249 if (this->
_c[v] == c1) {
252 else if (this->
_c[v] == c2) {
258 virtual inline void setColour(
const Node &v,
int vColour)
280 if (_counter[c] == 0) {
285 for (NodeIt v(
_g); v != lemon::INVALID; ++v) {
302 while (next ==
nullptr) {
304 next->
init(_counter);
311 int option = _sa->
_r->integer(diceSides);
319 else if (option <= 4) {
321 int c1 = _sa->
_r->integer(k - 1);
322 int c2 = c1 + 1 + _sa->
_r->integer(k - c1 - 1);
354 return -
static_cast<double>(this->
lcaValue());
357 LEMON_ASSERT(
false,
"Using bad solution");
358 return -std::numeric_limits<double>::infinity();
MinLCAStatus _s
The status of the solution.
long long lcaValue() const
Value (cost) of the current solution.
~MinLCAColoursSA()
Destructor.
NodeSA(BaseSA *sa, const typename BaseLCA::Colouring &c, MinLCAStatus s=UNDEFINED)
Constructor.
virtual void init()
Reset the data structures of the algorithm.
bool _delete_random
Delete the random number generator in destructor.
virtual void setColour(const Node &v, int vColour)
Set the colour of a vertex.
Class defining the node for the simulated annealing search changing the colours.
virtual MinLCAStatus run()
Run the algorithm.
virtual void init()
Reset the data structures of the algorithm.
Generic simulated annealing algorithm.
lemon::RangeIdMap< Graph, Node > _vertex_id_map
Map of vertex identifiers.
MinLCAColoursSA< Graph > BaseSA
Parent class.
std::vector< int > _counter
Number of vertices using a colour.
MinLCAStatus
Possible status for MinLCA solution.
MinLCAColoursSA(const Graph &g, int maxK=0, int seed=0)
Constructor.
const Graph & _g
A const reference to the graph we want to arrange.
int _k
Largest index of the colours used.
Colouring _c
The vertex colouring.
Class defining a local search approach changing the colours.
Solution has been found after running.
virtual void init(int steps, int stIter, int k, double lambda)
Reset the data structures of the algorithm.
BaseSA * _sa
Pointer to the parent class containing the problem data.
void setRandom(lemon::Random *r, bool deleteRandom=false)
Change the random number generator.
Default namespace Default namespace for MinLCA algorithms.
Defines the base class for MinLCA algorithms.
Algorithm has not run yet, or some other status.
int _max_k
Maximum number of colours allowed.
Base class for MinLCA algorithms.
Base class for defining optimisation MinLCA algorithms.
double energy() const
Energy of the node.
virtual void setColour(const Node &v, int vColour)
Set the colour of a vertex.
int totalColours() const
Total number of colours used.
Solution has not been found after running.
const Colouring & colouring() const
Current colouring.
NodeSA * randomNeighbour() const
Get a random neighbour.
Defines the base class for MinLCA optimisation algorithms.
void setSolutionNotFound()
Auxiliary function to set solution status to not found.
NodeSA * _result
Pointer to the node obtained with simulated annealing.
IntNodeMap Colouring
Type for graph colourings.
lemon::Random * _r
Pointer to the random number generator used.
virtual MinLCAStatus run()
Run the algorithm.
Description of generic simulated annealing algorithm.
virtual void init()
Initialise data structures. Do not use.
bool checkValid(const Node &v)
Check if a vertex and its adjacent vertices have different colours.
void swapColours(int c1, int c2)
Swap the vertices of two colours.
MinLCAStatus status() const
Status of the solution.
int maxK()
Maximum number of allowed colours.
MinLCAOpt< Graph > Base
Base class.
long long _lca
Cost of the current solution.
void setSolutionFound()
Auxiliary function to set solution status to found.
utils::SimulatedAnnealing< NodeSA > * _sa
Pointer to the simulated annealing algorithm.
void compress(int c)
Compress solution.