All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Macros Pages
local_search_example.cpp
Go to the documentation of this file.
1 //=======================================================================
2 // Copyright (c) 2013 Piotr Wygocki
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //=======================================================================
18 
19 #include <vector>
20 #include <iostream>
21 
22 namespace ls = paal::local_search;
23 using namespace paal;
24 
25 int f(int x) { return -x * x + 12 * x - 27; }
26 
27 struct get_moves {
28  const std::vector<int> neighb;
29 
30  public:
31 
32  get_moves() : neighb({ 10, -10, 1, -1 }) {}
33 
34  const std::vector<int> &operator()(int x) const { return neighb; }
35 };
36 
37 struct gain {
38  int operator()(int s, int u) { return f(s + u) - f(s); }
39 };
40 
41 struct commit {
42  bool operator()(int &s, int u) {
43  s = s + u;
44  return true;
45  }
46 };
47 
49 
51 
52 int main() {
54  // creating solution
55  int solution(0);
56 
57  // search
58  first_improving(solution, search_comps());
59 
60  // print
61  std::cout << "Local search solution: " << solution << std::endl;
62  return 0;
63 }
bool first_improving(Solution &solution, components...comps)
typename components::type< Args...> search_components
search_components template alias
int main()
[Local Search Components Example]
bool local_search(Solution &solution, SearchStrategy searchStrategy, ContinueOnSuccess succ, ContinueOnFail fail, components...comps)
detail