Functional
Functional implementations of the genetic algorithm operators.
Instead of the object-oriented genetic algorithm API (GeneticAlgorithm), one might wish to adopt a style that is more compatible with the functional programming paradigm. For such cases, the functional operators within this namespace can be used.
The operators within this namespace are designed to be called directly, allowing one to implement a genetic algorithm according to which, how, and when these operators are used.
Reasons for using the functional operators.
- Flexibility. This API provides various genetic-algorithm-related operators and gets out of the picture. The user has the complete control over what happens between this operator calls, and in what order these operators are used.
- Batched search. These functional operators are designed in such a way that, if they receive a batched population instead of a single population (i.e. if they receive a 3-or-more-dimensional tensor instead of a 2-dimensional tensor), they will broadcast their operations across the extra leftmost dimensions. This allows one to implement a genetic algorithm that works across many populations at once, in a vectorized manner.
- Nested optimization. It could be the case that the optimization problem at hand has an inner optimization problem within its fitness function. This inner optimization problem could be tackled with the help of a genetic algorithm built upon these functional operators. Such an approach would allow the user to run a search for each inner optimization problem across the entire population of the outer problem, in a vectorized manner (see the previous point titled "batched search").
Example usage. Let us assume that we have the following cost function that to be minimized:
A genetic algorithm could be designed with the help of these functional operators as follows:
import torch
from evotorch.operators.functional import two_point_cross_over, combine, take_best
def f(x: torch.Tensor) -> torch.Tensor:
return torch.linalg.norm(x - 1, dim=-1)
popsize = 100 # population size
solution_length = 20 # length of a solution
num_generations = 200 # number of generations
mutation_stdev = 0.01 # standard deviation for mutation
tournament_size = 4 # size for the tournament selection
# Randomly initialize a population, and compute the solution costs
population = torch.randn(popsize, solution_length)
costs = f(population)
# Initialize the variables that will store the decision values and the cost
# for the last population's best solution.
pop_best_values = None
pop_best_cost = None
# main loop of the optimization
for generation in range(1, 1 + num_generations):
# Given the population and the solution costs, pick parents and apply
# cross-over on them.
candidates = two_point_cross_over(
population,
costs,
tournament_size=tournament_size,
objective_sense="min",
)
# Apply Gaussian mutation on the candidates
candidates = candidates + (torch.randn(popsize, solution_length) * mutation_stdev)
# Compute the solution costs of the candidates
candidate_costs = f(candidates)
# Combine the parents and the candidates into an extended population
extended_values, extended_costs = combine(
(population, costs),
(candidates, candidate_costs),
)
# Take the best `popsize` number of solutions from the extended population
population, costs = take_best(
extended_values,
extended_costs,
popsize,
objective_sense="min",
)
# Take the best solution and its cost
pop_best_values, pop_best_cost = take_best(population, costs, objective_sense="min")
# Print the status
print("Generation:", generation, " Best cost within population:", best_cost)
# Print the result
print()
print("Best solution of the last population:")
print(pop_best_values)
print("Cost of the best solution of the last population:")
print(pop_best_cost)
combine(a, b, *, objective_sense=None)
¶
Combine two populations into one.
Usage 1: without evaluation results.
Let us assume that we have two decision values matrices, values1
values2
. The shapes of these matrices are (n1, L) and (n2, L)
respectively, where L represents the length of a solution.
Let us assume that the solutions that these decision values
represent are not evaluated yet. Therefore, we do not have evaluation
results (i.e. we do not have fitnesses). To combine these two
unevaluated populations, we use this function as follows:
combined_population = combine(values1, values2)
# We now have a combined decision values matrix, shaped (n1+n2, L).
Usage 2: with evaluation results, single-objective.
We again assume that we have two decision values matrices, values1
and values2
. Like in our previous example, these matrices are shaped
(n1, L) and (n2, L), respectively. Additionally, let us assume that we
know the evaluation results for the solutions represented by values1
and values2
. These evaluation results are represented by the tensors
evals1
and evals2
, shaped (n1,) and (n2,), respectively. To combine
these two evaluated populations, we use this function as follows:
c_values, c_evals = combine((values1, evals1), (values2, evals2))
# We now have a combined decision values matrix and a combined evaluations
# vector.
# `c_values` is shaped (n1+n2, L), and `c_evals` is shaped (n1+n2,).
Usage 3: with evaluation results, multi-objective.
We again assume that we have two decision values matrices, values1
and values2
. Like in our previous example, these matrices are shaped
(n1, L) and (n2, L), respectively. Additionally, we assume that we know
the evaluation results for these solutions. The evaluation results are
stored within the tensors evals1
and evals2
, whose shapes are
(n1, M) and (n2, M), where M is the number of objectives. To combine
these two evaluated populations, we use this function as follows:
c_values, c_evals = combine(
(values1, evals1),
(values2, evals2),
objective_sense=["min", "min"], # Assuming we have 2 min objectives
)
# We now have a combined decision values matrix and a combined evaluations
# vector.
# `c_values` is shaped (n1+n2, L), and `c_evals` is shaped (n1+n2,).
Support for ObjectArray.
This function supports decision values that are expressed via instances
of ObjectArray
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
a
|
Union[Tensor, ObjectArray, tuple]
|
A decision values tensor with at least 2 dimensions, or an
|
required |
b
|
Union[Tensor, ObjectArray, tuple]
|
A decision values tensor with at least 2 dimensions, or an
|
required |
objective_sense
|
Optional[Union[str, Iterable]]
|
In the case of single-objective optimization,
|
None
|
Source code in evotorch/operators/functional.py
1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 |
|
cosyne_permutation(values, evals=None, *, permute_all=True, objective_sense=None)
¶
Return the permuted (i.e. shuffled) version of the given decision values.
Shuffling of the decision values is done columnwise.
If permute_all
is given as True, each item within each column will be
subject to permutation. In this mode, the arguments evals
and
objective_sense
can be omitted (i.e. can be left as None).
If permute_all
is given as False, each item within each column is given
a probability of staying the same. This probability is higher for items
that belong to solutions with better fitnesses. In this mode, the
arguments evals
and objective_sense
are mandatory.
Reference:
Gomez, F., Schmidhuber, J., Miikkulainen, R., & Mitchell, M. (2008).
Accelerated Neural Evolution through Cooperatively Coevolved Synapses.
Journal of Machine Learning Research, 9(5).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
population
|
A tensor with at least 2 dimensions, representing the decision values of the solutions. Extra leftmost dimensions will be considered as batch dimensions. |
required | |
evals
|
Optional[Tensor]
|
Evaluation results (i.e. fitnesses), as a tensor with at least
one dimension. Extra leftmost dimensions will be considered as
batch dimensions. If |
None
|
permute_all
|
bool
|
Whether or not each item within each column will be subject to permutation operation. If given as False, items with better fitnesses have greater probabilities of staying the same. The default is True. |
True
|
objective_sense
|
Optional[str]
|
A string whose value is either 'min' or 'max',
representing the goal of the optimization. If |
None
|
Source code in evotorch/operators/functional.py
dominates(evals1, evals2, *, objective_sense)
¶
Return whether or not the first solution pareto-dominates the second one.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
evals1
|
Tensor
|
Evaluation results of the first solution. Expected as an at-least-1-dimensional tensor, the length of which must be equal to the number of objectives. Extra leftmost dimensions will be considered as batch dimensions. |
required |
evals2
|
Tensor
|
Evaluation results of the second solution. Expected as an at-least-1-dimensional tensor, the length of which must be equal to the number of objectives. Extra leftmost dimensions will be considered as batch dimensions. |
required |
objective_sense
|
list
|
Expected as a list of strings, where each string is either 'min' or 'max', expressing the direction of optimization regarding each objective. |
required |
Source code in evotorch/operators/functional.py
domination_counts(evals, *, objective_sense)
¶
Return a tensor expressing how many times each solution gets dominated
In this returned tensor, the i
-th item is an integer which specifies how
many times the i
-th solution is dominated.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
evals
|
Tensor
|
Expected as an at-least-2-dimensional tensor. In such a
2-dimensional evaluation tensor, the item |
required |
objective_sense
|
list
|
A list of strings, where each string is either 'min' or 'max', expressing the direction of optimization regarding each objective. |
required |
Source code in evotorch/operators/functional.py
domination_matrix(evals, *, objective_sense)
¶
Compute and return a pareto-domination matrix.
In this pareto-domination matrix P
, the item P[i,j]
is True if the
i
-th solution is dominated by the j
-th solution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
evals
|
Tensor
|
Evaluation results of the solutions, expected as a tensor
with at least 2 dimensions. In a 2-dimensional |
required |
objective_sense
|
list
|
A list of strings, where each string is either 'min' or 'max', expressing the direction of optimization regarding each objective. |
required |
Source code in evotorch/operators/functional.py
multi_point_cross_over(parents, evals=None, *, num_points, tournament_size=None, num_children=None, objective_sense=None)
¶
Apply multi-point cross-over on the given parents
.
If tournament_size
is given, parents for the cross-over operation will
be picked with the help of a tournament. Otherwise, the first half of the
given parents
will be the first set of parents, and the second half
of the given parents
will be the second set of parents.
The return value of this function is a new tensor containing the decision values of the child solutions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
parents
|
Tensor
|
A tensor with at least 2 dimensions, representing the decision values of the parent solutions. If this tensor has more than 2 dimensions, the extra leftmost dimension(s) will be considered as batch dimensions. |
required |
evals
|
Optional[Tensor]
|
A tensor with at least 1 dimension, representing the evaluation
results (i.e. fitnesses) of the parent solutions. If this tensor
has more than 1 dimension, the extra leftmost dimension(s) will be
considered as batch dimensions. If |
None
|
num_points
|
int
|
Number of points at which the decision values of the parent solutions will be cut and recombined to form the child solutions. |
required |
tournament_size
|
Optional[int]
|
If given as an integer that is greater than or equal
to 1, the parents for the cross-over operation will be picked
with the help of a tournament. In more details, each parent will
be picked as the result of comparing multiple competing solutions,
the number of these competing solutions being equal to this
|
None
|
num_children
|
Optional[int]
|
Optionally the number of children to produce as the
result of tournament selection and cross-over, as an even integer.
If tournament selection is enabled (i.e. if |
None
|
objective_sense
|
Optional[Union[str, list]]
|
Mandatory if |
None
|
Source code in evotorch/operators/functional.py
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 |
|
one_point_cross_over(parents, evals=None, *, tournament_size=None, num_children=None, objective_sense=None)
¶
Apply one-point cross-over on the given parents
.
Let us assume that we have the following two parent solutions:
________________________
parentA | a1 a2 a3 a4 a5 a6 |
parentB | b1 b2 b3 b4 b5 b6 |
|________________________|
This cross-over operation will first randomly decide a cutting point:
________|________________
parentA | a1 a2 | a3 a4 a5 a6 |
parentB | b1 b2 | b3 b4 b5 b6 |
|________|________________|
|
...and then form the following child solutions by recombining the decision values of the parents:
________|________________
child1 | a1 a2 | b3 b4 b5 b6 |
child2 | b1 b2 | a3 a4 a5 a6 |
|________|________________|
|
If tournament_size
is given, parents for the cross-over operation will
be picked with the help of a tournament. Otherwise, the first half of the
given parents
will be the first set of parents, and the second half
of the given parents
will be the second set of parents.
The return value of this function is a new tensor containing the decision values of the child solutions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
parents
|
Tensor
|
A tensor with at least 2 dimensions, representing the decision values of the parent solutions. If this tensor has more than 2 dimensions, the extra leftmost dimension(s) will be considered as batch dimensions. |
required |
evals
|
Optional[Tensor]
|
A tensor with at least 1 dimension, representing the evaluation
results (i.e. fitnesses) of the parent solutions. If this tensor
has more than 1 dimension, the extra leftmost dimension(s) will be
considered as batch dimensions. If |
None
|
tournament_size
|
Optional[int]
|
If given as an integer that is greater than or equal
to 1, the parents for the cross-over operation will be picked
with the help of a tournament. In more details, each parent will
be picked as the result of comparing multiple competing solutions,
the number of these competing solutions being equal to this
|
None
|
num_children
|
Optional[int]
|
Optionally the number of children to produce as the
result of tournament selection and cross-over, as an even integer.
If tournament selection is enabled (i.e. if |
None
|
objective_sense
|
Optional[str]
|
Mandatory if |
None
|
Source code in evotorch/operators/functional.py
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 |
|
pareto_utility(evals, *, objective_sense, crowdsort=True)
¶
Compute utility values for the solutions of a multi-objective problem.
A solution on a better pareto-front is assigned a higher utility value.
Additionally, if crowdsort
is given as True crowding distances will also
be taken into account. In more details, in the same pareto-front,
solutions with higher crowding distances will have increased utility
values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
evals
|
Tensor
|
Evaluation results, expected as a tensor with at least two
dimensions. A 2-dimensional |
required |
objective_sense
|
list
|
Expected as a list of strings, where each string is either 'min' or 'max'. The i-th item within this list represents the direction of the optimization for the i-th objective. |
required |
Source code in evotorch/operators/functional.py
simulated_binary_cross_over(parents, evals=None, *, eta, tournament_size=None, num_children=None, objective_sense=None)
¶
Apply simulated binary cross-over (SBX) on the given parents
.
If tournament_size
is given, parents for the cross-over operation will
be picked with the help of a tournament. Otherwise, the first half of the
given parents
will be the first set of parents, and the second half
of the given parents
will be the second set of parents.
The return value of this function is a new tensor containing the decision values of the child solutions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
parents
|
Tensor
|
A tensor with at least 2 dimensions, representing the decision values of the parent solutions. If this tensor has more than 2 dimensions, the extra leftmost dimension(s) will be considered as batch dimensions. |
required |
evals
|
Optional[Tensor]
|
A tensor with at least 1 dimension, representing the evaluation
results (i.e. fitnesses) of the parent solutions. If this tensor
has more than 1 dimension, the extra leftmost dimension(s) will be
considered as batch dimensions. If |
None
|
eta
|
Union[float, Tensor]
|
The crowding index, expected as a real number. Bigger eta values
result in children closer to their parents. If |
required |
tournament_size
|
Optional[int]
|
If given as an integer that is greater than or equal
to 1, the parents for the cross-over operation will be picked
with the help of a tournament. In more details, each parent will
be picked as the result of comparing multiple competing solutions,
the number of these competing solutions being equal to this
|
None
|
num_children
|
Optional[int]
|
Optionally the number of children to produce as the
result of tournament selection and cross-over, as an even integer.
If tournament selection is enabled (i.e. if |
None
|
objective_sense
|
Optional[str]
|
Mandatory if |
None
|
Source code in evotorch/operators/functional.py
1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 |
|
take_best(values, evals, n=None, *, objective_sense, crowdsort=True)
¶
Take the best solution, or the best n
number of solutions.
Single-objective case.
If the positional argument n
is omitted (i.e. is left as None), the
decision values and the evaluation result of the single best solution
will be returned.
If the positional argument n
is provided, top-n
solutions, together
with their evaluation results, will be returned.
Multi-objective case.
In the multi-objective case, the positional argument n
is mandatory.
With a valid value for n
given, n
number of solutions will be taken
from the best pareto-fronts. If crowdsort
is given as True (which is
the default), crowding distances of the solutions within the same
pareto-fronts will be an additional criterion when deciding which
solutions to take. Like in the single-objective case, the decision values
and the evaluation results of the taken solutions will be returned.
Support for ObjectArray.
This function supports decision values expressed via instances of
ObjectArray
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
values
|
Union[Tensor, ObjectArray]
|
Decision values, expressed via a tensor with at least
2 dimensions or via an |
required |
evals
|
Tensor
|
Evaluation results tensor, with at least 1 dimension. Extra leftmost dimensions will be taken as batch dimensions. |
required |
n
|
Optional[int]
|
If left as None, the single best solution will be taken. If given as an integer, this number of best solutions will be taken. Please note that, if the problem at hand has multiple objectives, this argument cannot be omitted. |
None
|
objective_sense
|
Union[str, list]
|
In the single-objective case, |
required |
crowdsort
|
bool
|
Relevant only when there are multiple objectives.
If |
True
|
Source code in evotorch/operators/functional.py
2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 |
|
tournament(solutions, evals, *, num_tournaments, tournament_size, objective_sense, return_indices=False, with_evals=False, split_results=False)
¶
Randomly organize pairs of tournaments and pick the winning solutions.
Hyperparameters regarding the tournament selection are
num_tournaments
(number of tournaments), and tournament_size
(size of each tournament).
How does each tournament work?
tournament_size
number of solutions are randomly sampled from the given
solutions
, and then, the best solution among the sampled solutions is
declared the winner. In the case of single-objective optimization, the
best solution is the one with the best evaluation result (i.e. best
fitness). In the case of multi-objective optimization, the best solution
is the one within the best pareto-front.
How are multiple tournaments are organized?
Two sets of tournaments are organized. Each set contains n
number of
tournaments, n
being the half of num_tournaments
.
For example, let us assume that num_tournaments
is 6. Then, we have:
First set of tournaments : tournamentA, tournamentB, tournamentC
Second set of tournaments : tournamentD, tournamentE, tournamentF
In this organization of tournaments, the winner of tournamentA is meant for cross-over with the winner of tournamentD; the winner of tournamentB is meant for cross-over with the winner of tournamentE; and the winner of tournamentC is meant for cross-over with the winner of tournamentF.
While sampling the participants for these tournaments, it is ensured that the winner of tournamentA does not participate into tournamentD; the winner of tournamentB does not participate into tournamentE; and the winner of tournamentC does not participate into tournamentF. Therefore, each cross-over operation is applied on two different parent solutions.
How are the tournament results represented? The tournament results are returned in various forms. These various forms are as follows.
Results in the form of decision values.
This is the default form of results (with return_indices=False
,
with_evals=False
, split_results=False
). Here, the results are
expressed as a single tensor (or ObjectArray
) of decision values.
The first half of these decision values represent the first set of
parents, and the second half of these decision values represent the second
half of these decision values represent the second set of parents.
For example, let us assume that the number of tournaments
(num_tournaments
) is configured as 6. In this case, the result is a
decision values tensor with 6 rows (or an ObjectArray
of length 6).
In these results (let us call them resulting_values
), the pairings
for the cross-over operations are as follows:
- resulting_values[0]
and resulting_values[3]
;
- resulting_values[1]
and resulting_values[4]
;
- resulting_values[2]
and resulting_values[5]
.
Results in the form of indices.
This form of results can be taken with arguments return_indices=True
,
with_evals=False
, split_results=False
. Here, the results are
expressed as a single tensor of integers, each integer being the index
of a solution within solutions
.
For example, let us assume that the number of tournaments
(num_tournaments
) is configured as 6. In this case, the result is a
tensor of indices of length 6.
In these results (let us call them resulting_indices
), the pairings
for the cross-over operations are as follows:
- resulting_indices[0]
and resulting_indices[3]
;
- resulting_indices[1]
and resulting_indices[4]
;
- resulting_indices[2]
and resulting_indices[5]
.
Results in the form of decision values and evaluations.
This form of results can be taken with arguments return_indices=False
,
with_evals=True
, split_results=False
. Here, the results are expressed
via a named tuple in the form (parent_values=..., parent_evals=...)
.
In this tuple, parent_values
stores a tensor (or an ObjectArray
)
representing the decision values of the picked solutions, and
parent_evals
stores the evaluation results as a tensor.
For example, let us assume that the number of tournaments
(num_tournaments
) is 6. With this assumption, in the returned named
tuple (let us call it result
), the pairings for the cross-over
operations are as follows:
- result.parent_values[0]
and result.parent_values[3]
;
- result.parent_values[1]
and result.parent_values[4]
;
- result.parent_values[2]
and result.parent_values[5]
.
For any solution result.parent_values[i]
, the evaluation result
is stored by result.parent_evals[i]
.
Results with split parent solutions.
This form of results can be taken with arguments return_indices=False
,
with_evals=False
, split_results=True
. The returned object is a
named tuple in the form (parent1_values=..., parent2_values=...)
.
In the returned named tuple (let us call it result
), the pairings for
the cross-over operations are as follows:
- result.parent1_values[0]
and result.parent2_values[0]
;
- result.parent1_values[1]
and result.parent2_values[1]
;
- result.parent1_values[2]
and result.parent2_values[2]
;
- and so on...
Results with split parent solutions and evaluations.
This form of results can be taken with arguments return_indices=False
,
with_evals=True
, split_results=True
. The returned object is a
named tuple, its attributes being parent1_values
, parent1_evals
,
parent2_values
, and parent2_evals
.
In the returned named tuple (let us call it result
), the pairings for
the cross-over operations are as follows:
- result.parent1_values[0]
and result.parent2_values[0]
;
- result.parent1_values[1]
and result.parent2_values[1]
;
- result.parent1_values[2]
and result.parent2_values[2]
;
- and so on...
For any solution result.parent_values[i]
, the evaluation result
is stored by result.parent_evals[i]
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
solutions
|
Union[Tensor, ObjectArray]
|
Decision values of the solutions. Can be a tensor with
at least 2 dimensions (where extra leftmost dimensions are to be
interpreted as batch dimensions), or an |
required |
evals
|
Tensor
|
Evaluation results of the solutions.
In the single-objective case, this is expected as an
at-least-1-dimensional tensor, the |
required |
num_tournaments
|
int
|
Number of tournaments that will be applied. In other words, number of winners that will be picked. |
required |
tournament_size
|
int
|
Number of solutions to be picked for the tournament |
required |
objective_sense
|
Union[str, list]
|
A string or a list of strings, where (each) string has either the value 'min' for minimization or 'max' for maximization. |
required |
return_indices
|
bool
|
If this is given as True, indices of the selected solutions will be returned, instead of their decision values. |
False
|
with_evals
|
bool
|
If this is given as True, evaluations of the selected solutions will be returned in addition to their decision values. |
False
|
split_results
|
bool
|
If this is given as True, tournament results will be split as first parents and second parents. If this is given as False, results will be stacked vertically, in the sense that the first half of the results are the first parents and the second half of the results are the second parents. |
False
|
Source code in evotorch/operators/functional.py
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 |
|
two_point_cross_over(parents, evals=None, *, tournament_size=None, num_children=None, objective_sense=None)
¶
Apply two-point cross-over on the given parents
.
Let us assume that we have the following two parent solutions:
________________________
parentA | a1 a2 a3 a4 a5 a6 |
parentB | b1 b2 b3 b4 b5 b6 |
|________________________|
This cross-over operation will first randomly decide two cutting points:
________|____________|____
parentA | a1 a2 | a3 a4 a5 | a6 |
parentB | b1 b2 | b3 b4 b5 | b6 |
|________|____________|____|
| |
...and then form the following child solutions by recombining the decision values of the parents:
________|____________|____
child1 | a1 a2 | b3 b4 b5 | a6 |
child2 | b1 b2 | a3 a4 a5 | b6 |
|________|____________|____|
| |
If tournament_size
is given, parents for the cross-over operation will
be picked with the help of a tournament. Otherwise, the first half of the
given parents
will be the first set of parents, and the second half
of the given parents
will be the second set of parents.
The return value of this function is a new tensor containing the decision values of the child solutions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
parents
|
Tensor
|
A tensor with at least 2 dimensions, representing the decision values of the parent solutions. If this tensor has more than 2 dimensions, the extra leftmost dimension(s) will be considered as batch dimensions. |
required |
evals
|
Optional[Tensor]
|
A tensor with at least 1 dimension, representing the evaluation
results (i.e. fitnesses) of the parent solutions. If this tensor
has more than 1 dimension, the extra leftmost dimension(s) will be
considered as batch dimensions. If |
None
|
tournament_size
|
Optional[int]
|
If given as an integer that is greater than or equal
to 1, the parents for the cross-over operation will be picked
with the help of a tournament. In more details, each parent will
be picked as the result of comparing multiple competing solutions,
the number of these competing solutions being equal to this
|
None
|
num_children
|
Optional[int]
|
Optionally the number of children to produce as the
result of tournament selection and cross-over, as an even integer.
If tournament selection is enabled (i.e. if |
None
|
objective_sense
|
Optional[str]
|
Mandatory if |
None
|
Source code in evotorch/operators/functional.py
1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 |
|
utility(evals, *, objective_sense, ranking_method='centered')
¶
Return utility values representing how good the evaluation results are.
A utility number is different from evals
in the sense that, worst
solution has the lowest utility, and the best solution has the highest
utility, regardless of the objective sense ('min' or 'max').
On the other hand, the lowest number within evals
could represent
the fitness of the best solution or of the worst solution, depending
on the objective sense.
The "centered" ranking is the same ranking method that was used within:
Tim Salimans, Jonathan Ho, Xi Chen, Szymon Sidor, Ilya Sutskever (2017).
Evolution Strategies as a Scalable Alternative to Reinforcement Learning
Parameters:
Name | Type | Description | Default |
---|---|---|---|
evals
|
Tensor
|
An at least 1-dimensional tensor that stores evaluation results (i.e. fitness values). Extra leftmost dimensions will be taken as batch dimensions. |
required |
objective_sense
|
str
|
A string whose value is either 'min' or 'max', which represents the goal of the optimization (minimization or maximization). |
required |
ranking_method
|
Optional[str]
|
Ranking method according to which the utilities will
be computed. Currently, this function supports:
'centered' (worst one gets -0.5, best one gets 0.5);
'linear' (worst one gets 0.0, best one gets 1.0);
'raw' (evaluation results themselves are returned, with the
additional behavior of flipping the signs if |
'centered'
|