public static void Main(string[] args) { int mineral, gas, supply; Data data = new Data(); if (args.Length != 3) { System.Console.WriteLine("Usage: ResourceAllocation #mineral #gas #supply"); //return; mineral = 1000; gas = 700; supply = 19; } else { mineral = Convert.ToInt32(args[0]); gas = Convert.ToInt32(args[1]); supply = Convert.ToInt32(args[2]); } int maxSize = Math.Max(mineral / 25, Math.Max(gas / 25, supply)); var domain = new ghost.Domain(maxSize, 0); var listUnits = MakeTerranUnits(domain); var setUnits = new SetVariables(listUnits); var constraints = new List <Constraint> { new MineralConstraint(setUnits, mineral), new GasConstraint(setUnits, gas), new SupplyConstraint(setUnits, supply) }; var objective = new MaxDPS("max DPS", mineral, gas, supply); var solver = new ghost.Solver <Variable, SetVariables, Constraint>(setUnits, constraints, objective); Console.WriteLine("Start solving trivial test"); solver.solve(20, 150); int mineralUsed = 0; int gasUsed = 0; double supplyUsed = 0.0; double DPS = 0.0; for (int i = 0; i < setUnits.GetNumberVariables(); ++i) { mineralUsed += setUnits.GetValue(i) * data.Dataset[setUnits.Name(i)].CostMineral; gasUsed += setUnits.GetValue(i) * data.Dataset[setUnits.Name(i)].CostGas; supplyUsed += setUnits.GetValue(i) * data.Dataset[setUnits.Name(i)].CostSupply; if (data.Dataset[setUnits.Name(i)].GroundAttack > 0) { DPS += setUnits.GetValue(i) * ((double)data.Dataset[setUnits.Name(i)].GroundAttack / data.Dataset[setUnits.Name(i)].GroundCooldown); } } DPS *= 24; Console.WriteLine("DPS = " + DPS); Console.WriteLine("Mineral left: " + (mineral - mineralUsed) + ", gas left: " + (gas - gasUsed) + ", supply left: " + (supply - supplyUsed)); }
override public double Cost(SetVariables variables) { var costs = new double[variables.GetNumberVariables()]; for (int i = 0; i < variables.GetNumberVariables(); ++i) { if (Data.Dataset[variables.Name(i)].GroundAttack > 0) { costs[i] = variables.GetValue(i) * ((double)Data.Dataset[variables.Name(i)].GroundAttack / Data.Dataset[variables.Name(i)].GroundCooldown); } } var total = costs.Sum(); return(total > 0 ? -total : double.MaxValue); }
public SupplyConstraint(SetVariables variables, double resource) : base(variables, resource) { }
public MineralConstraint(SetVariables variables, double resource) : base(variables, resource) { }
override public int HeuristicValue(List <int> valuesIndex, int variableIndex, SetVariables variables) { double minValue = double.MaxValue; int minIndex = -1; double tmp; int backup = variables.GetValue(variableIndex); for (int i = 0; i < valuesIndex.Count; ++i) { variables.SetValue(variableIndex, variables.Domain(variableIndex).GetValue(valuesIndex[i])); tmp = Cost(variables); if (minValue > tmp) { minValue = tmp; minIndex = i; } } // a priori not necessary, but just in case... variables.SetValue(variableIndex, backup); // Returns the INDEX of a possible value in valuesIndex, not the value itself! return(minIndex == -1 ? valuesIndex[Random.Next(0, valuesIndex.Count)] : valuesIndex[minIndex]); }