Serializable classes with subclasses

80 views Asked by At

I'm trying to build a selector in my main method:

fun main() {
    val gen = TestGenerator()
    val testSet = listOf(
        TestSpec("Test-1", SCENARIO, when (ALGORITHM) {
            AlgorithmType.MAXPLUS -> MaxPlusSpec(MIN_REWARD, MAX_REWARD, MAX_ITER, THRESHOLD, COMPUTATION_TYPE)
            AlgorithmType.MCTS -> MCTSSpec(MIN_REWARD, MAX_REWARD, MAX_ITER, C_VALUE, COMPUTATION_TYPE)
        })
    )
    gen.runLinearTestSet(testSet)
}

So, when either maxplus or mcts is selected with either centralized or decentralized computation, we want to pass the right parameters according to it.

I tried solving it like this:

@Serializable
data class MaxPlusSpec(
    val minReward: Float,
    val maxReward: Float,
    val maxIterationNumber: Int,
    val convergenceThreshold: Float,
    computationType: ComputationType
) : AlgorithmSpec(AlgorithmType.MAXPLUS, computationType)

@Serializable
data class MCTSSpec(
    val minReward: Float,
    val maxReward: Float,
    val maxIterationNumber: Int,
    val cValue: Float,
    computationType: ComputationType
) : AlgorithmSpec(AlgorithmType.MCTS, computationType)

Which throws errors:

'computationType' hides member of supertype 'AlgorithmSpec' and needs 'override' modifier

Serializable class has duplicate serial name of property 'computationType', either in the class itself or its supertypes

So I tried implementing it like this:

@Serializable
data class MaxPlusSpec(
    val minReward: Float,
    val maxReward: Float,
    val maxIterationNumber: Int,
    val convergenceThreshold: Float,
    computationType: ComputationType
) : AlgorithmSpec(AlgorithmType.MAXPLUS, computationType)

@Serializable
data class MCTSSpec(
    val minReward: Float,
    val maxReward: Float,
    val maxIterationNumber: Int,
    val cValue: Float,
    computationType: ComputationType
) : AlgorithmSpec(AlgorithmType.MCTS, computationType)

Which of course does not work because we can not assign computationtype without var/val keyword in data class.

I don't know how to make this work. Any help would be much appreciated.

Edit:

Currently went with this workaround, which isn't optimal but does work for now:

    @Serializable
abstract class AlgorithmSpec(
    val algorithmType: AlgorithmType
) {
    abstract val computationType: ComputationType
}

@Serializable
data class MaxPlusSpec(
    val minReward: Float,
    val maxReward: Float,
    val maxIterationNumber: Int,
    val convergenceThreshold: Float,
    override val computationType: ComputationType
) : AlgorithmSpec(AlgorithmType.MAXPLUS)

@Serializable
data class MCTSSpec(
    val minReward: Float,
    val maxReward: Float,
    val maxIterationNumber: Int,
    val cValue: Float,
    override val computationType: ComputationType
) : AlgorithmSpec(AlgorithmType.MCTS)
1

There are 1 answers

0
MbeezyP On BEST ANSWER

Solved as follows:

@Serializable
sealed class AlgorithmSpec(
    val algorithmType: AlgorithmType
) {
    abstract val computationType: ComputationType
}

@Serializable
data class MaxPlusSpec(
    val minReward: Float,
    val maxReward: Float,
    val maxIterationNumber: Int,
    val convergenceThreshold: Float,
    override val computationType: ComputationType
) : AlgorithmSpec(AlgorithmType.MAXPLUS)

@Serializable
data class MCTSSpec(
    val minReward: Float,
    val maxReward: Float,
    val maxIterationNumber: Int,
    val cValue: Float,
    override val computationType: ComputationType
) : AlgorithmSpec(AlgorithmType.MCTS)