Search in sources :

Example 1 with IntegerGene

use of io.jenetics.IntegerGene in project jenetics by jenetics.

the class XMLStreamWriterTest method write.

static void write(final IntegerChromosome ch, final XMLStreamWriter xml) throws XMLStreamException {
    xml.writeStartElement("int-chromosome");
    xml.writeAttribute("length", Integer.toString(ch.length()));
    xml.writeStartElement("min");
    xml.writeCharacters(ch.min().toString());
    xml.writeEndElement();
    xml.writeStartElement("max");
    xml.writeCharacters(ch.max().toString());
    xml.writeEndElement();
    xml.writeStartElement("alleles");
    for (IntegerGene gene : ch) {
        xml.writeStartElement("allele");
        xml.writeCharacters(gene.allele().toString());
        xml.writeEndElement();
    }
    xml.writeEndElement();
    xml.writeEndElement();
}
Also used : IntegerGene(io.jenetics.IntegerGene)

Example 2 with IntegerGene

use of io.jenetics.IntegerGene in project jenetics by jenetics.

the class CodecsTest method ofIntVectorVector.

@Test(dataProvider = "intVectorDataVector")
public void ofIntVectorVector(final IntRange[] domain) {
    final Codec<int[], IntegerGene> codec = Codecs.ofVector(domain);
    final Genotype<IntegerGene> gt = codec.encoding().newInstance();
    assertEquals(gt.length(), domain.length);
    for (int i = 0; i < gt.length(); ++i) {
        final Chromosome<IntegerGene> ch = gt.get(i);
        assertEquals(ch.length(), 1);
        final IntegerGene gene = ch.gene();
        assertEquals(gene.min().intValue(), domain[i].min());
        assertEquals(gene.max().intValue(), domain[i].max());
    }
    final Function<Genotype<IntegerGene>, int[]> f = codec.decoder();
    final int[] value = f.apply(gt);
    assertEquals(value.length, domain.length);
    for (int i = 0; i < domain.length; ++i) {
        assertEquals(gt.get(i).get(0).intValue(), value[i]);
    }
}
Also used : IntegerGene(io.jenetics.IntegerGene) Genotype(io.jenetics.Genotype) Test(org.testng.annotations.Test)

Example 3 with IntegerGene

use of io.jenetics.IntegerGene in project jenetics by jenetics.

the class StreamPublisherTest method publishClosingPublisher.

@Test
public void publishClosingPublisher() throws InterruptedException {
    final int generations = 20;
    final var publisher = new StreamPublisher<EvolutionResult<IntegerGene, Integer>>();
    final var stream = _engine.stream();
    final var lock = new ReentrantLock();
    final var finished = lock.newCondition();
    final AtomicBoolean running = new AtomicBoolean(true);
    final AtomicBoolean completed = new AtomicBoolean(false);
    final AtomicInteger count = new AtomicInteger();
    publisher.subscribe(new Subscriber<>() {

        private Subscription _subscription;

        @Override
        public void onSubscribe(final Subscription subscription) {
            _subscription = requireNonNull(subscription);
            _subscription.request(1);
        }

        @Override
        public void onNext(final EvolutionResult<IntegerGene, Integer> er) {
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                throw new CancellationException();
            }
            count.incrementAndGet();
            lock.lock();
            try {
                running.set(er.generation() < generations);
                finished.signal();
            } finally {
                lock.unlock();
            }
            _subscription.request(1);
        }

        @Override
        public void onComplete() {
            lock.lock();
            try {
                completed.set(true);
                finished.signalAll();
            } finally {
                lock.unlock();
            }
        }

        @Override
        public void onError(final Throwable throwable) {
        }
    });
    publisher.attach(stream);
    lock.lock();
    try {
        while (running.get()) {
            finished.await();
        }
    } finally {
        lock.unlock();
    }
    publisher.close();
    lock.lock();
    try {
        while (!completed.get()) {
            finished.await();
        }
    } finally {
        lock.unlock();
    }
    assertThat(count.get()).isGreaterThanOrEqualTo(generations);
    Assert.assertTrue(completed.get());
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IntegerGene(io.jenetics.IntegerGene) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CancellationException(java.util.concurrent.CancellationException) Subscription(java.util.concurrent.Flow.Subscription) Test(org.testng.annotations.Test)

Example 4 with IntegerGene

use of io.jenetics.IntegerGene in project jenetics by jenetics.

the class Generator method createChromosome.

private static IntegerChromosome createChromosome(final Board board, int iChromosome) {
    final var random = RandomAdapter.of(RandomRegistry.random());
    List<Integer> changes = new ArrayList<>();
    List<Integer> inputs = new ArrayList<>();
    for (int i = 0; i < Board.SIZE; i++) {
        changes.add(i + 1);
        if (board.get(iChromosome, i) != 0) {
            inputs.add(board.get(iChromosome, i));
        }
    }
    changes.removeAll(inputs);
    Collections.shuffle(changes, random);
    List<IntegerGene> genes = new ArrayList<>();
    for (int j = 0; j < Board.SIZE; j++) {
        if (board.get(iChromosome, j) == 0) {
            int value = changes.remove(changes.size() - 1);
            IntegerGene gene = IntegerGene.of(value, 1, Board.SIZE + 1);
            genes.add(gene);
        }
    }
    return IntegerChromosome.of(genes);
}
Also used : IntegerGene(io.jenetics.IntegerGene) ArrayList(java.util.ArrayList)

Example 5 with IntegerGene

use of io.jenetics.IntegerGene in project jenetics by jenetics.

the class StreamPublisherTest method publishLimitedStream.

@Test
public void publishLimitedStream() throws InterruptedException {
    final int generations = 20;
    final var publisher = new StreamPublisher<EvolutionResult<IntegerGene, Integer>>();
    final var stream = _engine.stream().limit(generations);
    final var lock = new ReentrantLock();
    final var finished = lock.newCondition();
    final AtomicBoolean running = new AtomicBoolean(true);
    final AtomicBoolean completed = new AtomicBoolean(false);
    final AtomicInteger count = new AtomicInteger();
    publisher.subscribe(new Subscriber<>() {

        private Subscription _subscription;

        @Override
        public void onSubscribe(final Subscription subscription) {
            _subscription = requireNonNull(subscription);
            _subscription.request(1);
        }

        @Override
        public void onNext(final EvolutionResult<IntegerGene, Integer> er) {
            count.incrementAndGet();
            _subscription.request(1);
        }

        @Override
        public void onComplete() {
            lock.lock();
            try {
                running.set(false);
                completed.set(true);
                finished.signal();
            } finally {
                lock.unlock();
            }
        }

        @Override
        public void onError(final Throwable throwable) {
        }
    });
    publisher.attach(stream);
    lock.lock();
    try {
        while (running.get()) {
            finished.await();
        }
    } finally {
        lock.unlock();
    }
    publisher.close();
    Assert.assertEquals(count.get(), generations);
    Assert.assertTrue(completed.get());
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IntegerGene(io.jenetics.IntegerGene) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Subscription(java.util.concurrent.Flow.Subscription) Test(org.testng.annotations.Test)

Aggregations

IntegerGene (io.jenetics.IntegerGene)8 Test (org.testng.annotations.Test)6 Genotype (io.jenetics.Genotype)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 DoubleChromosome (io.jenetics.DoubleChromosome)2 DoubleGene (io.jenetics.DoubleGene)2 IntegerChromosome (io.jenetics.IntegerChromosome)2 Optimize (io.jenetics.Optimize)2 DoubleRange (io.jenetics.util.DoubleRange)2 ISeq (io.jenetics.util.ISeq)2 String.format (java.lang.String.format)2 Subscription (java.util.concurrent.Flow.Subscription)2 Function (java.util.function.Function)2 IntStream (java.util.stream.IntStream)2 Assert (org.testng.Assert)2 DataProvider (org.testng.annotations.DataProvider)2 Alterer (io.jenetics.Alterer)1 BoltzmannSelector (io.jenetics.BoltzmannSelector)1 Gene (io.jenetics.Gene)1 LongChromosome (io.jenetics.LongChromosome)1