void MainWindow::drawPath(QItemSelection item){ QCPCurve *curve = qobject_cast<QCPCurve*>(ui->citiesPlot->plottable(0)); curve->clearData(); curve->setLineStyle(QCPCurve::lsLine); ui->citiesPlot->replot(); QModelIndex index = item.indexes().first(); Tour tour = qvariant_cast<Tour>(ui->listView->model()->data(index, Qt::UserRole)); for(int i=0; i<tour.tourSize(); i++){ int x = tour.getCity(i).getX(); int y = tour.getCity(i).getY(); curve->addData(x, y); } curve->addData(tour.getCity(0).getX(), tour.getCity(0).getY()); ui->citiesPlot->replot(); }
Tour * TourGA::crossover (Tour * parent1, Tour * parent2) { Tour * child = new Tour(); int startPos = (int)rand() % parent1->tourLength(); int endPos = (int)rand() % parent1->tourLength(); for (int index = 0; index < child->tourLength(); index++) { if ((startPos < endPos) && (index > startPos) && (index < endPos)) { child->setCity(index, parent1->getCity(index)); } else if (startPos > endPos) { if (!((index < startPos) && (index > endPos))) { child->setCity (index, parent1->getCity(index)); } } } for (int index = 0; index < parent2->tourLength(); index++) { if (!child->containsCity(parent2->getCity(index))) { for (int innerLoop = 0; innerLoop < child->tourLength(); innerLoop++) { if (child->getCity(innerLoop) == NULL) { child->setCity(innerLoop, parent2->getCity(index)); break; } } } } return child; }