الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

خورازمية البحث A*

مغلقمُجاب
بدأه روحاء ~ في 24 مارس 2013 · 3 رد · 670 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم ورحمة الله وبركاته
 
مطلوب مني برنامج يقوم بإيجاد أفضل مسار ( الطريق الأقصر ) بين المدينة الهدف والمدن الأخرى ،
هذا البرنامج لا أعلم مالخطأ فيه أتمنى مساعدتي ولكم جزيل الشكر.

 

 

AStarNode.java

import java.util.List;

/**
  The AStarNode class, along with the AStarSearch class,
  implements a generic A* search algorithm. The AStarNode
  class should be subclassed to provide searching capability.
*/
public abstract class AStarNode implements Comparable {

  AStarNode pathParent;
  float costFromStart;
  float estimatedCostToGoal;


  public float getCost() {
    return costFromStart + estimatedCostToGoal;
  }


  public int compareTo(Object other) {
    float thisValue = this.getCost();
    float otherValue = ((AStarNode)other).getCost();

    float v = thisValue - otherValue;
    return (v>0)?1:(v<0)?-1:0; // sign function
  }


  /**
    Gets the cost between this node and the specified
    adjacent (AKA "neighbor" or "child") node.
  */
  public abstract float getCost(AStarNode node);


  /**
    Gets the estimated cost between this node and the
    specified node. The estimated cost should never exceed
    the true cost. The better the estimate, the more
    effecient the search.
  */
  public abstract float getEstimatedCost(AStarNode node);


  /**
    Gets the children (AKA "neighbors" or "adjacent nodes")
    of this node.
  */
  public abstract List getNeighbors();
}

 

 

AStarSearch.java

 

import java.util.*;

/**
  The AStarSearch class, along with the AStarNode class,
  implements a generic A* search algorithm. The AStarNode
  class should be subclassed to provide searching capability.
*/
public class AStarSearch {


  /**
    A simple priority list, also called a priority queue.
    Objects in the list are ordered by their priority,
    determined by the object's Comparable interface.
    The highest priority item is first in the list.
  */
  public static class PriorityList extends LinkedList {

    public void add(Comparable object) {
      for (int i=0; i<size(); i++) {
        if (object.compareTo(get(i)) <= 0) {
          add(i, object);
          return;
        }
      }
      addLast(object);
    }
  }


  /**
    Construct the path, not including the start node.
  */
  protected List constructPath(AStarNode node) {
    LinkedList path = new LinkedList();
    while (node.pathParent != null) {
      path.addFirst(node);
      node = node.pathParent;
    }
    return path;
  }


  /**
    Find the path from the start node to the end node. A list
    of AStarNodes is returned, or null if the path is not
    found. 
  */
  public List findPath(AStarNode startNode, AStarNode goalNode) {

    PriorityList openList = new PriorityList();
    LinkedList closedList = new LinkedList();

    startNode.costFromStart = 0;
    startNode.estimatedCostToGoal =
      startNode.getEstimatedCost(goalNode);
    startNode.pathParent = null;
    openList.add(startNode);

    while (!openList.isEmpty()) {
      AStarNode node = (AStarNode)openList.removeFirst();
      if (node == goalNode) {
        // construct the path from start to goal
        return constructPath(goalNode);
      }

      List neighbors = node.getNeighbors();
      for (int i=0; i<neighbors.size(); i++) {
        AStarNode neighborNode =
          (AStarNode)neighbors.get(i);
        boolean isOpen = openList.contains(neighborNode);
        boolean isClosed =
          closedList.contains(neighborNode);
        float costFromStart = node.costFromStart +
          node.getCost(neighborNode);

        // check if the neighbor node has not been
        // traversed or if a shorter path to this
        // neighbor node is found.
        if ((!isOpen && !isClosed) ||
          costFromStart < neighborNode.costFromStart)
        {
          neighborNode.pathParent = node;
          neighborNode.costFromStart = costFromStart;
          neighborNode.estimatedCostToGoal =
            neighborNode.getEstimatedCost(goalNode);
          if (isClosed) {
            closedList.remove(neighborNode);
          }
          if (!isOpen) {
            openList.add(neighborNode);
          }
        }
      }
      closedList.add(node);
    }

    // no path found
    return null;
  }

}
#2

معقول محد عارف :(

الدكتور سمح لنا بنسخه من النت وأنا نسخته بس الظاهر مافي دالة المين ، أحد يساعدني ووعد بدعيله في الحرم قدام الكعبة :rolleyes:

#3 أفضل إجابة

السلام عليكم 

 

طبعاً الكود حق الAStarNode هو كود مجرد abstract عام .. تحتاج انك تعمل implementation للكلاس AStarNode وبعدها تنادي الفنكشن findPath 

 

بالعموم الطريقة كالتالي/

 

الموجود عندك: DAG (Directed Acyclic Graph) or Tree 

هذا يتضمن وجود طريقة لتكوين الsuccessors للنود الحالي. - هذا الامر تحصل عليه لما تعمل الimplementation للكلاس نود.  

 

 بعدها تضيف النودز في ال Priority Queue حسب الf(x)=g(x)+h(x 

 

بعدها تمشي في الPQ لما تلقى  Path او null مافيه باث. 

 

اي سؤال حاضرين

#4

^

 

جزاك الله خير وأسعدك

لم أدرس تنفيذ الخوارزمية برمجياً ولي فترة طويلة منقطعة عن البرمجة بحكم دراستي

لذلك أجد صعوبة في فهم الكود

بالإضافة إلى قائمة المدن التي أريد إضافتها في الكود لا أعلم أين أضيفها :(

 

من منكم يستطيع إيجاد الكود كامل أو إضافة النقاط التي ذكرها الأستاذ عيسى!

كل الشكر ولن أنسى وعدي.

−1

هذا الموضوع مغلق.

مواضيع مشابهة