diff --git a/notebooks/Defining problems.ipynb b/notebooks/Defining problems.ipynb index 460dc0a..0ea1c68 100644 --- a/notebooks/Defining problems.ipynb +++ b/notebooks/Defining problems.ipynb @@ -81,7 +81,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "44759988", "metadata": {}, "outputs": [ @@ -89,12 +89,12 @@ "name": "stdout", "output_type": "stream", "text": [ - "ContinuousProblem{Real}(Bounds{Real}[Bounds{Real}(-1000.0, 1000.0)], Function[var\"#222#223\"(), var\"#224#225\"()], Function[], \"Schaffer\")\n" + "ContinuousProblem{Real}(Bounds{Real}[Bounds{Real}(-1000.0, 1000.0)], Function[var\"#9#10\"(), var\"#11#12\"()], Function[], \"Schaffer\")\n" ] } ], "source": [ - "include(\"../src/continuousProblem.jl\")\n", + "using metajul\n", "\n", "schaffer = ContinuousProblem{Real}(\"Schaffer\")\n", "\n", @@ -165,7 +165,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "id": "d496dfdc", "metadata": {}, "outputs": [ @@ -333,7 +333,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "id": "00ca6da7", "metadata": {}, "outputs": [], @@ -342,15 +342,21 @@ ], "metadata": { "kernelspec": { - "display_name": "Julia 1.8.0", - "language": "julia", - "name": "julia-1.8" + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" }, "language_info": { - "file_extension": ".jl", - "mimetype": "application/julia", - "name": "julia", - "version": "1.8.0" + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.5" } }, "nbformat": 4, diff --git a/src/util/comparator.jl b/src/util/comparator.jl index fcbcf5a..0adb05c 100644 --- a/src/util/comparator.jl +++ b/src/util/comparator.jl @@ -28,29 +28,30 @@ Compare two numerics vectors `x` and `y` according to the dominance relationship function compareForDominance(x::Vector{T}, y::Vector{T})::Int where {T<:Number} @assert length(x) == length(y) "The vectors have a different length" - bestIsSolution1 = false - bestIsSolution2 = false + bestIsSolution1 = 0 + bestIsSolution2 = 0 - @inbounds for i in 1:length(x) + for i in 1:length(x) + if x[i] != y[i] if x[i] < y[i] - bestIsSolution1 = true - elseif x[i] > y[i] - bestIsSolution2 = true + bestIsSolution1 = 1 end - - # Early exit if both flags are true - if bestIsSolution1 && bestIsSolution2 - return 0 + if x[i] > y[i] + bestIsSolution2 = 1 end + end end - if bestIsSolution1 - return -1 - elseif bestIsSolution2 - return 1 + result = 0 + if bestIsSolution2 < bestIsSolution1 + result = -1 + elseif bestIsSolution2 > bestIsSolution1 + result = 1 else - return 0 + result = 0 end + + return result end function compareForDominance(solution1::Solution, solution2::Solution)::Int diff --git a/test/util/comparatorTest.jl b/test/util/comparatorTest.jl index 40d8aba..70714e5 100644 --- a/test/util/comparatorTest.jl +++ b/test/util/comparatorTest.jl @@ -17,6 +17,10 @@ end @test compareForDominance([2.0], [1.0]) == 1 @test compareForDominance([1.0, 2.0], [1.0, 1.0]) == 1 + @test compareForDominance([2.0, 2.0], [2.0, 1.0]) == 1 + @test compareForDominance([1.0, 2.0], [2.0, 1.0]) == 0 + @test compareForDominance([1.0, 2.0], [1.0, 2.0]) == 0 + @test compareForDominance([1.0, 1.0], [1.0, 2.0]) == -1 @test compareForDominance([1.0,2.0,3.1], [1.0,2.0,3.1]) == 0