<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using CocoWrapper;
using Common.Parameters;

namespace Common
{

    public static class FunctionFactory
    {
        private static int cacheLimit = 5;
        private static Dictionary<string, IFitnessFunction<double[], double[]>>  functionCache = new Dictionary<string, IFitnessFunction<double[], double[]>>();

        public static void SaveToCache(string id, IFitnessFunction<double[], double[]> function)
        {
            if (functionCache.Count == cacheLimit)
            {
                functionCache.Clear();
            }
            functionCache.Add(id,function);
            if (functionCache.Count > cacheLimit)
            {
                functionCache.Clear();
            }
        }

        public static IFitnessFunction<double[],double[]> GetFitnessFunction(FunctionParameters parameters)
        {
            if (functionCache.ContainsKey(parameters.FitnessFunctionType))
            {
                return functionCache[parameters.FitnessFunctionType];
            }
            if (parameters.FitnessFunctionType.Contains("bbob"))
            {
                var functionId = parameters.FitnessFunctionType;
                var observerOptions =
                             "result_folder: PSO_on_bbob_" +
                             DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString()
                           + " algorithm_name: PSO"
                           + " algorithm_info: \"Cluster node for optimizing BBOB functions\"";
                
                var suite = new Suite("bbob", "year: 2016", "dimensions: " + parameters.Dimension);
                var observer = new Observer("bbob", observerOptions);
                var benchmark = new Benchmark(suite, observer);
                Problem problem;
                /* Iterate over all problems in the suite */
                while ((problem = benchmark.getNextProblem()) != null)
                {
                    if (problem.Id != functionId) continue;
                    var upper = problem.getLargestValuesOfInterest();
                    var bounds =
                        problem.getSmallestValuesOfInterest()
                            .Select((x, i) => new DimensionBound(x, upper[i]))
                            .ToArray();
                    parameters.SearchSpace = bounds;
                    return new FitnessFunction(problem.evaluateFunction);
                }

            }
            switch (parameters.FitnessFunctionType)
            {
                case "quadratic":
                    return new QuadraticFunction(parameters);
                case "rastrigin":
                    return new RastriginFunction(parameters);
                case "rosenbrock":
                    return new RosenbrockFunction(parameters);
                default:
                    throw new ArgumentException("Unknown function type.");
            }
        }
    }
}
<commit_msg>Fixed function factory<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using CocoWrapper;
using Common.Parameters;

namespace Common
{

    public static class FunctionFactory
    {
        private static int cacheLimit = 5;
        private static Dictionary<string, IFitnessFunction<double[], double[]>>  functionCache = new Dictionary<string, IFitnessFunction<double[], double[]>>();

        public static void SaveToCache(string id, IFitnessFunction<double[], double[]> function)
        {
            if (functionCache.Count == cacheLimit)
            {
                functionCache.Clear();
            }
            functionCache.Add(id,function);
            if (functionCache.Count > cacheLimit)
            {
                functionCache.Clear();
            }
        }

        private static Benchmark benchmark = null;
        private static Observer observer = null;

        private static void restartBbob(int dimension)
        {
            var suite = new Suite("bbob", "year: 2016", "dimensions: " + dimension);
            if (observer == null)
            {
                var observerOptions =
                    "result_folder: PSO_on_bbob_" +
                    DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString()
                    + " algorithm_name: PSO"
                    + " algorithm_info: \"Cluster node for optimizing BBOB functions\"";
                observer = new Observer("observer", observerOptions);
            }

            benchmark = new Benchmark(suite, observer);
        }

        public static IFitnessFunction<double[],double[]> GetFitnessFunction(FunctionParameters parameters)
        {
            if (functionCache.ContainsKey(parameters.FitnessFunctionType))
            {
                return functionCache[parameters.FitnessFunctionType];
            }
            if (parameters.FitnessFunctionType.Contains("bbob"))
            {
                var functionId = parameters.FitnessFunctionType;
                Problem problem;
                /* Iterate over all problems in the suite */
                restartBbob(parameters.Dimension);
                while ((problem = benchmark.getNextProblem()) != null)
                {
                    if (problem.Id != functionId) continue;
                    var upper = problem.getLargestValuesOfInterest();
                    var bounds =
                        problem.getSmallestValuesOfInterest()
                            .Select((x, i) => new DimensionBound(x, upper[i]))
                            .ToArray();
                    parameters.SearchSpace = bounds;
                    var function = new FitnessFunction(problem.evaluateFunction);
                    return function;
                }


            }
            switch (parameters.FitnessFunctionType)
            {
                case "quadratic":
                    return new QuadraticFunction(parameters);
                case "rastrigin":
                    return new RastriginFunction(parameters);
                case "rosenbrock":
                    return new RosenbrockFunction(parameters);
                default:
                    throw new ArgumentException("Unknown function type.");
            }
        }
    }
}

{'fraction_non_alphanumeric': 0.08571882951653945, 'fraction_numerical': 0.0015903307888040711, 'mean_word_length': 1.5901976935749589, 'pattern_counts': {'":': 6, '<': 15, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5649637', 'n_tokens_mistral': 1473, 'n_tokens_neox': 1373, 'n_words': 326}