Skip to content

Instantly share code, notes, and snippets.

@Goblin80
Created May 16, 2018 12:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Goblin80/d5f772cf481f05b5056393691186eccf to your computer and use it in GitHub Desktop.
Save Goblin80/d5f772cf481f05b5056393691186eccf to your computer and use it in GitHub Desktop.
[MATLAB] A fitness function for a 4x4 sudoku using genetic algorithm
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