This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private const val startPoint = "A" | |
| private const val finalPoint = "G" | |
| data class Vertex(val id: String, val length: Int) | |
| // adjacency list graph | |
| val graph = | |
| mapOf( | |
| "A" to listOf(Vertex("B", 3), Vertex("C", 5)), | |
| "B" to listOf(Vertex("A", 3), Vertex("D", 4), Vertex("E", 2)), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import kotlin.math.pow | |
| import kotlin.random.Random | |
| const val boardSize = 40 | |
| const val tries = 100 | |
| val random = Random(System.currentTimeMillis()) | |
| fun cost(solution: Array<Int>): Int { | |
| var cost = 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import kotlin.math.pow | |
| const val boardSize = 10 | |
| fun cost(solution: Array<Int>): Int { | |
| var cost = 0 | |
| for (i in 0 until boardSize) { | |
| for (j in (i + 1) until boardSize) { | |
| if (solution[i] == solution[j]) cost++ | |
| if (solution[i] == solution[j] + (j - i)) cost++ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.awt.Color | |
| import java.awt.Dimension | |
| import java.awt.Graphics | |
| import java.io.File | |
| import javax.imageio.ImageIO | |
| import javax.swing.BorderFactory | |
| import javax.swing.JFrame | |
| import javax.swing.JPanel | |
| import kotlin.math.* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package sk.ssnd; | |
| import com.badlogic.gdx.ApplicationAdapter; | |
| import com.badlogic.gdx.Gdx; | |
| import com.badlogic.gdx.Input; | |
| import com.badlogic.gdx.graphics.OrthographicCamera; | |
| import com.badlogic.gdx.graphics.Texture; | |
| import com.badlogic.gdx.graphics.g2d.Sprite; | |
| import com.badlogic.gdx.graphics.g2d.SpriteBatch; | |
| import com.badlogic.gdx.math.Vector2; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package sk.ssnd.platformer; | |
| import com.badlogic.gdx.ApplicationAdapter; | |
| import com.badlogic.gdx.Gdx; | |
| import com.badlogic.gdx.graphics.GL20; | |
| import com.badlogic.gdx.graphics.OrthographicCamera; | |
| import com.badlogic.gdx.math.Vector2; | |
| import com.badlogic.gdx.physics.box2d.*; | |
| public class Game extends ApplicationAdapter { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.awt.Color | |
| import java.awt.Dimension | |
| import java.awt.Graphics | |
| import java.io.File | |
| import javax.imageio.ImageIO | |
| import javax.swing.BorderFactory | |
| import javax.swing.JFrame | |
| import javax.swing.JPanel | |
| import kotlin.math.* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import kotlin.math.absoluteValue | |
| import kotlin.math.sin | |
| import kotlin.random.Random | |
| const val chromosomeSize = 100 | |
| const val mutationProbability = 0.05 // each block of chromosome mutates with this probability | |
| const val populationSize = 1000 // this many individuals will be in each generation | |
| const val keepNBest = 200 // this many best individuals will be taken without change to next gen | |
| const val tournamentSize = 3 // this many individuals will fight in tournament |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import kotlin.math.absoluteValue | |
| import kotlin.random.Random | |
| const val chromosomeSize = 8 | |
| const val mutationProbability = 0.05 // each block of chromosome mutates with this probability | |
| const val populationSize = 1000 // this many individuals will be in each generation | |
| const val keepNBest = 200 // this many best individuals will be taken without change to next gen | |
| const val tournamentSize = 3 // this many individuals will fight in tournament | |
| val random = Random(System.currentTimeMillis()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import kotlin.math.abs | |
| const val target = "Hello World!" | |
| const val possibilities = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789 .,!?" | |
| val String.fitness: Int get() = // lower fitness better (for this case) fitness of 0 = solution | |
| indices.sumOf { index -> | |
| val indexFirst = possibilities.indexOf(target[index]) | |
| val indexSecond = possibilities.indexOf(this[index]) | |
| val high = maxOf(indexFirst, indexSecond) |
NewerOlder