Typed Ratings and Dependency Injection via Ratings

A rating function determines the fitness of a given solution to the problem in question. They are generally used in order to guide problem solvers. However, rating functions can also be used in order to inject additional code into solvers. This can be seen as a form of dependency injection. A simple example is provided at the end of this article.

Note: During the design phase of the second version of Gel I searched for methods to create complex constraints out of relatively simple constraints. Typed ratings can be used in order to i.e. negate a given constraint with high quality. I also looked for ways to organize complex manipulations of solutions. However, typed ratings were of no use for the solvers during my master thesis. Instead, the given solution was queried for variables that dissatisfy certain aspects of the constraints.

Note: This article does currently not consider existing research yet.

The hill climber for discrete values is able to imitate any discrete solving algorithm that optimizes a given complete solution. For a given history of intermediate solutions one has to determine a path in the search space visiting all intermediate solutions in the given order. The rating function can be used in order to determine the next step of the hill climber at the current step of the solution path.

If there is a high number of neighbors at the current position of the problem space, then the performance of the hill climber might be inappropriate. This can be compensated by adjusting the problem space dynamically in order to minimize the number of neighbors during a hill climbing step. This shows that the solution space can also be used in order to inject code into solving programs.

The following Python code shows how Simulated Annealing can be injected into the hill climbing algorithm. Note that the implemented rating functions are based on side effects and are therefore not suitable for certain local search algorithms.


#!/usr/bin/env python3
"""This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/."""
__author__ = 'Mārtiņš Avots'
import unittest
from random import uniform, shuffle
from math import log, inf
class annealing_relative_rating:
def __init__(self,
rating_function,
temperature_function,
rating_improvement_factor,
rating_diminishment_factor):
self.rating_function = rating_function
self.temperature_function = temperature_function
self.counter = 0
self.rating_improvement_factor = rating_improvement_factor
self.rating_diminishment_factor = rating_diminishment_factor
def __call__(self, solution):
rating = self.rating_function(solution)
self.counter += 1
temperature = self.temperature_function(self.counter)
if rating == 0 or temperature == 0:
return rating
elif uniform(0, 1) >= temperature:
return rating * self.rating_improvement_factor
else:
if rating * self.rating_diminishment_factor == 0:
return rating
else:
return rating * self.rating_diminishment_factor
class annealing_absolute_rating:
def __init__(self, rating_function, temperature_function, max_rating):
self.rating_function = rating_function
self.temperature_function = temperature_function
self.counter = 0
self.max_rating = max_rating
def __call__(self, solution):
rating = self.rating_function(solution)
self.counter += 1
temperature = self.temperature_function(self.counter)
if rating == 0 or temperature == 0:
return rating
elif uniform(0, 1) >= temperature:
return rating
else:
return self.max_rating
def climb_hill(rating_function, problem_space, current_solution):
best_solution = current_solution
for neighbour in problem_space(current_solution):
if rating_function(best_solution) > rating_function(neighbour):
best_solution = neighbour
return best_solution
def Queen_N_problem(n):
return  [0] * n
def Queen_N_rating(queen_problem):
rVal = 0
for i in range(len(queen_problem)):
for j in range(len(queen_problem)):
if [i] != [j] and queen_problem[i] == queen_problem[j]:
rVal += 1
for i in range(0, len(queen_problem) - 1):
for j in range(i + 1, len(queen_problem)):
k = j - i
if queen_problem[i] == queen_problem[j] - k:
rVal += 1
if queen_problem[i] == queen_problem[j] + k:
rVal += 1
return rVal
def Queen_N_neighbours(queen_problem):
rVal = []
for i in range(len(queen_problem)):
for j in range(len(queen_problem)):
neighbour = list(queen_problem)
neighbour[i] = j
rVal.append(neighbour)
shuffle(rVal)
return rVal
class HillClimbingTestLocalOptima(unittest.TestCase):
"""By default only functions starting with test are executed during testing."""
def __temperatureTestFunction(self, x):
"""Determined by trial and error from universal allocation program's temperature functions."""
return (log(4.0) / log((x + 1)**1.5)) - 0.05
def __hillClimbingTest_GetsStuckInBadLocalOptima(self, rating_function, max_iterations):
for i in range(max_iterations):
solution = Queen_N_problem(8)
for i in range(1000):
solution = climb_hill(rating_function, Queen_N_neighbours, solution)
if Queen_N_rating(solution) == 0:
break
if Queen_N_rating(solution) != 0:
return True
return False
self.assertTrue(
)
def testAbsoluteAnnealingHillClimberFindsGlobalOptima(self):
self.assertFalse(
annealing_absolute_rating(
Queen_N_rating,
self.__temperatureTestFunction,
inf), # A floating-point positive infinity.
10)
)
def testRelativeAnnealingHillClimberFindsGlobalOptima(self):
self.assertFalse(
annealing_relative_rating(
Queen_N_rating,
self.__temperatureTestFunction,
1./8., # Determined by trial and error.
8), # Determined by trial and error.
10)
)
if __name__ == "__main__":
unittest.main()


d:toDo

Every rating function is of one of the following types:

The lower the rating the better the solution. A cost function is also called loss function.

d:toDo
• mathematical formalization

Consecutively, some properties related to the quality of rating functions are listed.

A proportional object function evaluates the compliance of a solution to a given problem: if the rating of a solution A is X-times higher as the rating of another solution B then B disagrees the given constraints X-times as much as A. Constraint weights are considered in this context.

d:toDo
• mathematical formalization

An exact cost function is a proportional cost function where an optimal solution is rated with zero. Note that a rating function that only returns the number of dissatisfied constraints may not be exact. An exact object function needs to respect the weights of the constraints in questions.

d:toDo
• mathematical formalization

An invertible objective function has a return value with a known upper bound. This means that one can take an invertible cost function and create a negation of that function. This is equal to a conversion of a fitness function into a loss function and vice versa.

d:toDo
• mathematical formalization

For a given distorted cost function: If the rating of a solution A is X times higher as the rating of another solution B then A might not disagree the given constraints X-times more than B. Constraint weights are considered in this context.

d:toDo
• mathematical formalization

If a given solution is rated multiple times with such a function, then the result is always the same.

d:toDo
• mathematical formalization

If a given solution is rated multiple times with such a function than the rating may not be the same: i.e. a learning program that learned a rating function but still adjusts its weightings. If a side effect free system is used, then this can be implemented via the concept of time. This can be done by adding a parameter to the rating function representing the current time:

d:toDo
• mathematical formalization

An alternative approach is to implement the rating function as a monad:

d:toDo
• mathematical formalization

A rating function where every solving algorithm has a low probability to find an optimal solution to a randomly chosen problem instance for a given problem:

d:toDo
• mathematical formalization

Subsequently, some types of object functions with different qualities are listed:

For a given solution and search space this cost function returns the minimum distance between the given solution and any optimal solution:

d:toDo
• mathematical formalization

For a given solution, feasible region and solver this cost function returns the expected minimal distance between the given solution and any optimal solution:

d:toDo
• mathematical formalization

For a given feasible region, solution, local search algorithm and a maximum search space walk distance this profit function returns the rating improvements that the search algorithm is expected to find for a given neighbor of the current solution:

d:toDo
• mathematical formalization

For a given feasible region, solution, local search algorithm and a maximum search space walk distance this fitness function returns the probability that the search algorithm will find an optimal solution:

d:toDo
• mathematical formalization
wide screen
1. splitcells
1. dem
2. external-documentation.html
3. guidelines
4. history.html
5. index.html
6. lang
1. perspective
1. guidelines
2. index.html
7. project
2. gel
2. constraint
3. history
4. index.html
5. latest-articles.html
6. model
1. optimization
1. interactive
2. typed-ratings.html
2. rating
7. presentation
8. problem
1. theory
1. assignment
1. problem
1. bibliography
2. complexity
3. index.deprecated.html
4. index.html
5. index.illustration.svg
6. linear.deprecated.html
7. multidimensional.html
9. project
10. solution
11. test
1. functionality
1. n-queen-problem
2. n-queen-problem.html
3. n-queen-problem.implementation.html
3. martins
1. avots
1. website
1. 404.html
4. archive
5. art
6. crib-sheet
8. den
9. experimental
10. images
1. icon.png
2. icon.svg
11. index.html
12. info
13. js
2. basic.default.js
3. basic.js
4. jquery.particleground.min.js
5. math-jax
6. syntaxhighlighter.white
14. legal
20. premature-content.html
21. programming
22. programs
23. projects
24. protocol
25. symbiosis
26. test.html
27. unstructured-thoughts
28. websites
1. cards
1. visiting
29. white
1. 2015
1. 03
1. 01
2. network
1. CHANGELOG.html
3. deployment.html
4. dictionary.html
5. external-contribution.html
6. guidelines
1. changelog.html
2. commonmark.html
3. gist
5. maven.html
6. source-types.html
7. tickets.html
7. legal
1. Developer_Certificate_of_Origin.v1.1.html
2. Developer_Certificate_of_Origin.v1.1.txt
8. overview.html
9. project.html
10. tickets
1. done
2. index.html
3. open
3. system
4. website
1. 404.html
2. css
3. den
4. empty.html
5. images
1. icon.png
2. icon.svg
1. a.city.at.night.jpg
2. average.source.code.image.generator.jpg
3. average.source.code.insight.jpg
4. average.source.code.jpg
5. bird_s.banner.jpg
6. black.background.blog.discovery.0.jpg
7. black.background.blog.discovery.1.jpg
8. black.background.blog.programming.0.jpg
9. black.background.cooperation.discovery.jpg
10. community.2016.12.11.chrom.0.dina4.jpg
11. community.DSC02958.jpg
12. community.DSC03192.jpg
13. community.DSC03197.jpg
15. gallery.0.jpg
16. gallery.1.jpg
17. gallery.2.jpg
18. gallery.3.jpg
19. gallery.4.jpg
20. gallery.5.jpg
21. gallery.6.jpg
22. gallery.7.jpg
23. little_big_present.jpg
24. net.splitcells.network.logo.jpg
25. neural.birds.2.jpg
26. neural.birds.3.jpg
27. neural.birds.4.jpg
28. neural.birds.5.jpg
29. neural.birds.6.jpg
30. night.at.home.jpg
31. simple.bird.jpg
32. simple.ruins.jpg
33. some_things_are_done_without_reason.1.jpg
34. some_things_are_done_without_reason.2.jpg
35. some_things_are_done_without_reason.3.jpg
36. starting-to-learn-how-to-draw-a-face.jpg
37. surreal.biological.cell.jpg
38. thumbnail
1. big
2. medium
3. small
39. white.background.blog.discovery.0.jpg
40. white.background.blog.discovery.1.jpg
41. white.background.blog.programming.0.jpg
42. white.background.cooperation.discovery.jpg
44. white.project.logo.art.jpg
45. white.project.logo.generic.allocator.jpg
6. index.html
7. js