From b503ed6070c054bb9d8312fc88bd4b26e868da85 Mon Sep 17 00:00:00 2001 From: ajnebro Date: Thu, 28 Nov 2024 11:28:19 +0100 Subject: [PATCH] Update notebook --- examples/SMPSOWithObserver.jl | 2 +- .../NSGA-II solving multiobjective TSP.ipynb | 119 ++++++++++++++++++ 2 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 notebooks/NSGA-II solving multiobjective TSP.ipynb diff --git a/examples/SMPSOWithObserver.jl b/examples/SMPSOWithObserver.jl index 05e606c..893a0ab 100644 --- a/examples/SMPSOWithObserver.jl +++ b/examples/SMPSOWithObserver.jl @@ -41,7 +41,7 @@ function main() #solver.velocityUpdate = DefaultVelocityUpdate(c1Min, c1Max, c2Min, c2Max) observer = FrontPlotObserver(5000, name(problem), readFrontFromCSVFile("data/referenceFronts/ZDT6.csv")) - register!(getObservable(solver), observer) + register!(observable(solver), observer) optimize!(solver) diff --git a/notebooks/NSGA-II solving multiobjective TSP.ipynb b/notebooks/NSGA-II solving multiobjective TSP.ipynb new file mode 100644 index 0000000..1f26acd --- /dev/null +++ b/notebooks/NSGA-II solving multiobjective TSP.ipynb @@ -0,0 +1,119 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "94d16fd5", + "metadata": {}, + "source": [ + "# NSGA-II solving multi-objective TSP\n", + "\n", + "This notebook shows how to use NSGA-II to solve a bi-objective TSP problem." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ebc4e903", + "metadata": {}, + "outputs": [], + "source": [ + "using MetaJul\n", + "\n", + "tsp_data_dir = joinpath(@__DIR__, \"..\", \"resources/tspDataFiles\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f45c2dcd", + "metadata": { + "scrolled": true + }, + "outputs": [], + "source": [ + "problem100Cities = multiObjectiveTSP(\"kroAB100\", [joinpath(tsp_data_dir, \"kroA100.tsp\"), joinpath(tsp_data_dir, \"kroB100.tsp\")])" + ] + }, + { + "cell_type": "markdown", + "id": "615b7de1", + "metadata": {}, + "source": [ + "## Standard NSGA-II configured with default settings" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "bef9eff3", + "metadata": {}, + "outputs": [], + "source": [ + "solver::NSGAII = NSGAII(problem)" + ] + }, + { + "cell_type": "markdown", + "id": "479b963e", + "metadata": {}, + "source": [ + "### Run the algorithm" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d5445a96", + "metadata": {}, + "outputs": [], + "source": [ + "optimize!(solver) ;\n", + "\n", + "println(\"Computing time: \", computingTime(solver))" + ] + }, + { + "cell_type": "markdown", + "id": "545b54c9", + "metadata": {}, + "source": [ + "### Print the results" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "83b0fd8f", + "metadata": {}, + "outputs": [], + "source": [ + "using Plots\n", + " \n", + "#data to plot\n", + "x = [solution.objectives[1] for solution in foundSolutions(solver)];\n", + "y = [solution.objectives[2] for solution in foundSolutions(solver)];\n", + " \n", + "gr();\n", + " \n", + "scatter(x, y, title = \"Pareto front approximation\", label = \"Solutions\")\n", + "xlabel!(\"First objective\")\n", + "ylabel!(\"Second objective\")" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Julia 1.11.0", + "language": "julia", + "name": "julia-1.11" + }, + "language_info": { + "file_extension": ".jl", + "mimetype": "application/julia", + "name": "julia", + "version": "1.11.0" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}