From 61e58e74e1d6043633fef3d54d1795c149095f71 Mon Sep 17 00:00:00 2001 From: Igor Machado Date: Thu, 7 Dec 2023 18:31:00 +0000 Subject: [PATCH] using Union instead of pipe, for python 3.9 --- demo/03_QuickstartTSP_VNS_BRKGA/dev-mainTSP-fcore-brkga.py | 4 ++-- .../03_QuickstartTSP_VNS_BRKGA/mainTSP-fcore-brkga-part3.py | 4 ++-- demo/03_QuickstartTSP_VNS_BRKGA/mainTSP-fcore-brkga.py | 4 ++-- optframe/engine.py | 4 +++- optframe/protocols.py | 6 +++--- 5 files changed, 12 insertions(+), 10 deletions(-) diff --git a/demo/03_QuickstartTSP_VNS_BRKGA/dev-mainTSP-fcore-brkga.py b/demo/03_QuickstartTSP_VNS_BRKGA/dev-mainTSP-fcore-brkga.py index f7e8ae4..5a086d2 100644 --- a/demo/03_QuickstartTSP_VNS_BRKGA/dev-mainTSP-fcore-brkga.py +++ b/demo/03_QuickstartTSP_VNS_BRKGA/dev-mainTSP-fcore-brkga.py @@ -187,7 +187,7 @@ def generateRK(problemCtx: ProblemContextTSP, ptr_array_double : LibArrayDouble) from optframe.core import LibArrayDouble -from typing import Tuple +from typing import Tuple, Union import ctypes @@ -217,7 +217,7 @@ def decodeSolution(pTSP: ProblemContextTSP, array_double : LibArrayDouble) -> So return sol @staticmethod - def decodeMinimize(pTSP: ProblemContextTSP, array_double : LibArrayDouble, needsSolution: bool) -> Tuple[SolutionTSP|None, float]: + def decodeMinimize(pTSP: ProblemContextTSP, array_double : LibArrayDouble, needsSolution: bool) -> Tuple[Union[SolutionTSP,None], float]: # # print("decodeMinimize! needsSolution="+str(needsSolution), flush=True) sol = DecoderTSP.decodeSolution(pTSP, array_double) diff --git a/demo/03_QuickstartTSP_VNS_BRKGA/mainTSP-fcore-brkga-part3.py b/demo/03_QuickstartTSP_VNS_BRKGA/mainTSP-fcore-brkga-part3.py index 192f04f..961d621 100644 --- a/demo/03_QuickstartTSP_VNS_BRKGA/mainTSP-fcore-brkga-part3.py +++ b/demo/03_QuickstartTSP_VNS_BRKGA/mainTSP-fcore-brkga-part3.py @@ -1,6 +1,6 @@ from optframe.core import LibArrayDouble -from typing import Tuple +from typing import Tuple, Union import ctypes @@ -30,7 +30,7 @@ def decodeSolution(pTSP: ProblemContextTSP, array_double : LibArrayDouble) -> So return sol @staticmethod - def decodeMinimize(pTSP: ProblemContextTSP, array_double : LibArrayDouble, needsSolution: bool) -> Tuple[SolutionTSP|None, float]: + def decodeMinimize(pTSP: ProblemContextTSP, array_double : LibArrayDouble, needsSolution: bool) -> Tuple[Union[SolutionTSP,None], float]: # # print("decodeMinimize! needsSolution="+str(needsSolution), flush=True) sol = DecoderTSP.decodeSolution(pTSP, array_double) diff --git a/demo/03_QuickstartTSP_VNS_BRKGA/mainTSP-fcore-brkga.py b/demo/03_QuickstartTSP_VNS_BRKGA/mainTSP-fcore-brkga.py index f964cb5..226c971 100644 --- a/demo/03_QuickstartTSP_VNS_BRKGA/mainTSP-fcore-brkga.py +++ b/demo/03_QuickstartTSP_VNS_BRKGA/mainTSP-fcore-brkga.py @@ -181,7 +181,7 @@ def generateRK(problemCtx: ProblemContextTSP, ptr_array_double : LibArrayDouble) from optframe.core import LibArrayDouble -from typing import Tuple +from typing import Tuple, Union import ctypes @@ -211,7 +211,7 @@ def decodeSolution(pTSP: ProblemContextTSP, array_double : LibArrayDouble) -> So return sol @staticmethod - def decodeMinimize(pTSP: ProblemContextTSP, array_double : LibArrayDouble, needsSolution: bool) -> Tuple[SolutionTSP|None, float]: + def decodeMinimize(pTSP: ProblemContextTSP, array_double : LibArrayDouble, needsSolution: bool) -> Tuple[Union[SolutionTSP,None], float]: # # print("decodeMinimize! needsSolution="+str(needsSolution), flush=True) sol = DecoderTSP.decodeSolution(pTSP, array_double) diff --git a/optframe/engine.py b/optframe/engine.py index 2bb2b02..67b02c5 100644 --- a/optframe/engine.py +++ b/optframe/engine.py @@ -643,7 +643,9 @@ def add_decoder_rk(self, problemCtx, decoder_rk_callback): self.callback_utils_decref_ptr) return IdDecoderRandomKeysNoEvaluation(idx_dec) - def add_edecoder_op_rk_class(self, p : XProblem, decoder_rk: Type[XDecoderRandomKeysMinimize|XDecoderRandomKeysMaximize]): + # decoder_rk: Type[XDecoderRandomKeysMinimize|XDecoderRandomKeysMaximize] + # cannot make Union here... think about python 3.9 (pipe only after 3.10) + def add_edecoder_op_rk_class(self, p : XProblem, decoder_rk: Union[XDecoderRandomKeysMinimize,XDecoderRandomKeysMaximize]): """ Add a XDecoderRandomKeys class to the engine. diff --git a/optframe/protocols.py b/optframe/protocols.py index 76ea4c2..8c59ae8 100644 --- a/optframe/protocols.py +++ b/optframe/protocols.py @@ -1,6 +1,6 @@ #!/usr/bin/python3 -from typing import Protocol, runtime_checkable, Tuple +from typing import Protocol, runtime_checkable, Tuple, Union # protocols should only depend on 'core' from optframe.core import SearchOutput @@ -95,13 +95,13 @@ def decodeSolution(p: XProblem, rk : LibArrayDouble) -> XSolution: @runtime_checkable class XDecoderRandomKeysMinimize(Protocol): @staticmethod - def decodeMinimize(p: XProblem, rk : LibArrayDouble, needsSolution: bool) -> Tuple[XSolution|None, float]: + def decodeMinimize(p: XProblem, rk : LibArrayDouble, needsSolution: bool) -> Tuple[Union[XSolution,None], float]: ... @runtime_checkable class XDecoderRandomKeysMaximize(Protocol): @staticmethod - def decodeMaximize(p: XProblem, rk : LibArrayDouble, needsSolution: bool) -> Tuple[XSolution|None, float]: + def decodeMaximize(p: XProblem, rk : LibArrayDouble, needsSolution: bool) -> Tuple[Union[XSolution,None], float]: ... @runtime_checkable