Created
May 16, 2018 12:42
-
-
Save Goblin80/d5f772cf481f05b5056393691186eccf to your computer and use it in GitHub Desktop.
[MATLAB] A fitness function for a 4x4 sudoku using genetic algorithm
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
function score = sudoku(x) | |
x = reshape(round(x), 4, 4); | |
x(1,1) = 1; | |
x(1,4) = 2; | |
x(2,3) = 1; | |
x(3,2) = 3; | |
x(4,1) = 4; | |
x(4,4) = 3; | |
score = length(x(x < 1)) + length(x(x > 4)); | |
for i = 1:4 | |
score = score + 8 - length(unique(x(i, :))) - length(unique(x(:, i))); | |
end | |
if ~score | |
clc | |
x | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment