Skip to content
This repository was archived by the owner on Jul 17, 2024. It is now read-only.

chore: Use long scores types instead of int scores #96

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions tests/test_constraint_streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,31 @@ def define_constraints(constraint_factory: ConstraintFactory):
assert len(justifications) == 0


def test_long_scores():
@constraint_provider
def define_constraints(constraint_factory: ConstraintFactory):
return [
constraint_factory.for_each(Entity)
.reward(SimpleScore.ONE, lambda e: e.value.number)
.as_constraint('Maximize value')
]

score_manager = create_score_manager(define_constraints)
entity_a: Entity = Entity('A')
entity_b: Entity = Entity('B')

# Overflow an int
value_1 = Value(3_000_000_000)
value_2 = Value(6_000_000_000)

entity_a.value = value_1
entity_b.value = value_2

problem = Solution([entity_a, entity_b], [value_1, value_2])

assert score_manager.explain(problem).score == SimpleScore.of(9_000_000_000)


ignored_python_functions = {
'_call_comparison_java_joiner',
'__init__',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
import ai.timefold.jpyinterpreter.types.PythonLikeType;
import ai.timefold.jpyinterpreter.types.collections.PythonLikeTuple;
import ai.timefold.jpyinterpreter.types.numeric.PythonInteger;
import ai.timefold.solver.core.api.score.buildin.bendable.BendableScore;
import ai.timefold.solver.core.api.score.buildin.bendablelong.BendableLongScore;

public final class BendableScorePythonJavaTypeMapping implements PythonJavaTypeMapping<PythonLikeObject, BendableScore> {
public final class BendableScorePythonJavaTypeMapping implements PythonJavaTypeMapping<PythonLikeObject, BendableLongScore> {
private final PythonLikeType type;
private final Constructor<?> constructor;
private final Field initScoreField;
Expand All @@ -34,20 +34,20 @@ public PythonLikeType getPythonType() {
}

@Override
public Class<? extends BendableScore> getJavaType() {
return BendableScore.class;
public Class<? extends BendableLongScore> getJavaType() {
return BendableLongScore.class;
}

private static PythonLikeTuple<PythonInteger> toPythonList(int[] scores) {
private static PythonLikeTuple<PythonInteger> toPythonList(long[] scores) {
PythonLikeTuple<PythonInteger> out = new PythonLikeTuple<>();
for (int score : scores) {
for (long score : scores) {
out.add(PythonInteger.valueOf(score));
}
return out;
}

@Override
public PythonLikeObject toPythonObject(BendableScore javaObject) {
public PythonLikeObject toPythonObject(BendableLongScore javaObject) {
try {
var instance = constructor.newInstance();
initScoreField.set(instance, PythonInteger.valueOf(javaObject.initScore()));
Expand All @@ -60,23 +60,23 @@ public PythonLikeObject toPythonObject(BendableScore javaObject) {
}

@Override
public BendableScore toJavaObject(PythonLikeObject pythonObject) {
public BendableLongScore toJavaObject(PythonLikeObject pythonObject) {
try {
var initScore = ((PythonInteger) initScoreField.get(pythonObject)).value.intValue();
var hardScoreTuple = ((PythonLikeTuple) hardScoresField.get(pythonObject));
var softScoreTuple = ((PythonLikeTuple) softScoresField.get(pythonObject));
int[] hardScores = new int[hardScoreTuple.size()];
int[] softScores = new int[softScoreTuple.size()];
long[] hardScores = new long[hardScoreTuple.size()];
long[] softScores = new long[softScoreTuple.size()];
for (int i = 0; i < hardScores.length; i++) {
hardScores[i] = ((PythonInteger) hardScoreTuple.get(i)).value.intValue();
hardScores[i] = ((PythonInteger) hardScoreTuple.get(i)).value.longValue();
}
for (int i = 0; i < softScores.length; i++) {
softScores[i] = ((PythonInteger) softScoreTuple.get(i)).value.intValue();
softScores[i] = ((PythonInteger) softScoreTuple.get(i)).value.longValue();
}
if (initScore == 0) {
return BendableScore.of(hardScores, softScores);
return BendableLongScore.of(hardScores, softScores);
} else {
return BendableScore.ofUninitialized(initScore, hardScores, softScores);
return BendableLongScore.ofUninitialized(initScore, hardScores, softScores);
}
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import ai.timefold.jpyinterpreter.types.PythonJavaTypeMapping;
import ai.timefold.jpyinterpreter.types.PythonLikeType;
import ai.timefold.jpyinterpreter.types.numeric.PythonInteger;
import ai.timefold.solver.core.api.score.buildin.hardmediumsoft.HardMediumSoftScore;
import ai.timefold.solver.core.api.score.buildin.hardmediumsoftlong.HardMediumSoftLongScore;

public final class HardMediumSoftScorePythonJavaTypeMapping
implements PythonJavaTypeMapping<PythonLikeObject, HardMediumSoftScore> {
implements PythonJavaTypeMapping<PythonLikeObject, HardMediumSoftLongScore> {
private final PythonLikeType type;
private final Constructor<?> constructor;
private final Field initScoreField;
Expand All @@ -36,12 +36,12 @@ public PythonLikeType getPythonType() {
}

@Override
public Class<? extends HardMediumSoftScore> getJavaType() {
return HardMediumSoftScore.class;
public Class<? extends HardMediumSoftLongScore> getJavaType() {
return HardMediumSoftLongScore.class;
}

@Override
public PythonLikeObject toPythonObject(HardMediumSoftScore javaObject) {
public PythonLikeObject toPythonObject(HardMediumSoftLongScore javaObject) {
try {
var instance = constructor.newInstance();
initScoreField.set(instance, PythonInteger.valueOf(javaObject.initScore()));
Expand All @@ -55,16 +55,16 @@ public PythonLikeObject toPythonObject(HardMediumSoftScore javaObject) {
}

@Override
public HardMediumSoftScore toJavaObject(PythonLikeObject pythonObject) {
public HardMediumSoftLongScore toJavaObject(PythonLikeObject pythonObject) {
try {
var initScore = ((PythonInteger) initScoreField.get(pythonObject)).value.intValue();
var hardScore = ((PythonInteger) hardScoreField.get(pythonObject)).value.intValue();
var mediumScore = ((PythonInteger) mediumScoreField.get(pythonObject)).value.intValue();
var softScore = ((PythonInteger) softScoreField.get(pythonObject)).value.intValue();
var hardScore = ((PythonInteger) hardScoreField.get(pythonObject)).value.longValue();
var mediumScore = ((PythonInteger) mediumScoreField.get(pythonObject)).value.longValue();
var softScore = ((PythonInteger) softScoreField.get(pythonObject)).value.longValue();
if (initScore == 0) {
return HardMediumSoftScore.of(hardScore, mediumScore, softScore);
return HardMediumSoftLongScore.of(hardScore, mediumScore, softScore);
} else {
return HardMediumSoftScore.ofUninitialized(initScore, hardScore, mediumScore, softScore);
return HardMediumSoftLongScore.ofUninitialized(initScore, hardScore, mediumScore, softScore);
}
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import ai.timefold.jpyinterpreter.types.PythonJavaTypeMapping;
import ai.timefold.jpyinterpreter.types.PythonLikeType;
import ai.timefold.jpyinterpreter.types.numeric.PythonInteger;
import ai.timefold.solver.core.api.score.buildin.hardsoft.HardSoftScore;
import ai.timefold.solver.core.api.score.buildin.hardsoftlong.HardSoftLongScore;

public final class HardSoftScorePythonJavaTypeMapping implements PythonJavaTypeMapping<PythonLikeObject, HardSoftScore> {
public final class HardSoftScorePythonJavaTypeMapping implements PythonJavaTypeMapping<PythonLikeObject, HardSoftLongScore> {
private final PythonLikeType type;
private final Constructor<?> constructor;
private final Field initScoreField;
Expand All @@ -33,12 +33,12 @@ public PythonLikeType getPythonType() {
}

@Override
public Class<? extends HardSoftScore> getJavaType() {
return HardSoftScore.class;
public Class<? extends HardSoftLongScore> getJavaType() {
return HardSoftLongScore.class;
}

@Override
public PythonLikeObject toPythonObject(HardSoftScore javaObject) {
public PythonLikeObject toPythonObject(HardSoftLongScore javaObject) {
try {
var instance = constructor.newInstance();
initScoreField.set(instance, PythonInteger.valueOf(javaObject.initScore()));
Expand All @@ -51,15 +51,15 @@ public PythonLikeObject toPythonObject(HardSoftScore javaObject) {
}

@Override
public HardSoftScore toJavaObject(PythonLikeObject pythonObject) {
public HardSoftLongScore toJavaObject(PythonLikeObject pythonObject) {
try {
var initScore = ((PythonInteger) initScoreField.get(pythonObject)).value.intValue();
var hardScore = ((PythonInteger) hardScoreField.get(pythonObject)).value.intValue();
var softScore = ((PythonInteger) softScoreField.get(pythonObject)).value.intValue();
var hardScore = ((PythonInteger) hardScoreField.get(pythonObject)).value.longValue();
var softScore = ((PythonInteger) softScoreField.get(pythonObject)).value.longValue();
if (initScore == 0) {
return HardSoftScore.of(hardScore, softScore);
return HardSoftLongScore.of(hardScore, softScore);
} else {
return HardSoftScore.ofUninitialized(initScore, hardScore, softScore);
return HardSoftLongScore.ofUninitialized(initScore, hardScore, softScore);
}
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import ai.timefold.jpyinterpreter.types.PythonJavaTypeMapping;
import ai.timefold.jpyinterpreter.types.PythonLikeType;
import ai.timefold.jpyinterpreter.types.numeric.PythonInteger;
import ai.timefold.solver.core.api.score.buildin.simple.SimpleScore;
import ai.timefold.solver.core.api.score.buildin.simplelong.SimpleLongScore;

public final class SimpleScorePythonJavaTypeMapping implements PythonJavaTypeMapping<PythonLikeObject, SimpleScore> {
public final class SimpleScorePythonJavaTypeMapping implements PythonJavaTypeMapping<PythonLikeObject, SimpleLongScore> {
private final PythonLikeType type;
private final Constructor<?> constructor;
private final Field initScoreField;
Expand All @@ -31,12 +31,12 @@ public PythonLikeType getPythonType() {
}

@Override
public Class<? extends SimpleScore> getJavaType() {
return SimpleScore.class;
public Class<? extends SimpleLongScore> getJavaType() {
return SimpleLongScore.class;
}

@Override
public PythonLikeObject toPythonObject(SimpleScore javaObject) {
public PythonLikeObject toPythonObject(SimpleLongScore javaObject) {
try {
var instance = constructor.newInstance();
initScoreField.set(instance, PythonInteger.valueOf(javaObject.initScore()));
Expand All @@ -48,14 +48,14 @@ public PythonLikeObject toPythonObject(SimpleScore javaObject) {
}

@Override
public SimpleScore toJavaObject(PythonLikeObject pythonObject) {
public SimpleLongScore toJavaObject(PythonLikeObject pythonObject) {
try {
var initScore = ((PythonInteger) initScoreField.get(pythonObject)).value.intValue();
var score = ((PythonInteger) scoreField.get(pythonObject)).value.intValue();
var score = ((PythonInteger) scoreField.get(pythonObject)).value.longValue();
if (initScore == 0) {
return SimpleScore.of(score);
return SimpleLongScore.of(score);
} else {
return SimpleScore.ofUninitialized(initScore, score);
return SimpleLongScore.ofUninitialized(initScore, score);
}
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,56 @@ def applyAsInt(self, argument1, argument2, argument3, argument4, argument5):
return JInt(self.delegate(argument1, argument2, argument3, argument4, argument5))


@JImplements('java.util.function.ToLongFunction', deferred=True)
class PythonToLongFunction:
def __init__(self, delegate):
self.delegate = delegate

@JOverride
def applyAsLong(self, argument):
return JLong(self.delegate(argument))


@JImplements('java.util.function.ToLongBiFunction', deferred=True)
class PythonToLongBiFunction:
def __init__(self, delegate):
self.delegate = delegate

@JOverride
def applyAsLong(self, argument1, argument2):
return JLong(self.delegate(argument1, argument2))


@JImplements('ai.timefold.solver.core.api.function.ToLongTriFunction', deferred=True)
class PythonToLongTriFunction:
def __init__(self, delegate):
self.delegate = delegate

@JOverride
def applyAsLong(self, argument1, argument2, argument3):
return JLong(self.delegate(argument1, argument2, argument3))


@JImplements('ai.timefold.solver.core.api.function.ToLongQuadFunction', deferred=True)
class PythonToLongQuadFunction:
def __init__(self, delegate):
self.delegate = delegate

@JOverride
def applyAsLong(self, argument1, argument2, argument3, argument4):
return JLong(self.delegate(argument1, argument2, argument3, argument4))


@JImplements('ai.timefold.solver.core.api.function.ToLongPentaFunction', deferred=True)
class PythonToLongPentaFunction:
def __init__(self, delegate):
self.delegate = delegate

@JOverride
def applyAsLong(self, argument1, argument2, argument3, argument4, argument5):
return JLong(self.delegate(argument1, argument2, argument3, argument4, argument5))



@JImplements('java.util.function.Predicate', deferred=True)
class PythonPredicate:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ def register_score_python_java_type_mappings():
_scores_registered = True

from .score._score import SimpleScore, HardSoftScore, HardMediumSoftScore, BendableScore
from ai.timefold.solver.core.api.score.buildin.simple import SimpleScore as _SimpleScore
from ai.timefold.solver.core.api.score.buildin.hardsoft import HardSoftScore as _HardSoftScore
from ai.timefold.solver.core.api.score.buildin.hardmediumsoft import HardMediumSoftScore as _HardMediumSoftScore
from ai.timefold.solver.core.api.score.buildin.bendable import BendableScore as _BendableScore
from ai.timefold.solver.core.api.score.buildin.simplelong import SimpleLongScore as _SimpleScore
from ai.timefold.solver.core.api.score.buildin.hardsoftlong import HardSoftLongScore as _HardSoftScore
from ai.timefold.solver.core.api.score.buildin.hardmediumsoftlong import HardMediumSoftLongScore as _HardMediumSoftScore
from ai.timefold.solver.core.api.score.buildin.bendablelong import BendableLongScore as _BendableScore

from ai.timefold.solver.python.score import (SimpleScorePythonJavaTypeMapping,
HardSoftScorePythonJavaTypeMapping,
Expand Down
Loading
Loading