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 / ga.kt
Last active March 8, 2022 07:18
"Hello World!" Simplistic Genetic Algorithm with tournament selectionThis sample is meant to be readable and is not efficient at all, so!!! Do not use in production !!!
import kotlin.math.abs
import kotlin.random.Random
const val target = "Hello World!"
const val possibilities = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789 .,!?"
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 / sa.kt
Last active March 9, 2022 10:28
"Hello World!" Simplistic Simulated Annealing Algorithm.This sample is meant to be readable and is not efficient at all, so!!! Do not use in production !!!
import kotlin.math.abs
import kotlin.math.pow
import kotlin.random.Random
const val target = "Hello World!"
const val possibilities = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789 .,!?"
val random = Random(System.currentTimeMillis())
val String.fitness: Int get() = // lower fitness better (for this case) fitness of 0 = solution
@VizGhar
VizGhar / hc.kt
Last active March 8, 2022 07:14
"Hello World!"Simplistic Hill Climbing 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)
@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)
@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 / 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
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 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 {
@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;
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.*