Beispiel #1
0
        static void Main(string[] args)
        {
            // The values will be in [0, 9]
            var domain = new Domain(10, 0);

            // Three variables
            var x   = new Variable("x", domain, 0);
            var y   = new Variable("y", domain, 0);
            var z   = new Variable("z", domain, 0);
            var set = new SetVariables <Variable>(x, y, z);

            var constraints = new List <NumberConstraint>
            {
                new SmallerThan(set, 1, 7), // y (at index 1) should be smaller than 7
                new SmallerThan(set, 2, 3)  // z (at index 2) should be smaller than 3
            };

            // Look for the largest sum of the three numbers
            // that satisfies the constraints
            var objective = new LargestSum();

            var solver = new SumSolver(set, constraints, objective);

            solver.solve(100, 600); // There is no rush so take the time to find a good solution

            int total = Enumerable
                        .Range(0, set.GetNumberVariables())
                        .Sum(i => set.GetValue(i));

            // The optimal result is 17 (x=9, y=6, z=2)
            Console.WriteLine("Computed solution = {0}, Best solution = {1}", total, 9 + 6 + 2);
            Console.ReadLine();
        }
        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 = 20000;
            gas = 14000;
            supply = 380;
              }
              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( 100, 2000 );

              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) );
        }
Beispiel #3
0
        public override double Cost(SetVariables <Variable> variables)
        {
            // Compute the sum for the current variable assignments
            var cost = Enumerable
                       .Range(0, variables.GetNumberVariables())
                       .Sum(i => variables.GetValue(i));

            // Return the inverse sum since we want to maximize it
            return(1.0f / cost);
        }
Beispiel #4
0
        public void SimulateCostBaseTest()
        {
            setBuildings.SetValue(0, -1);
            setBuildings.SetValue(1, 0);
            setBuildings.SetValue(2, 8);
#if DEBUG
            setBuildings.Print();
#endif
            var varSimCost = new Dictionary <int, double[]>();
            for (int i = 0; i < setBuildings.Domain(1).MaxValue() + 1; ++i)
            {
                varSimCost[i] = new double[setBuildings.GetNumberVariables()];
                for (var j = 0; j < setBuildings.GetNumberVariables(); ++j)
                {
                    varSimCost[i][j] = -1;
                }
            }

            var noHoles  = new WallShape(setBuildings);
            var vecCosts = noHoles.SimulateCost(1, varSimCost);

            foreach (var tuple in vecCosts)
            {
                if (tuple.Key == 0 || tuple.Key == 7 || tuple.Key == 8 || tuple.Key == 12 || tuple.Key == 13)
                {
                    Assert.AreEqual(2.0, tuple.Value);
                }
                else if (tuple.Key == 1 || tuple.Key == 6 || tuple.Key == 11 || tuple.Key == 16 || tuple.Key == 17 || tuple.Key == 18)
                {
                    Assert.AreEqual(0.0, tuple.Value);
                }
                else
                {
                    Assert.AreEqual(4.0, tuple.Value);
                }
            }
        }