|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "math" |
| 5 | + "math/rand" |
| 6 | + |
| 7 | + "github.com/frickiericker/learn-go/07-magic_square/matrix" |
| 8 | + "github.com/frickiericker/learn-go/07-magic_square/seq" |
| 9 | +) |
| 10 | + |
| 11 | +type MagicSquareSolver struct { |
| 12 | + square *matrix.Square |
| 13 | + magic float64 |
| 14 | + rnd *rand.Rand |
| 15 | +} |
| 16 | + |
| 17 | +func NewMagicSquareSolver(size int, rnd *rand.Rand) *MagicSquareSolver { |
| 18 | + square := matrix.NewSquare(size) |
| 19 | + for i := range square.Data() { |
| 20 | + square.Data()[i] = float64(i + 1) |
| 21 | + } |
| 22 | + return &MagicSquareSolver{ |
| 23 | + square: square, |
| 24 | + magic: float64(magicConstant(size)), |
| 25 | + rnd: rnd, |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +func (solver *MagicSquareSolver) Get() *matrix.Square { |
| 30 | + return solver.square |
| 31 | +} |
| 32 | + |
| 33 | +func (solver *MagicSquareSolver) Randomize() { |
| 34 | + seq.Shuffle(seq.NewFloat64Slice(solver.square.Data()), solver.rnd) |
| 35 | +} |
| 36 | + |
| 37 | +func (solver *MagicSquareSolver) evaluate() float64 { |
| 38 | + loss := float64(0) |
| 39 | + magic := solver.magic |
| 40 | + for i := 0; i < solver.square.Size(); i++ { |
| 41 | + loss += math.Abs(sumRow(solver.square, i) - magic) |
| 42 | + loss += math.Abs(sumCol(solver.square, i) - magic) |
| 43 | + } |
| 44 | + loss += math.Abs(sumDiag(solver.square) - magic) |
| 45 | + loss += math.Abs(sumAntidiag(solver.square) - magic) |
| 46 | + return loss |
| 47 | +} |
| 48 | + |
| 49 | +func (solver *MagicSquareSolver) SearchNeighbor() float64 { |
| 50 | + n := solver.square.Size() |
| 51 | + data := solver.square.Data() |
| 52 | + c1 := solver.rnd.Intn(n * n) |
| 53 | + c2 := solver.rnd.Intn(n * n) |
| 54 | + |
| 55 | + lossBefore := solver.evaluate() |
| 56 | + data[c1], data[c2] = data[c2], data[c1] |
| 57 | + lossAfter := solver.evaluate() |
| 58 | + if lossAfter - 3 > lossBefore { |
| 59 | + data[c1], data[c2] = data[c2], data[c1] |
| 60 | + return lossBefore |
| 61 | + } |
| 62 | + return lossAfter |
| 63 | +} |
| 64 | + |
| 65 | +func sumRow(square *matrix.Square, row int) float64 { |
| 66 | + sum := float64(0) |
| 67 | + for col := 0; col < square.Cols(); col++ { |
| 68 | + sum += square.Get(row, col) |
| 69 | + } |
| 70 | + return sum |
| 71 | +} |
| 72 | + |
| 73 | +func sumCol(square *matrix.Square, col int) float64 { |
| 74 | + sum := float64(0) |
| 75 | + for row := 0; row < square.Rows(); row++ { |
| 76 | + sum += square.Get(row, col) |
| 77 | + } |
| 78 | + return sum |
| 79 | +} |
| 80 | + |
| 81 | +func sumDiag(square *matrix.Square) float64 { |
| 82 | + sum := float64(0) |
| 83 | + for i := 0; i < square.Size(); i++ { |
| 84 | + sum += square.Get(i, i) |
| 85 | + } |
| 86 | + return sum |
| 87 | +} |
| 88 | + |
| 89 | +func sumAntidiag(square *matrix.Square) float64 { |
| 90 | + sum := float64(0) |
| 91 | + size := square.Size() |
| 92 | + for i := 0; i < size; i++ { |
| 93 | + sum += square.Get(i, size-i-1) |
| 94 | + } |
| 95 | + return sum |
| 96 | +} |
| 97 | + |
| 98 | +func magicConstant(n int) int { |
| 99 | + return n * (n*n + 1) / 2 |
| 100 | +} |
0 commit comments