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();
}
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]);
}
}
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());
}
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);
}
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());
}
Aggregations