I am trying to implement several algorithms that share a branch-and-bound setup on top, but have different sub problems. To do this, I decided to create abstract types for the sub problems and make the branch-and-bound type generic, but I get an error that I just do not understand at all.
Here is a minimal reproduction of the problem:
struct Dataset
end
struct Duals
end
# === ABSTRACT FACTORY TYPE ===
abstract type SubProblemFactory end
abstract type SubProblem end
function make_sub_problem(factory::SubProblemFactory,
dataset::Dataset,
duals::Duals)::SubProblem
error("make_sub_problem was not implemented")
end
# === CONCRETE IMPL ===
struct MySubProblemFactory <: SubProblemFactory
end
function make_sub_problem(factory::MySubProblemFactory,
dataset::Dataset,
duals::Duals)::MySubProblem
MySubProblem(dataset, duals)
end
mutable struct MySubProblem <: SubProblem
dataset :: Dataset
duals :: Duals
end
# === HOLDS A SUB PROBLEM FACTORY ===
struct BranchAndBound{F <: SubProblemFactory}
factory :: F
dataset :: Dataset
function BranchAndBound{F}(factory::F, dataset::Dataset) where F<:SubProblemFactory
new(factory, dataset)
end
end
function solve()
dataset = Dataset()
factory = MySubProblemFactory()
bb = BranchAndBound(factory, dataset)
end
solve()
ERROR: LoadError: MethodError: no method matching BranchAndBound(::MySubProblemFactory, ::Dataset)
Stacktrace:
[1] solve() at /tmp/test.jl:44
[2] top-level scope at /tmp/test.jl:47
in expression starting at /tmp/test.jl:47
Where have I gone wrong here? The method clearly exists, as the constructor is defined for any subtype of SubProblemFactory
, and MySubProblemFactory
is indeed defined to be a subtype of that.