Advertisement
Girafi_02

Untitled

May 8th, 2022
1,217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include <QDebug>
  4. #include <QPainter>
  5. #include <QGraphicsScene>
  6. #include <QGraphicsTextItem>
  7. #include <QMimeData>
  8. #include <QPoint>
  9. #include <QPointF>
  10. #include <QMouseEvent>
  11. #include <QRandomGenerator>
  12. #include <functional>
  13. #include <vector>
  14. #include <math.h>
  15. #include <QTimer>
  16. #include "QtTest/QTest"
  17.  
  18.  
  19. MainWindow::MainWindow(QWidget *parent)
  20.     : QMainWindow(parent)
  21.     , ui(new Ui::MainWindow)
  22. {
  23.     ui->setupUi(this);
  24.     ui->graphicsView->setSceneRect(this->frameGeometry());
  25.     QPixmap img("C:/Users/Girafi/Documents/untitled/sticker.png");
  26.     scene = new QGraphicsScene(this);
  27.     ui->graphicsView->setScene(scene);
  28.     ui->graphicsView->fitInView(scene->sceneRect());
  29.     QBrush redBrush(Qt::red);
  30.     QBrush blueBrush(Qt::blue);
  31.     QPen blackPen(Qt::black);
  32.     blackPen.setWidth(6);
  33.  
  34. }
  35.  
  36. MainWindow::~MainWindow()
  37. {
  38.     delete ui;
  39.     delete scene;
  40.     delete ellipse;
  41.     delete pixmap;
  42. }
  43.  
  44.  
  45. int firststart = 0;
  46. QVector <QPoint> points;
  47.  
  48. void MainWindow::mousePressEvent(QMouseEvent *event)
  49. {
  50.     QPoint remapped = ui->graphicsView->mapFromParent(event->pos());
  51.     if (ui->graphicsView->rect().contains(remapped) && event->button() == Qt::LeftButton)
  52.     {
  53.          QPointF mousePoint = ui->graphicsView->mapToScene(remapped);
  54.          points.push_back(mousePoint.toPoint());
  55.          MainWindow::updating_scene(mousePoint.toPoint());
  56.     }
  57.     ui->label->setText("Кількість точек: " + QString::number(points.size()));
  58. }
  59.  
  60. void MainWindow::justfortimer()
  61. {
  62.     qDebug() << "timer";
  63. }
  64.  
  65. void MainWindow::drawline(int a, int b)
  66. {
  67.     line = scene->addLine(points[a].x(), points[a].y(), points[b].x(), points[b].y(), QPen(Qt::black, 2)); //і намалювання палки
  68.     scene->update();
  69. }
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76. /*void MainWindow::paintEvent2(QPaintEvent *)
  77. {
  78.     if(firststart++) {
  79.         qDebug() << "paintevent";
  80.         QPainter p(this);
  81.         QPen pen;
  82.         pen.setColor(Qt::black);
  83.         pen.setWidth(50);
  84.         points.push_back(QWidget::mapFromGlobal(QCursor::pos()));
  85.         for(int i = 0; i < points.size(); i++) p.drawEllipse(points[i], 3, 3);
  86.     }
  87. }*/
  88.  
  89. void MainWindow::on_pushButton_clicked()
  90. {
  91.     ui->progressBar->setValue(0);
  92.     scene->clear();
  93.     points.clear();
  94.     ui->textEdit->clear();
  95.     qDebug() << "___________________________________________";
  96. }
  97.  
  98. void MainWindow::updating_scene(QVector<QPoint> points)
  99. {
  100.     scene->clear();
  101.     for (int i = 0; i < points.size(); i++) {
  102.         ellipse = scene->addEllipse(points[i].x() - 3, points[i].y() - 3, 6, 6, QPen(Qt::black), QBrush(Qt::red));
  103.     }
  104. }
  105.  
  106. void MainWindow::updating_scene(QPoint point)
  107. {
  108.     ellipse = scene->addEllipse(point.x() - 3, point.y() - 3, 6, 6, QPen(Qt::black), QBrush(Qt::red));
  109. }
  110.  
  111. void MainWindow::on_pushButton_2_clicked()
  112. {
  113.     ui->progressBar->setValue(0);
  114.     ui->progressBar->setMaximum((points.size()*2)*points.size());
  115.     ui->textEdit->clear(); //очистка поля виводу
  116.     QVector <QVector<double>> chance   (points.size(),QVector<double>(points.size()));// Створення основних носіїв данних
  117.     QVector <QVector<double>> pheromone(points.size(),QVector<double>(points.size()));
  118.     QVector <QVector<double>> closeness(points.size(),QVector<double>(points.size()));
  119.     QVector <QVector<double>> lenofways(points.size(),QVector<double>(points.size()));
  120.     QVector <QVector<int>> way(points.size(),QVector<int>(points.size() + 1));
  121.     int pow_for_len = 4, pow_for_pheromone = 1;
  122.     QVector <int> minimal_way (points.size() + 1);
  123.     int minimal_len_of_way;
  124.     for (int i = 0; i < points.size(); i++) {
  125.         for (int j = 0; j < points.size(); j++) { //запуск вложеного циклу що заповнить вектор "феромонів" на початкове значення, у моєму випадку 0.2, та порахує відстань та близкість між точками
  126.             pheromone[i][j] = 0.1;
  127.             double len = std::hypot(points[j].x() - points[i].x(), points[j].y() - points[i].y()); // формула знаходження відстані між точками і та j
  128.             closeness[i][j] = 200/len; // а тут вираховується близькість, значення, обернене до відсатні. Константа 200, а не 1, щоб були більш адекватні значення
  129.             lenofways[i][j] = len;
  130.             if (i == j) continue; //тут йде обрив ітерації щоб не виводити зайву інформацію, типу "відстань між точками 0 і 0"
  131.             //ui->textEdit->append("Растояние между точками " + QString::number(i) + " и " + QString::number(j) + ": " + QString::number(len));
  132.             //ui->textEdit->append("Близость: " + QString::number(closeness[i][j]));
  133.         }
  134.     }
  135.  
  136.     int count_of_iterations = 0;
  137.  
  138.     for (int count = 0; count < points.size()*2; count++) {
  139.         for (int index = 0; index < points.size(); index++) {
  140.             count_of_iterations++;
  141.             QVector <int> local_minimum_way;
  142.             int local_len_of_way;
  143.             way[index][0] = index;
  144.             way[index][points.size()] = index;
  145.             int i = index, max = 0;
  146.             //qDebug() << "index:" << index;
  147.             for (QVector <int> usual_variebles; usual_variebles.size() != points.size() - 1; max++) {
  148.                 double sum(0);
  149.                 int temp_chance[100] {};
  150.                 //qDebug() << "iteration" << max;
  151.                 //if (max > 100) exit(142);
  152.                 for (int j = 0; j < points.size(); j++) {
  153.                     if (i == j || usual_variebles.count(j) != 0) continue;
  154.                     sum = sum + pow(pheromone[i][j], pow_for_pheromone) * pow(closeness[i][j], pow_for_len);
  155.                 }
  156.                 for (int j = 0, last_ruletka = 0; j < points.size(); j++) {
  157.                     if (i == j || usual_variebles.count(j) != 0) continue;
  158.                     chance[i][j] = (pow(pheromone[i][j], pow_for_pheromone) * pow(closeness[i][j], pow_for_len)) / sum;
  159.                     //qDebug() << "Шанс перехода между" << i << "и" << j << (pheromone[i][j] * closeness[i][j])/sum;
  160.                     //ui->textEdit->append("Шанс перехода между " + QString::number(i) + " и " + QString::number(j) + " = " + QString::number((pheromone[i][j] * closeness[i][j])/sum));
  161.                     int t = ((chance[i][j]*100 + 0.5)/100)*100;
  162.                     //qDebug() << "t" << t;
  163.                     for (int z = last_ruletka; z < t + last_ruletka; z++) {
  164.                         //qDebug() << "j" << j << "z" << z;
  165.                         temp_chance[z] = j;
  166.                     }
  167.                     last_ruletka = t + last_ruletka;
  168.                     /*for (int z = 0, temp = 0; z < 100; z++) {
  169.                     if (z >= int((chance[i][temp]*10)/10*100) && temp != points.size() - 1) temp++;
  170.                     temp_chance[z] = temp;
  171.                 }*/
  172.                 }
  173.                 for (int z = 0; z < 100; z++) {
  174.                     //qDebug() << z << ':' << temp_chance[z];
  175.                 }
  176.                 int temp = QRandomGenerator::global()->bounded(0,100); //ось самий вибір напряму
  177.                 //qDebug() << "temp_chance[temp]" << temp_chance[temp];
  178.                 //ui->textEdit->append("temp_chance[temp] " + QString::number(temp_chance[temp]));
  179.                 if (std::find(usual_variebles.begin(), usual_variebles.end(), temp_chance[temp]) != usual_variebles.end() || i == temp_chance[temp] || index == temp_chance[temp]) continue;
  180.                 usual_variebles.append(i);
  181.                 MainWindow::drawline(i, temp_chance[temp]);
  182.                 //qDebug() << "Переход между" << i << "и" << temp_chance[temp];
  183.                 //ui->textEdit->append("Переход между " + QString::number(i) + " и " + QString::number(temp_chance[temp]));
  184.                 i = temp_chance[temp];
  185.                 way[index][usual_variebles.size()] = i;
  186.                 //QTest::qWait(1);
  187.  
  188.             }
  189.             //if (max == 0) local_len_of_way = ;
  190.             ui->progressBar->setValue(ui->progressBar->value() + 1);
  191.             MainWindow::drawline(i, index);
  192.             QTest::qWait(1);
  193.             MainWindow::updating_scene(points);
  194.         }
  195.  
  196.  
  197.         for (int i = 0; i < points.size(); i++) {
  198.             QString str_way {"Шлях #" + QString::number(i) + " був таким: "};
  199.             for (int j = 0; j < points.size() + 1; j++) {
  200.                 str_way.append(QString::number(way[i][j]) + ", ");
  201.             }
  202.             qDebug() << str_way;
  203.             //ui->textEdit->append(str_way);
  204.         }
  205.  
  206.         for (int i = 0; i < points.size(); i++) {
  207.             int len_of_way = 0;
  208.             for (int j = 0; j < points.size() - 1; j++) {
  209.                 len_of_way = len_of_way + lenofways[way[i][j]][way[i][j+1]];
  210.             }
  211.             qDebug() << "way[i].size() - 1" << way[i].size() - 1 << "lenofways[way[i][way[i].size() - 1]][0]" << lenofways[way[i][way[i].size() - 1]][0];
  212.             len_of_way = len_of_way + lenofways[way[i][way[i].size() - 1]][way[i][0]];
  213.             ui->textEdit->append("len of way # " + QString::number(i) + " = " + QString::number(len_of_way));
  214.             for (int j = 0; j < points.size() - 1; j++) {
  215.                 pheromone[way[i][j]][way[i][j + 1]] = pheromone[way[i][j]][way[i][j + 1]] + (double) 200/len_of_way;
  216.  
  217.             }
  218.             //qDebug() << "len_of_way #" << i << len_of_way;
  219.             if (count == 0 && i == 0) minimal_len_of_way = len_of_way;
  220.             if (len_of_way < minimal_len_of_way) {
  221.                 minimal_len_of_way = len_of_way;
  222.                 minimal_way = way[i];
  223.             }
  224.         }
  225.         //qDebug() << "count:" << count;
  226.         for (int i = 0; i < points.size(); i++) {
  227.             for (int j = 0; j < pheromone.size(); j++) {
  228.                 pheromone[i][j] = pheromone[i][j] * 0.66;
  229.                 //ui->textEdit->append("pheromone on way from " + QString::number(i) + " to " + QString::number(j) + " = " + QString::number(pheromone[i][j]));
  230.                 //qDebug() << "pheromone on way from" << i << "to" << j << '=' << pheromone[i][j];
  231.             }
  232.         }
  233.  
  234.     }
  235.  
  236.     ui->textEdit->append("Найкоротший шлях між точками = " + QString::number(minimal_len_of_way));
  237.     qDebug() << "Найкоротший шлях між точками =" << minimal_len_of_way;
  238.     QString str_way {"Цей шлях був такий: "};
  239.     for (int i = 0; i < minimal_way.size(); i++) {
  240.         str_way.append(QString::number(minimal_way[i]) + ", ");
  241.     }
  242.     ui->textEdit->append(str_way);
  243.     MainWindow::draw_way(minimal_way);
  244.     qDebug() << "count iter" << count_of_iterations;
  245. }
  246.  
  247.  
  248.  
  249. //scene->addPixmap(img);
  250. //img = new QPixmap("C:/Users/Girafi/Documents/untitled/sticker2.png");
  251. //QPixmap pixmap("C:/Users/Girafi/Documents/untitled/sticker2.png");
  252. //pixmap = new QPixmap("image.png");
  253. //pixmap = new QPixmap("C:/Users/Girafi/Documents/untitled/sticker2.png");
  254.  
  255. void MainWindow::on_pushButton_3_clicked()
  256. {
  257.     qDebug() << scene->height() << ui->graphicsView->pos();
  258.     scene->clear();
  259.     points.clear();
  260.     for (int i = 0; i < ui->spinBox->value(); i++) {
  261.         points.append(QPoint(QRandomGenerator::global()->bounded(ui->graphicsView->pos().x(), ui->graphicsView->width()), QRandomGenerator::global()->bounded(ui->graphicsView->pos().y(), ui->graphicsView->size().height())));
  262.         ellipse = scene->addEllipse(points[i].x(), points[i].y(), 6, 6, QPen(Qt::black), QBrush(Qt::red));
  263.  
  264.     }
  265.     line = scene->addLine(ui->graphicsView->pos().x(), ui->graphicsView->pos().y(), ui->graphicsView->pos().x() + ui->graphicsView->width(), ui->graphicsView->pos().y() + ui->graphicsView->height());
  266.  
  267. }
  268.  
  269. void MainWindow::draw_way(QVector <int> way)
  270. {
  271.     for (int i = 0; i < way.size() - 1; i++) {
  272.         MainWindow::drawline(way[i], way[i+1]);
  273.     }
  274. }
  275.  
  276.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement