Skip to content

Instantly share code, notes, and snippets.

View VizGhar's full-sized avatar

Ján Kandráč VizGhar

  • Bratislava, Slovakia
View GitHub Profile
@VizGhar
VizGhar / dijkstra.kt
Created October 10, 2022 10:08
Dijkstra implementation in Kotlin language
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)),
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
@VizGhar
VizGhar / nqueens_tabusearch.kt
Created June 20, 2022 14:56
N-queens problem solved using TabuSearch Algorithm
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++
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.*
@VizGhar
VizGhar / Game.java
Created April 26, 2022 09:16
LibGDX + Box2D colissions + mapping sprite to game
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;
@VizGhar
VizGhar / Game.java
Created April 25, 2022 10:17
libGDX - falling square
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 {
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.*
@VizGhar
VizGhar / ga-sin.kt
Last active March 25, 2022 20:45
Geneticky algoritmus pre vypocet sinusoidy v jazyku C++
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
@VizGhar
VizGhar / n-queens-problem-genetic_algorithm.kt
Created March 20, 2022 13:31
Solution for 8 (or n) queen problem solved using genetic algorithm
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())
@VizGhar
VizGhar / ts.kt
Created March 8, 2022 07:13
"Hello World!" Simplistic Tabu Search Algorithm. This sample is meant to be readable and is not efficient at all, so !!! Do not use in production !!!
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)