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

org.apache.ignite.ml.genetic.utils.GAGridUtils Maven / Gradle / Ivy

Go to download

Apache Ignite® is a Distributed Database For High-Performance Computing With In-Memory Speed.

There is a newer version: 2.15.0
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.ignite.ml.genetic.utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.cache.Cache.Entry;

import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.cache.query.QueryCursor;
import org.apache.ignite.cache.query.SqlQuery;

import org.apache.ignite.ml.genetic.Chromosome;
import org.apache.ignite.ml.genetic.Gene;
import org.apache.ignite.ml.genetic.cache.PopulationCacheConfig;
import org.apache.ignite.ml.genetic.parameter.GAGridConstants;

/**
 * GA Grid Helper routines
 */
public class GAGridUtils {
    /**
     * Retrieve chromosomes
     *
     * @param ignite Ignite
     * @param query Sql
     * @return List of Chromosomes
     */
    public static List getChromosomes(Ignite ignite, String query) {
        List chromosomes = new ArrayList();

        IgniteCache populationCache = ignite.getOrCreateCache(PopulationCacheConfig.populationCache());

        SqlQuery sql = new SqlQuery(Chromosome.class, query);

        try (QueryCursor> cursor = populationCache.query(sql)) {
            for (Entry e : cursor)
                chromosomes.add(e.getValue());
        }

        return chromosomes;
    }

    /**
     * Retrieve genes in order
     *
     * @param ignite Ignite
     * @param chromosome Chromosome
     * @return List of Genes
     */
    public static List getGenesInOrderForChromosome(Ignite ignite, Chromosome chromosome) {
        List genes = new ArrayList();
        IgniteCache cache = ignite.cache(GAGridConstants.GENE_CACHE);

        long[] primaryKeys = chromosome.getGenes();

        for (int k = 0; k < primaryKeys.length; k++) {

            StringBuffer sbSqlClause = new StringBuffer();
            sbSqlClause.append("_key IN ");
            sbSqlClause.append("(");
            sbSqlClause.append(primaryKeys[k]);
            sbSqlClause.append(")");

            SqlQuery sql = new SqlQuery(Gene.class, sbSqlClause.toString());

            try (QueryCursor> cursor = cache.query(sql)) {
                for (Entry e : cursor)
                    genes.add(e.getValue());
            }
        }

        return genes;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy