All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ai.timefold.solver.python.PythonValueRangeFactory Maven / Gradle / Ivy

Go to download

Timefold solves planning problems. This lightweight, embeddable planning engine implements powerful and scalable algorithms to optimize business resource scheduling and planning. This module contains the core implementation of the Python solver.

There is a newer version: 1.14.0
Show newest version
package ai.timefold.solver.python;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Iterator;
import java.util.Random;
import java.util.function.Function;

import ai.timefold.jpyinterpreter.types.numeric.PythonBoolean;
import ai.timefold.jpyinterpreter.types.numeric.PythonFloat;
import ai.timefold.jpyinterpreter.types.numeric.PythonInteger;
import ai.timefold.solver.core.api.domain.valuerange.CountableValueRange;
import ai.timefold.solver.core.api.domain.valuerange.ValueRangeFactory;

@SuppressWarnings("unused")
public class PythonValueRangeFactory {
    private PythonValueRangeFactory() {
    }

    private record IteratorMapper(Iterator sourceIterator, Function valueConvertor)
            implements
                Iterator {

        @Override
        public boolean hasNext() {
            return sourceIterator.hasNext();
        }

        @Override
        public To_ next() {
            return valueConvertor.apply(sourceIterator.next());
        }
    }

    private record ValueRangeMapper(CountableValueRange sourceValueRange,
            Function valueConvertor, Function inverseValueConvertor)
            implements
                CountableValueRange {

        @Override
        public long getSize() {
            return sourceValueRange.getSize();
        }

        @Override
        public To_ get(long index) {
            return valueConvertor.apply(sourceValueRange.get(index));
        }

        @Override
        public Iterator createOriginalIterator() {
            return new IteratorMapper<>(sourceValueRange.createOriginalIterator(), valueConvertor);
        }

        @Override
        public boolean isEmpty() {
            return sourceValueRange.isEmpty();
        }

        @Override
        public boolean contains(To_ value) {
            return sourceValueRange.contains(inverseValueConvertor.apply(value));
        }

        @Override
        public Iterator createRandomIterator(Random random) {
            return new IteratorMapper<>(sourceValueRange.createRandomIterator(random), valueConvertor);
        }
    }

    public static CountableValueRange createIntValueRange(BigInteger from, BigInteger to) {
        return new ValueRangeMapper<>(ValueRangeFactory.createBigIntegerValueRange(from, to),
                PythonInteger::valueOf,
                pythonInteger -> pythonInteger.value);
    }

    public static CountableValueRange createIntValueRange(BigInteger from, BigInteger to, BigInteger step) {
        return new ValueRangeMapper<>(ValueRangeFactory.createBigIntegerValueRange(from, to, step),
                PythonInteger::valueOf,
                pythonInteger -> pythonInteger.value);
    }

    public static CountableValueRange createFloatValueRange(BigDecimal from, BigDecimal to) {
        return new ValueRangeMapper<>(ValueRangeFactory.createBigDecimalValueRange(from, to),
                decimal -> PythonFloat.valueOf(decimal.doubleValue()),
                pythonFloat -> BigDecimal.valueOf(pythonFloat.value));
    }

    public static CountableValueRange createFloatValueRange(BigDecimal from, BigDecimal to, BigDecimal step) {
        return new ValueRangeMapper<>(ValueRangeFactory.createBigDecimalValueRange(from, to, step),
                decimal -> PythonFloat.valueOf(decimal.doubleValue()),
                pythonFloat -> BigDecimal.valueOf(pythonFloat.value));
    }

    public static CountableValueRange createBooleanValueRange() {
        return new ValueRangeMapper<>(ValueRangeFactory.createBooleanValueRange(),
                PythonBoolean::valueOf,
                PythonBoolean::getBooleanValue);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy