/// <summary> /// 找到一组数中第k小元素 最坏时间复杂度O^2 期望时间复杂度O(n) /// </summary> /// <typeparam name="T"></typeparam> /// <param name="a"></param> /// <param name="k"></param> /// <returns></returns> public static T Select <T>(T[] a, int k) { RandomAlgorithm.Shuffle <T>(a); int lo = 0, hi = a.Length - 1; while (hi > lo) { int j = QuickSort <T> .Partition(a, lo, hi); if (j == k) { return(a[k]); } else if (j > k) { hi = j - 1; } else if (j < k) { lo = j + 1; } } return(a[k]); }
public async Task <ActionResult <IEnumerable <Film> > > GetRandomFilms() { RandomAlgorithm alg = new RandomAlgorithm(db); //В конструктор передается пустой аккаунт просто как заглушка потому, что в интерфейсе IFilmSelection есть сигнатура метода с параметром return(await alg.GetFilmsAsync(new Account())); }
protected override void SolveInstance(IGH_DataAccess DA) { Interval range = new Interval(0, 0); if (!DA.GetData(0, ref range)) { return; } var randomSolution = new RandomAlgorithm(range); CreateAndSetAlgorithm(DA, randomSolution.Solve); }
public void RandomAlgorithm_ReturnsMove() { var generator = new TestCase1.Generator(); var applier = new TestCase1.Applier(); var algorithm = new RandomAlgorithm <TestCase1.State, TestCase1.Move>(generator); var initState = new TestCase1.State(1, 0); var move1 = algorithm.Calculate(initState); var state2 = applier.Apply(initState, move1); var move2 = algorithm.Calculate(state2); Assert.NotNull(move1); Assert.NotNull(move2); }
private IAlgorithm <ChessRepresentation, BaseMove> GetAlgorithm(Type algorithmType, Type evaluatorType) { var generator = new MoveGenerator(_mechanism); var applier = new MoveApplier(_mechanism); var evaluator = (IEvaluator <ChessRepresentation>)Activator.CreateInstance(evaluatorType, new[] { _mechanism }); // TODO : A bit hacky, refactor later! if (algorithmType == typeof(MinimaxAlgorithm <ChessRepresentation, BaseMove>)) { var minimax = new MinimaxAlgorithm <ChessRepresentation, BaseMove>(evaluator, generator, applier) { MaxDepth = (int)numericUpDown1.Value }; return(minimax); } if (algorithmType == typeof(AlphaBetaAlgorithm <ChessRepresentation, BaseMove>)) { var ab = new AlphaBetaAlgorithm <ChessRepresentation, BaseMove>(evaluator, generator, applier) { MaxDepth = (int)numericUpDown1.Value }; return(ab); } if (algorithmType == typeof(MinimaxAverageAlgorithm <ChessRepresentation, BaseMove>)) { var minimaxAvg = new MinimaxAverageAlgorithm <ChessRepresentation, BaseMove>(evaluator, generator, applier) { MaxDepth = (int)numericUpDown1.Value }; return(minimaxAvg); } if (algorithmType == typeof(GreedyAlgorithm <ChessRepresentation, BaseMove>)) { var greedy = new GreedyAlgorithm <ChessRepresentation, BaseMove>(evaluator, generator, applier); return(greedy); } if (algorithmType == typeof(RandomAlgorithm <ChessRepresentation, BaseMove>)) { var randomAlgorithm = new RandomAlgorithm <ChessRepresentation, BaseMove>(generator); return(randomAlgorithm); } throw new ArgumentOutOfRangeException(nameof(algorithmType)); }
/// <summary> /// Generates a random list based on a given list (default random algorithm uses modified Fisher-Yates /// </summary> /// <typeparam name="T"></typeparam> /// <param name="list">list to be randomized</param> /// <param name="algorithm">type of algorithm used for randomization</param> /// <returns></returns> public static List <T> GenerateRandomList <T>(List <T> list, RandomAlgorithm algorithm = RandomAlgorithm.FisherYatesShuffle) { if (list.Count == 0) { return(new List <T>()); } List <T> randomList = new List <T>(); switch (algorithm) { case RandomAlgorithm.FisherYatesShuffle: randomList = FisherYatesShuffleList(list); break; default: break; } return(randomList); }
public async Task InvokeAsync(HttpContext context) { var balancerAlgorithmSetting = LoadBalancerSettings.Current.BalancingAlgorithm.ToLowerInvariant().Replace(" ", ""); BalancingAlgorithm balancerAlgorithm = null; if (balancerAlgorithmSetting == "roundrobin") { balancerAlgorithm = new RoundRobinAlgorithm(); } else if (balancerAlgorithmSetting == "random") { balancerAlgorithm = new RandomAlgorithm(); } else if (balancerAlgorithmSetting == "connectioncount") { balancerAlgorithm = new ConnectionCountAlgorithm(); } context.Items["destination"] = balancerAlgorithm.GetInstance(); await _next.Invoke(context); }
public static void Sort(T[] a) { RandomAlgorithm.Shuffle <T>(a); Sort(a, 0, a.Length - 1); }
static void Main(string[] args) { QAPAlgorithm test = null; string command = ""; while (command != "exit") { command = Console.ReadLine(); switch (command) { case "gen": //Console.WriteLine("nOfGenerations: "); //int nOfGenerations = Int32.Parse(Console.ReadLine()); //Console.WriteLine("p_crossover: "); //double p_crossover = Double.Parse(Console.ReadLine()); //Console.WriteLine("p_mutation: "); //double p_mutation = Double.Parse(Console.ReadLine()); //Console.WriteLine("tourn_size: "); //int tourn_size = Int32.Parse(Console.ReadLine()); //Console.WriteLine("pop_size: "); //int pop_size = Int32.Parse(Console.ReadLine()); //Console.WriteLine("test_case: "); //int gen_test_case = Int32.Parse(Console.ReadLine()); //base test = new GeneticAlgorithm(100, 100, 0.7, 0.01, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); //test selection test = new GeneticAlgorithm(100, 100, 0.7, 0.01, SelectionMethod.Roulette, 1, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(100, 100, 0.7, 0.01, SelectionMethod.Tournament, 1, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(100, 100, 0.7, 0.01, SelectionMethod.Tournament, 2, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(100, 100, 0.7, 0.01, SelectionMethod.Tournament, 10, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(100, 100, 0.7, 0.01, SelectionMethod.Tournament, 25, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(100, 100, 0.7, 0.01, SelectionMethod.Tournament, 100, $"input{12}.txt"); test.Run(); //test mutation test = new GeneticAlgorithm(100, 100, 0.7, 0.00, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(100, 100, 0.7, 0.02, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(100, 100, 0.7, 0.05, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(100, 100, 0.7, 0.10, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(100, 100, 0.7, 0.20, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(100, 100, 0.7, 0.50, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(100, 100, 0.7, 1.00, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); //test crossover test = new GeneticAlgorithm(100, 100, 0.0, 0.01, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(100, 100, 0.2, 0.01, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(100, 100, 0.4, 0.01, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(100, 100, 0.5, 0.01, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(100, 100, 0.6, 0.01, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(100, 100, 0.8, 0.01, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(100, 100, 1.0, 0.01, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); //test pop-size test = new GeneticAlgorithm(100, 10, 0.7, 0.01, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(100, 20, 0.7, 0.01, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(100, 50, 0.7, 0.01, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(100, 100, 0.7, 0.01, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(100, 200, 0.7, 0.01, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(100, 500, 0.7, 0.01, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); //test gen-count test = new GeneticAlgorithm(10, 100, 0.7, 0.01, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(20, 100, 0.7, 0.01, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(50, 100, 0.7, 0.01, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(100, 100, 0.7, 0.01, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(200, 100, 0.7, 0.01, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(500, 100, 0.7, 0.01, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); //test both test = new GeneticAlgorithm(500, 500, 0.7, 0.01, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); Console.WriteLine("done!"); break; case "grd": Console.WriteLine("starting_point: "); int starting_point = Int32.Parse(Console.ReadLine()); Console.WriteLine("test_case: "); int grd_test_case = Int32.Parse(Console.ReadLine()); test = new GreedyAlgorithm(starting_point, $"input{grd_test_case}.txt"); break; case "rnd": Console.WriteLine("best_of: "); int best_of = Int32.Parse(Console.ReadLine()); Console.WriteLine("test_case: "); int rnd_test_case = Int32.Parse(Console.ReadLine()); test = new RandomAlgorithm(best_of, $"input{rnd_test_case}.txt"); break; default: Console.WriteLine("wut"); break; } if (test != null) { test.Input.ReadFile(); Console.WriteLine(test.Evaluate(test.Run())); } } }