Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jbytecode/randomkeyga.scala
Random Key Genetic Algorithms for Permutation Optimization in Scala
https://github.com/jbytecode/randomkeyga.scala
Last synced: about 1 month ago
JSON representation
Random Key Genetic Algorithms for Permutation Optimization in Scala
- Host: GitHub
- URL: https://github.com/jbytecode/randomkeyga.scala
- Owner: jbytecode
- License: mit
- Created: 2024-09-26T12:23:10.000Z (3 months ago)
- Default Branch: main
- Last Pushed: 2024-11-13T17:23:56.000Z (about 1 month ago)
- Last Synced: 2024-11-13T18:29:35.897Z (about 1 month ago)
- Language: Scala
- Size: 16.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# randomkeyga.scala
Random Key Genetic Algorithms for Permutation Optimization in Scala# Example
## Traveling Salesman Example
```scala
import org.expr.tsp._
@main def hello(): Unit =
val distanceMatrix = List(
List(0, 1, 2, 3, 4.0),
List(1, 0, 1, 2, 3.0),
List(2, 1, 0, 1, 2.0),
List(3, 2, 1, 0, 1.0),
List(4, 3, 2, 1, 0.0)
)val popsize = 100
val crossoverprob = 0.9
val mutationprob = 0.1
val elitism = 1
val maxiter = 100val tspresult = tsp(distanceMatrix,
popsize,
crossoverprob,
mutationprob,
elitism,
maxiter)val route = tspresult.route
val cost = tspresult.cost
Console.println(Console.YELLOW + "The Random Key Genetic Algorithm - Example")
Console.println(Console.RED + "===========================================")
Console.println(Console.GREEN + "Distance Matrix:")
distanceMatrix.foreach(row => Console.println(Console.MAGENTA + row.mkString(" ")))
Console.println(Console.GREEN + s"Route: $route")
Console.println(Console.GREEN + s"Cost: $cost")
```The output is
```bash
#> sbt run
sbt:rkga> run
[info] running hello
The Random Key Genetic Algorithm - Example
===========================================
Distance Matrix:
0.0 1.0 2.0 3.0 4.0
1.0 0.0 1.0 2.0 3.0
2.0 1.0 0.0 1.0 2.0
3.0 2.0 1.0 0.0 1.0
4.0 3.0 2.0 1.0 0.0
Route: List(0, 4, 3, 2, 1)
Cost: 8.0
[success] Total time: 0 s, completed Sep 26, 2024, 10:42:12 PM
```