Exemple #1
0
        public STATE solve(ICSP <STATE> csp, STATE[] beams, int number_of_beams)
        {
            List <STATE> new_beams = beams.ToList <STATE>();

            while (new_beams.Count != 0)
            {
                MyDictionary <STATE, int> better_neighbors = new MyDictionary <STATE, int>();
                foreach (STATE beam in new_beams)
                {
                    STATE[] neighbors  = csp.neighbors_states(beam);
                    int     beam_score = csp.constraint_satisfaction(beam);
                    foreach (STATE neighbor in neighbors)
                    {
                        int score = csp.constraint_satisfaction(neighbor);
                        if (csp.total_satisfaction(score))
                        {
                            return(neighbor);
                        }
                        if (beam_score < score)
                        {
                            better_neighbors.Add(neighbor, score);
                        }
                    }
                }

                if (better_neighbors.Count() == 0)//trapped in local optimum
                {
                    STATE best_state = new_beams.First();
                    int   best_score = int.MinValue;
                    foreach (STATE beam in new_beams)
                    {
                        int score = csp.constraint_satisfaction(beam);
                        if (best_score < score)
                        {
                            best_score = score;
                            best_state = beam;
                        }
                    }
                    return(best_state);
                }

                better_neighbors.SortByValue();
                new_beams = new List <STATE>(number_of_beams);
                for (int i = 0; i < number_of_beams; i++)
                {
                    if (better_neighbors.IsEmpty())
                    {
                        break;
                    }
                    new_beams.Add(better_neighbors.Pop().Key);
                }
                number_of_beams = new_beams.Count;
            }

            throw new ArgumentException("out of beam", "state[] beams");
        }
        public STATE solve(ICSP <STATE> csp, STATE initialize_state)
        {
            STATE state = initialize_state;

            while (!csp.total_satisfaction(state) && temp > Freezing_temperature)
            {
                state = next_state(csp, state);
                cool_down();
            }
            return(state);
        }
        public override STATE next_state(ICSP <STATE> csp, STATE state)
        {
            int current_score = csp.constraint_satisfaction(state);

            STATE[] neighbors = csp.neighbors_states(state);
            foreach (STATE node in neighbors)
            {
                if (current_score < csp.constraint_satisfaction(node))
                {
                    return(node);
                }
            }
            return(state);
        }
        public STATE solve(ICSP <STATE> csp, STATE initialize_state)
        {
            STATE state = initialize_state;

            while (!csp.total_satisfaction(state))
            {
                STATE next = next_state(csp, state);
                if (csp.is_equal_state(state, next))//trapped in local optimum
                {
                    return(state);
                }
                state = next;
            }
            return(state);
        }
        public STATE next_state(ICSP <STATE> csp, STATE state)
        {
            int current_score = csp.constraint_satisfaction(state);

            STATE[] neighbors = csp.neighbors_states(state);
            foreach (STATE node in neighbors)
            {
                int neighbor_socre = csp.constraint_satisfaction(node);
                int delta          = neighbor_socre - current_score;
                if (accept(temp, delta))
                {
                    return(node);
                }
            }
            return(state);
        }
Exemple #6
0
        public override STATE next_state(ICSP <STATE> csp, STATE state)
        {
            STATE best_neighbor = state;
            int   best_score    = csp.constraint_satisfaction(state);

            STATE[] neighbors = csp.neighbors_states(state);
            foreach (STATE node in neighbors)
            {
                int score = csp.constraint_satisfaction(node);
                if (best_score < score)
                {
                    best_score    = score;
                    best_neighbor = node;
                }
            }
            return(best_neighbor);
        }
 public abstract STATE next_state(ICSP <STATE> csp, STATE state);