示例#1
0
 public State(Jar barrel, Jar jar1, Jar jar2)
 {
     Barrel          = barrel;
     Jar1            = jar1;
     Jar2            = jar2;
     FitnessFunction = CalculateFitnessFunction();
 }
示例#2
0
        static void Main(string[] args)
        {
            var barrel = new Jar(12, 12);
            var jar1   = new Jar(5, 0);
            var jar2   = new Jar(7, 0);

            var startState = new State(barrel, jar1, jar2);

            var tabu = new Tabu();


            tabu.Search(startState);


            Console.ReadKey();
        }
示例#3
0
        public List <State> GenerateChildrenStates(State state)
        {
            List <State> ChildrenStates = new List <State>();

            Jar barrel;
            Jar jar1;
            Jar jar2;

            //for state.Barrel
            if (state.Barrel.ActualCapacity != state.Barrel.MaxCapacity)                                       //if a barrel is not full
            {
                if (state.Jar1.ActualCapacity != 0)                                                            //if state.Jar1 is not empty
                {
                    if (state.Jar1.ActualCapacity >= (state.Barrel.MaxCapacity - state.Barrel.ActualCapacity)) //if jar1 has more water than needed
                    {
                        barrel = new Jar(state.Barrel.MaxCapacity, state.Barrel.MaxCapacity);
                        jar1   = new Jar(state.Jar1.MaxCapacity, state.Jar1.ActualCapacity - (state.Barrel.MaxCapacity - state.Barrel.ActualCapacity));
                        jar2   = new Jar(state.Jar2.MaxCapacity, state.Jar2.ActualCapacity);
                        ChildrenStates.Add(new State(barrel, jar1, jar2));
                    }
                    else//jar1 has less water than needed
                    {
                        barrel = new Jar(state.Barrel.MaxCapacity, state.Barrel.ActualCapacity + state.Jar1.ActualCapacity);
                        jar1   = new Jar(state.Jar1.MaxCapacity, 0);
                        jar2   = new Jar(state.Jar2.MaxCapacity, state.Jar2.ActualCapacity);
                        ChildrenStates.Add(new State(barrel, jar1, jar2));
                    }
                }

                if (state.Jar2.ActualCapacity != 0)                                                            //if state.Jar2 is not empty
                {
                    if (state.Jar2.ActualCapacity >= (state.Barrel.MaxCapacity - state.Barrel.ActualCapacity)) //if jar2 has more water than needed
                    {
                        barrel = new Jar(state.Barrel.MaxCapacity, state.Barrel.MaxCapacity);
                        jar2   = new Jar(state.Jar2.MaxCapacity, state.Jar2.ActualCapacity - (state.Barrel.MaxCapacity - state.Barrel.ActualCapacity));
                        jar1   = new Jar(state.Jar1.MaxCapacity, state.Jar1.ActualCapacity);
                        ChildrenStates.Add(new State(barrel, jar1, jar2));
                    }
                    else//if jar2 has less water than needed
                    {
                        barrel = new Jar(state.Barrel.MaxCapacity, state.Barrel.ActualCapacity + state.Jar2.ActualCapacity);
                        jar2   = new Jar(state.Jar2.MaxCapacity, 0);
                        jar1   = new Jar(state.Jar1.MaxCapacity, state.Jar1.ActualCapacity);
                        ChildrenStates.Add(new State(barrel, jar1, jar2));
                    }
                }
            }
            //------------------------------------------------------------------------------------------------------------------------------------
            //For state.Jar1
            if (state.Jar1.ActualCapacity != state.Jar1.MaxCapacity) //if a jar1 is not full
            {
                if (state.Barrel.ActualCapacity != 0)                //if state.Barrel is not empty
                {
                    if (state.Barrel.ActualCapacity >= (state.Jar1.MaxCapacity - state.Jar1.ActualCapacity))
                    {
                        jar1   = new Jar(state.Jar1.MaxCapacity, state.Jar1.MaxCapacity);
                        barrel = new Jar(state.Barrel.MaxCapacity, state.Barrel.ActualCapacity - (state.Jar1.MaxCapacity - state.Jar1.ActualCapacity));
                        jar2   = new Jar(state.Jar2.MaxCapacity, state.Jar2.ActualCapacity);
                        ChildrenStates.Add(new State(barrel, jar1, jar2));
                    }
                    else
                    {
                        jar1   = new Jar(state.Jar1.MaxCapacity, state.Jar1.ActualCapacity + state.Barrel.ActualCapacity);
                        barrel = new Jar(state.Barrel.MaxCapacity, 0);
                        jar2   = new Jar(state.Jar2.MaxCapacity, state.Jar2.ActualCapacity);
                        ChildrenStates.Add(new State(barrel, jar1, jar2));
                    }
                }

                if (state.Jar2.ActualCapacity != 0)//if state.Jar2 is not empty
                {
                    if (state.Jar2.ActualCapacity >= (state.Jar1.MaxCapacity - state.Jar1.ActualCapacity))
                    {
                        jar1   = new Jar(state.Jar1.MaxCapacity, state.Jar1.MaxCapacity);
                        jar2   = new Jar(state.Jar2.MaxCapacity, state.Jar2.ActualCapacity - (state.Jar1.MaxCapacity - state.Jar1.ActualCapacity));
                        barrel = new Jar(state.Barrel.MaxCapacity, state.Barrel.ActualCapacity);
                        ChildrenStates.Add(new State(barrel, jar1, jar2));
                    }
                    else
                    {
                        jar1   = new Jar(state.Jar1.MaxCapacity, state.Jar1.ActualCapacity + state.Jar2.ActualCapacity);
                        jar2   = new Jar(state.Jar2.MaxCapacity, 0);
                        barrel = new Jar(state.Barrel.MaxCapacity, state.Barrel.ActualCapacity);
                        ChildrenStates.Add(new State(barrel, jar1, jar2));
                    }
                }
            }
            //------------------------------------------------------------------------------------------------------------------------------------------
            //For state.Jar2
            if (state.Jar2.ActualCapacity != state.Jar2.MaxCapacity) //if a jar2 is not full
            {
                if (state.Barrel.ActualCapacity != 0)                //if state.Barrel is not empty
                {
                    if (state.Barrel.ActualCapacity >= (state.Jar2.MaxCapacity - state.Jar2.ActualCapacity))
                    {
                        jar2   = new Jar(state.Jar2.MaxCapacity, state.Jar2.MaxCapacity);
                        barrel = new Jar(state.Barrel.MaxCapacity, state.Barrel.ActualCapacity - (state.Jar2.MaxCapacity - state.Jar2.ActualCapacity));
                        jar1   = new Jar(state.Jar1.MaxCapacity, state.Jar1.ActualCapacity);
                        ChildrenStates.Add(new State(barrel, jar1, jar2));
                    }
                    else
                    {
                        jar2   = new Jar(state.Jar2.MaxCapacity, state.Jar2.ActualCapacity + state.Barrel.ActualCapacity);
                        barrel = new Jar(state.Barrel.MaxCapacity, 0);
                        jar1   = new Jar(state.Jar1.MaxCapacity, state.Jar1.ActualCapacity);
                        ChildrenStates.Add(new State(barrel, jar1, jar2));
                    }
                }

                if (state.Jar1.ActualCapacity != 0)//if state.Jar1 is not empty
                {
                    if (state.Jar1.ActualCapacity >= (state.Jar2.MaxCapacity - state.Jar2.ActualCapacity))
                    {
                        jar2   = new Jar(state.Jar2.MaxCapacity, state.Jar2.MaxCapacity);
                        jar1   = new Jar(state.Jar1.MaxCapacity, state.Jar1.ActualCapacity - (state.Jar2.MaxCapacity - state.Jar2.ActualCapacity));
                        barrel = new Jar(state.Barrel.MaxCapacity, state.Barrel.ActualCapacity);
                        ChildrenStates.Add(new State(barrel, jar1, jar2));
                    }
                    else
                    {
                        jar2   = new Jar(state.Jar2.MaxCapacity, state.Jar2.ActualCapacity + state.Jar1.ActualCapacity);
                        jar1   = new Jar(state.Jar1.MaxCapacity, 0);
                        barrel = new Jar(state.Barrel.MaxCapacity, state.Barrel.ActualCapacity);
                        ChildrenStates.Add(new State(barrel, jar1, jar2));
                    }
                }
            }
            //---------------------------------------------------------------------------------------------------------------------------------------------
            ChildrenStates.OrderBy(f => f.FitnessFunction);//sort children states by ascending of the fitness function

            return(ChildrenStates);
        }