Пример #1
0
        public override double Cost(SetVariables variables)
        {
            double gaps = 0.0;

            for (int i = 0; i < variables.GetNumberVariables(); ++i)
            {
                gaps += GapSize(i, variables);
            }

            return(gaps);
        }
Пример #2
0
        public override int HeuristicVariable(List <int> indexes, SetVariables variables)
        {
            var gapList = new List <int>();

            for (int i = 0; i < indexes.Count; ++i)
            {
                gapList.Add(GapSize(indexes[i], variables));
            }

            int largestGap = gapList.Max();

            var largestGapIndex = new List <int>();

            for (int i = 0; i < gapList.Count; ++i)
            {
                if (gapList[i] == largestGap)
                {
                    largestGapIndex.Add(i);
                }
            }

            return(largestGapIndex.Count == 1 ? largestGapIndex[0] : largestGapIndex[Random.Next(0, largestGapIndex.Count)]);
        }
Пример #3
0
        private static int GapSize(int index, SetVariables variables)
        {
            if (!variables.IsSelected(index))
            {
                return(0);
            }

            int gaps = 0;

            var neighbors = variables.GetBuildingsAbove(index);

            gaps += neighbors.Count(x => variables.GapTop(index) + variables.GapBottom(x) >= 16);

            neighbors = variables.GetBuildingsOnRight(index);
            gaps     += neighbors.Count(x => variables.GapRight(index) + variables.GapLeft(x) >= 16);

            neighbors = variables.GetBuildingsBelow(index);
            gaps     += neighbors.Count(x => variables.GapBottom(index) + variables.GapTop(x) >= 16);

            neighbors = variables.GetBuildingsOnLeft(index);
            gaps     += neighbors.Count(x => variables.GapLeft(index) + variables.GapRight(x) >= 16);

            return(gaps);
        }
Пример #4
0
        public override int HeuristicValue(List <int> valuesIndex, int variableIndex, SetVariables variables)
        {
            if (variables.PossibleValues(variableIndex)[valuesIndex[0]] == -1)
            {
                return(valuesIndex[0]);
            }
            else
            {
                double minDist = double.MaxValue;
                double tmp;
                int    minIndex = valuesIndex[0];
                foreach (var v in valuesIndex)
                {
                    tmp = variables.RealDistanceSTTo(variables.PossibleValues(variableIndex)[v]);
                    if (minDist > tmp)
                    {
                        minDist  = tmp;
                        minIndex = v;
                    }
                }

                return(minIndex);
            }
        }
Пример #5
0
        static void Main(string[] args)
        {
            var unbuildables = new List <SetVariables.Point> {
                new SetVariables.Point(7, 12),
                new SetVariables.Point(7, 13),
                new SetVariables.Point(7, 14),
                new SetVariables.Point(7, 15),
                new SetVariables.Point(8, 10),
                new SetVariables.Point(8, 11),
                new SetVariables.Point(8, 12),
                new SetVariables.Point(8, 13),
                new SetVariables.Point(8, 14),
                new SetVariables.Point(8, 15),
                new SetVariables.Point(9, 10),
                new SetVariables.Point(9, 11),
                new SetVariables.Point(9, 12),
                new SetVariables.Point(9, 13),
                new SetVariables.Point(9, 14),
                new SetVariables.Point(9, 15),
                new SetVariables.Point(10, 8),
                new SetVariables.Point(10, 9),
                new SetVariables.Point(10, 10),
                new SetVariables.Point(10, 11),
                new SetVariables.Point(10, 12),
                new SetVariables.Point(10, 13),
                new SetVariables.Point(10, 14),
                new SetVariables.Point(10, 15),
                new SetVariables.Point(11, 8),
                new SetVariables.Point(11, 9),
                new SetVariables.Point(11, 10),
                new SetVariables.Point(11, 11),
                new SetVariables.Point(11, 12),
                new SetVariables.Point(11, 13),
                new SetVariables.Point(11, 14),
                new SetVariables.Point(11, 15)
            };

            var domain = new ghost.Domain(16 * 12, 0);

            var listBuildings = MakeTerranBuildings(domain);

            var setBuildings = new SetVariables(listBuildings,
                                                12,
                                                16,
                                                new SetVariables.Point(11, 7),
                                                new SetVariables.Point(6, 15));

            setBuildings.Unbuildable(unbuildables);

            var constraints = new List <Constraint> {
                new OverLap(setBuildings),
                new Buildable(setBuildings),
                new WallShape(setBuildings),
                new StartingTargetTiles(setBuildings)
            };

            var objective = new GapObjective();

            var solver = new ghost.Solver <Variable, SetVariables, Constraint>(setBuildings, constraints, objective);

            Console.WriteLine("Start solving trivial test");
            solver.solve(20, 150, false);


            // 3.4 GHz, mono 20,150 7 tours 44 iterations
            //            C++      12 tours 400 iteratons
        }
Пример #6
0
 public StartingTargetTiles(SetVariables variables) : base(variables)
 {
 }
Пример #7
0
 public WallShape(SetVariables variables) : base(variables)
 {
 }
Пример #8
0
 public Buildable(SetVariables variables) : base(variables)
 {
 }
Пример #9
0
 public OverLap(SetVariables variables) : base(variables)
 {
 }