tools/hhtracer/tracer.cpp
author unC0Rr
Sun, 12 Jan 2025 22:48:47 +0100
changeset 16085 9ad74696ddec
parent 16084 2d65bd46c92f
permissions -rw-r--r--
Add some progress on hhtracer
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16084
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
     1
#include "tracer.h"
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
     2
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
     3
#include <QRandomGenerator>
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
     4
#include <QSvgGenerator>
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
     5
16085
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
     6
Tracer::Tracer(QObject *parent)
16084
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
     7
    : QObject{parent},
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
     8
      palette_{{Qt::black,
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
     9
                Qt::white,
16085
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
    10
                {"#9f086e"},
16084
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
    11
                {"#f29ce7"},
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
    12
                {"#54a2fa"},
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
    13
                {"#2c78d2"}}} {}
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
    14
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
    15
QList<QColor> Tracer::palette() const { return palette_; }
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
    16
16085
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
    17
void Tracer::setPalette(const QList<QColor> &newPalette) {
16084
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
    18
  if (palette_ == newPalette) return;
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
    19
  palette_ = newPalette;
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
    20
  emit paletteChanged();
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
    21
}
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
    22
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
    23
double Tracer::bestSolution() const { return bestSolution_; }
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
    24
16085
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
    25
void Tracer::start(const QString &fileName) {
16084
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
    26
  qDebug() << "Starting using" << fileName;
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
    27
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
    28
  bestSolution_ = 0;
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
    29
  solutions_.clear();
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
    30
  generation_.clear();
16085
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
    31
  referenceImage_ = QImage{};
16084
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
    32
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
    33
  if (palette_.isEmpty()) {
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
    34
    qDebug("Empty palette");
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
    35
    return;
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
    36
  }
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
    37
16085
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
    38
  referenceImage_.load(QUrl(fileName).toLocalFile());
16084
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
    39
16085
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
    40
  if (referenceImage_.isNull()) {
16084
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
    41
    qDebug("Failed to load image");
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
    42
    return;
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
    43
  }
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
    44
16085
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
    45
  for (int i = 0; i < 600; ++i) {
16084
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
    46
    generation_.append(Solution{{32, 32}, palette_});
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
    47
  }
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
    48
}
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
    49
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
    50
void Tracer::step() {
16085
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
    51
  const auto size = generation_.size();
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
    52
  const auto keepSize = 10;
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
    53
  const auto replaceSize = 50;
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
    54
  const auto kept = generation_.mid(0, keepSize);
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
    55
  generation_ = generation_.mid(0, size - replaceSize);
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
    56
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
    57
  for (int i = 0; i < replaceSize; ++i) {
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
    58
    generation_.append(Solution{{32, 32}, palette_});
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
    59
  }
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
    60
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
    61
  auto rg = QRandomGenerator::global();
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
    62
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
    63
  for (qsizetype i = 0; i < size; i += 4) {
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
    64
    const auto first = rg->bounded(size);
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
    65
    const auto second = rg->bounded(size);
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
    66
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
    67
    if (first != second) {
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
    68
      generation_[first].crossover(generation_[second]);
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
    69
    }
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
    70
  }
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
    71
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
    72
  std::for_each(std::begin(generation_), std::end(generation_),
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
    73
                [this](auto &s) { s.mutate(palette_); });
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
    74
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
    75
  std::for_each(std::begin(solutions_), std::end(solutions_),
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
    76
                [this](const auto &fn) { QFile::remove(fn); });
16084
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
    77
  solutions_.clear();
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
    78
16085
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
    79
  generation_.append(kept);
16084
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
    80
16085
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
    81
  for (auto &solution : generation_) {
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
    82
    solution.render(newFileName());
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
    83
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
    84
    solution.calculateFitness(referenceImage_);
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
    85
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
    86
    solution.fitness += solution.cost() * 100;
16084
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
    87
  }
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
    88
16085
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
    89
  std::sort(std::begin(generation_), std::end(generation_),
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
    90
            [](const auto &a, const auto &b) { return a.fitness < b.fitness; });
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
    91
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
    92
  std::for_each(std::begin(generation_) + size, std::end(generation_),
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
    93
                [](const auto &s) { QFile::remove(s.fileName); });
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
    94
  generation_.remove(size, kept.size());
16084
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
    95
16085
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
    96
  bestSolution_ = generation_[0].fitness;
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
    97
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
    98
  std::transform(std::begin(generation_), std::end(generation_),
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
    99
                 std::back_inserter(solutions_),
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   100
                 [](const auto &a) { return a.fileName; });
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   101
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   102
  emit bestSolutionChanged();
16084
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   103
  emit solutionsChanged();
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   104
}
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   105
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   106
QStringList Tracer::solutions() const { return solutions_; }
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   107
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   108
QString Tracer::newFileName() {
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   109
  static qlonglong counter{0};
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   110
  counter += 1;
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   111
  return tempDir_.filePath(
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   112
      QStringLiteral("hedgehog_%1.svg").arg(counter, 3, 32, QChar(u'_')));
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   113
}
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   114
16085
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   115
Solution::Solution(QSizeF size, const QList<QColor> &palette) : size{size} {
16084
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   116
  fitness = 0;
16085
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   117
  primitives = {Primitive(size, palette), Primitive(size, palette)};
16084
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   118
}
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   119
16085
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   120
void Solution::calculateFitness(const QImage &target) {
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   121
  QImage candidate{fileName};
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   122
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   123
  if (candidate.isNull()) {
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   124
    fitness = 1e32;
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   125
    return;
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   126
  }
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   127
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   128
  // Both images assumed same size, same format
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   129
  double diffSum = 0;
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   130
  int width = target.width();
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   131
  int height = target.height();
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   132
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   133
  for (int y = 0; y < height; ++y) {
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   134
    auto candScan = reinterpret_cast<const QRgb *>(candidate.scanLine(y));
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   135
    auto targScan = reinterpret_cast<const QRgb *>(target.scanLine(y));
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   136
    for (int x = 0; x < width; ++x) {
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   137
      // Compare RGBA channels
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   138
      const QRgb cPix = candScan[x];
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   139
      const QRgb tPix = targScan[x];
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   140
      // const auto ca = qAlpha(cPix) / 255.0;
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   141
      const auto ta = qAlpha(tPix) / 255.0;
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   142
      const auto dr = qRed(cPix) - qRed(tPix);
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   143
      const auto dg = qGreen(cPix) - qGreen(tPix);
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   144
      const auto db = qBlue(cPix) - qBlue(tPix);
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   145
      const auto da = qAlpha(cPix) - qAlpha(tPix);
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   146
      diffSum +=
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   147
          qMax(qMax(qMax(dr * dr, dg * dg), db * db) * ta, da * da * 1.0);
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   148
    }
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   149
  }
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   150
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   151
  fitness = diffSum;
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   152
}
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   153
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   154
void Solution::render(const QString &fileName) {
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   155
  this->fileName = fileName;
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   156
16084
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   157
  const auto imageSize = size.toSize();
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   158
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   159
  QSvgGenerator generator;
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   160
  generator.setFileName(fileName);
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   161
  generator.setSize(imageSize);
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   162
  generator.setViewBox(QRect(0, 0, imageSize.width(), imageSize.height()));
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   163
  generator.setTitle("Hedgehog");
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   164
  generator.setDescription("Approximation of a target image using primitives");
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   165
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   166
  QPainter painter;
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   167
  painter.begin(&generator);
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   168
  painter.setRenderHint(QPainter::Antialiasing, true);
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   169
16085
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   170
  for (const auto &primitive : primitives) {
16084
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   171
    painter.setPen(primitive.pen);
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   172
    painter.setBrush(primitive.brush);
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   173
    painter.resetTransform();
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   174
    painter.translate(primitive.origin);
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   175
    painter.rotate(primitive.rotation);
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   176
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   177
    switch (primitive.type) {
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   178
      case Polygon: {
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   179
        QPolygonF polygon;
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   180
        polygon.append({0, 0});
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   181
        polygon.append(primitive.points);
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   182
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   183
        painter.drawPolygon(polygon);
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   184
        break;
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   185
      }
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   186
      case Circle:
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   187
        painter.drawEllipse({0, 0}, primitive.radius1, primitive.radius2);
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   188
        break;
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   189
    }
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   190
  }
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   191
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   192
  painter.end();
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   193
}
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   194
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   195
double Solution::cost() const {
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   196
  return std::accumulate(primitives.constBegin(), primitives.constEnd(), 0,
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   197
                         [](auto a, auto p) { return a + p.cost(); });
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   198
}
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   199
16085
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   200
void Solution::mutate(const QList<QColor> &palette) {
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   201
  if (primitives.isEmpty()) {
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   202
    return;
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   203
  }
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   204
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   205
  auto rg = QRandomGenerator::global();
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   206
  double mutationRate = 0.05;
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   207
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   208
  if (rg->bounded(1.0) > mutationRate) {
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   209
    return;
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   210
  }
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   211
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   212
  for (auto &prim : primitives) {
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   213
    // Pen width
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   214
    if (rg->bounded(1.0) < mutationRate) {
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   215
      prim.pen.setWidthF(prim.pen.widthF() * (rg->bounded(1.5) + 0.5) + 0.05);
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   216
    }
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   217
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   218
    // Origin
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   219
    if (rg->bounded(1.0) < mutationRate) {
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   220
      prim.origin += QPointF(rg->bounded(10.0) - 5.0, rg->bounded(10.0) - 5.0);
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   221
    }
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   222
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   223
    if (prim.type == Polygon) {
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   224
      // Points
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   225
      for (auto &pt : prim.points) {
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   226
        if (rg->bounded(1.0) < mutationRate) {
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   227
          prim.origin +=
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   228
              QPointF(rg->bounded(10.0) - 5.0, rg->bounded(10.0) - 5.0);
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   229
        }
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   230
      }
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   231
    } else {  // Circle/ellipse
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   232
      if (rg->bounded(1.0) < mutationRate) {
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   233
        prim.radius1 *= rg->bounded(0.4) + 0.8;
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   234
      }
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   235
      if (rg->bounded(1.0) < mutationRate) {
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   236
        prim.radius2 *= rg->bounded(0.4) + 0.8;
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   237
      }
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   238
      if (rg->bounded(1.0) < mutationRate) {
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   239
        prim.rotation = rg->bounded(90.0);
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   240
      }
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   241
    }
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   242
  }
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   243
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   244
  if (rg->bounded(1.0) < mutationRate) {
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   245
    auto i = rg->bounded(primitives.size());
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   246
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   247
    Primitive p{size, palette};
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   248
    primitives.insert(i, p);
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   249
  }
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   250
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   251
  if (rg->bounded(1.0) < mutationRate) {
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   252
    auto i = rg->bounded(primitives.size());
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   253
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   254
    primitives.remove(i);
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   255
  }
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   256
}
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   257
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   258
void Solution::crossover(Solution &other) {
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   259
  const auto n = qMin(primitives.size(), other.primitives.size());
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   260
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   261
  auto rg = QRandomGenerator::global();
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   262
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   263
  if (rg->bounded(1.0) < 0.02) {
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   264
    if (n <= 1) {
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   265
      return;
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   266
    }
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   267
    // swap tails
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   268
    const auto cp = rg->bounded(1, primitives.size());
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   269
    const auto ocp = rg->bounded(1, other.primitives.size());
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   270
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   271
    const auto tail = primitives.mid(cp);
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   272
    const auto otherTail = other.primitives.mid(ocp);
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   273
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   274
    primitives.remove(cp, primitives.size() - cp);
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   275
    other.primitives.remove(ocp, other.primitives.size() - ocp);
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   276
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   277
    primitives.append(otherTail);
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   278
    other.primitives.append(tail);
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   279
  } else {
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   280
    if (n < 1) {
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   281
      return;
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   282
    }
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   283
    // swap one element
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   284
    const auto cp = rg->bounded(primitives.size());
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   285
    const auto ocp = rg->bounded(other.primitives.size());
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   286
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   287
    qSwap(primitives[cp], other.primitives[ocp]);
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   288
  }
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   289
}
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   290
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   291
Primitive::Primitive(QSizeF size, const QList<QColor> &palette) {
16084
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   292
  auto rg = QRandomGenerator::global();
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   293
  auto randomPoint = [&]() -> QPointF {
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   294
    return {rg->bounded(size.width()), rg->bounded(size.height())};
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   295
  };
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   296
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   297
  if (rg->bounded(2) == 0) {
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   298
    type = Polygon;
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   299
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   300
    points.append(randomPoint());
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   301
    points.append(randomPoint());
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   302
  } else {
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   303
    type = Circle;
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   304
16085
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   305
    radius1 = rg->bounded(size.width() * 0.2) + 2;
9ad74696ddec Add some progress on hhtracer
unC0Rr
parents: 16084
diff changeset
   306
    radius2 = rg->bounded(size.width() * 0.2) + 2;
16084
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   307
    rotation = rg->bounded(90);
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   308
  }
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   309
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   310
  pen = QPen(palette[rg->bounded(palette.length())]);
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   311
  pen.setWidthF(rg->bounded(size.width() * 0.1));
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   312
  brush = QBrush(palette[rg->bounded(palette.length())]);
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   313
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   314
  origin = randomPoint();
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   315
}
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   316
2d65bd46c92f Start work on hedgehog tracer
unC0Rr
parents:
diff changeset
   317
double Primitive::cost() const { return 1.0 + 0.1 * points.length(); }