private void InitializeServers(ProblemInput input, ServerSelector serverSelector) { IEnumerable <Pool> reversedPools = input.Pools; reversedPools.Reverse(); foreach (var pool in input.Pools) { if (!_rowAllocator.AllocateNextServerToPool(input, serverSelector, pool)) { throw new Exception("Couldn't allocate in initialization!"); } } foreach (var pool in reversedPools) { if (!_rowAllocator.AllocateNextServerToPool(input, serverSelector, pool)) { throw new Exception("Couldn't allocate in initialization!"); } } foreach (var pool in input.Pools) { _poolGuaranteedCapacities[pool] = pool.GurranteedCapacity(_result); } }
protected override ProblemOutput Solve(ProblemInput input) { _input = input; _poolGuaranteedCapacities = new Dictionary <Pool, int>(); _result = new ProblemOutput { _allocations = new Dictionary <Server, ServerAllocation>(), original_input = input }; _rowAllocator = new RowAllocator(_input, _result, this.NumbersGenerator); _serverSelector = new ServerSelector(_input, _result); // TODO: Make sure order is correct InitializeServers(input, _serverSelector); int used = 0, notUsed = 0; while (_serverSelector.HasAvailableServer) { var pool = GetLowestCapacityPool(); if (!_rowAllocator.AllocateNextServerToPool(input, _serverSelector, pool)) { notUsed++; } else { used++; _poolGuaranteedCapacities[pool] = pool.GurranteedCapacity(_result); } } while (_rowAllocator.HasUnusedServers) { var pool = GetLowestCapacityPool(); _rowAllocator.AllocateUnsedServerToPool(pool); _poolGuaranteedCapacities[pool] = pool.GurranteedCapacity(_result); } //var lowestPool = GetLowestCapacityPool(); //var highestPool = GetHighestCapacityPool(); //Console.WriteLine("Lowest ({0})", lowestPool.GurranteedCapacity(_result)); //PrintPoolRows(lowestPool); //Console.WriteLine("Highest ({0})", highestPool.GurranteedCapacity(_result)); //PrintPoolRows(highestPool); TryToMoveServersFromBestToWorst(); //Console.WriteLine("Used: "+ used); //Console.WriteLine("NotUsed: "+ notUsed); return(_result); }
public bool AllocateNextServerToPool(ProblemInput input, ServerSelector serverSelector, Pool pool) { var nextServer = serverSelector.UseNextServer(); ServerAllocation allocation = AlllocateServerToRow(input, nextServer, pool); if (allocation == null) { _unusedServers.Push(nextServer); return(false); } allocation.Pool = pool; _result._allocations.Add(allocation.Server, allocation); return(true); }