Nama : Timothy Richard Sugianto
NIM : 1601261065
Kelas : 04PUT
Mata Kuliah : Intelegensia Semu
Dosen : Afan Galih Salman, ST., M.SI
Soal
1. Apa yang dimaksud Adversarial Search & Constraint Satisfaction Problems? Berikan Contoh
2. Apa itu Propositional Logic? Berikan Contoh
3. Buat Coding (c,c++ atau java) untuk algoritma A & algoritma A* (A star)
Jawaban
1. Adversarial Search Problem adalah jenis pencarian yang memecahkan permasalahan dimana
mengharuskan kita untuk memperhatikan semua kemungkinan gerak dari lawan yang mungkin
terjadi. Untuk melakukannya, biasanya menggunakan algoritma pencarian seperti algoritma
minimaks, dan Alpha beta Prunning. Contoh : permainan Tic Tac Toe
Constraint Satisfaction Problem adalah permasalahan yang tujuannya adalah mendapatkan
suatu kombinasi variabel-variabel tertentu yang memenuhi aturan-aturan (constraints) tertentu.
Contohnya : Map Coloring
2. Propositional Logic, atau dikenal sebagai sentential logic adalah Sebuah cabang logika yang
mempelajari cara menyatukan atau mengubah pernyataan ke bentuk pernyataan yang lebih
rumit. Penggabungan 2 pernyataan simpe dengan kata “dan” adalah salah satu contoh umum
dalam menyatukan pernyataan. Ketika 2 pernyataan di gabungkan dengan “dan”, kompleksitas
pernyataan yang dibentuk adalah benar jika dan hanya jika kedua komponen pernyataan
tersebut adalah benar. Oleh karena ini, argument dibawah ini secara logika adalah benar :
Paris adalah ibukota Prancis DAN paris memiliki populasi lebih dari 2 juta.
Oleh karena itu, Paris memiliki populasi lebih dari 2 juta.
Contoh :
1. George W. Bush adalah presiden US.
2. George W. Bush adalah anak dari presiden US.
3. Oleh karena itu, ada seseorang yang merupakan presiden US dan sekaligus merupakan anak
dari presiden US
3. Algoritma A
final class NodeData<T> {
private final T nodeId;
private final Map<T, Double> heuristic;
private double g;
private double h;
private double f;
public NodeData (T nodeId, Map<T, Double> heuristic) {
this.nodeId = nodeId;
this.g = Double.MAX_VALUE;
this.heuristic = heuristic;
}
public T getNodeId() {
return nodeId;
}
public double getG() {
return g;
}
public void setG(double g) {
this.g = g;
}
public void calcF(T destination) {
this.h = heuristic.get(destination);
this.f = g + h;
}
public double getH() {
return h;
}
public double getF() {
return f;
}
}
final class GraphAStar<T> implements Iterable<T> {
private final Map<T, Map<NodeData<T>, Double>> graph;
private final Map<T, Map<T, Double>> heuristicMap;
private final Map<T, NodeData<T>> nodeIdNodeData;
public GraphAStar(Map<T, Map<T, Double>> heuristicMap) {
if (heuristicMap == null) throw new NullPointerException(“The huerisic map should not
benull”);
graph = new HashMap<T, Map<NodeData<T>, Double>>();
nodeIdNodeData = new HashMap<T, NodeData<T>>();
this.heuristicMap = heuristicMap;
}
public void addNode(T nodeId) {
if (nodeId == null) throw new NullPointerException(“The node cannot be null”);
if (!heuristicMap.containsKey(nodeId)) throw new NoSuchElementException(“This node
isnot a part of euristic map”);
graph.put(nodeId, new HashMap<NodeData<T>, Double>());
nodeIdNodeData.put(nodeId, new NodeData<T>(nodeId, heuristicMap.get(nodeId)));
}
public void addEdge(T nodeIdFirst, T nodeIdSecond, double length) {
if (nodeIdFirst == null || nodeIdSecond == null) throw new NullPointerException(“The first
nor second node can be null.”);
if (!heuristicMap.containsKey(nodeIdFirst) || !heuristicMap.containsKey(nodeIdSecond)) {
throw new NoSuchElementException(“Source and Destination both should be part of the part
of euristic map”);
}
if (!graph.containsKey(nodeIdFirst) || !graph.containsKey(nodeIdSecond)) {
throw new NoSuchElementException(“Source and Destination both should be part of the part
of graph”);
}
graph.get(nodeIdFirst).put(nodeIdNodeData.get(nodeIdSecond), length);
graph.get(nodeIdSecond).put(nodeIdNodeData.get(nodeIdFirst), length);
}
public Map<NodeData<T>, Double> edgesFrom (T nodeId) {
if (nodeId == null) throw new NullPointerException(“The input node should not be null.”);
if (!heuristicMap.containsKey(nodeId)) throw new NoSuchElementException(“This node is not a
part of euristic map”);
if (!graph.containsKey(nodeId)) throw new NoSuchElementException(“The node should not be
null.”);
return Collections.unmodifiableMap(graph.get(nodeId));
}
public NodeData<T> getNodeData (T nodeId) {
if (nodeId == null) { throw new NullPointerException(“The nodeid should not be empty”); }
if (!nodeIdNodeData.containsKey(nodeId)) { throw new NoSuchElementException(“The nodeId
does not exist”); }
return nodeIdNodeData.get(nodeId);
}
@Override public Iterator<T> iterator() {
return graph.keySet().iterator();
}
}
public class Astar<T>
private final GraphAStar<T> graph;
public Astar (GraphAStar<T> graphAStar) {
this.graph = graphAStar;
}
public class NodeComparator implements Comparator<NodeData<T>> {
public int compare(NodeData<T> nodeFirst, NodeData<T> nodeSecond) {
if (nodeFirst.getF() > nodeSecond.getF()) return 1;
if (nodeSecond.getF() > nodeFirst.getF()) return -1;
return 0;
}
}
public List<T> astar(T source, T destination) {
final Queue<NodeData<T>> openQueue = new PriorityQueue<NodeData<T>>(11, new
NodeComparator());
NodeData<T> sourceNodeData = graph.getNodeData(source);
sourceNodeData.setG(0);
sourceNodeData.calcF(destination);
openQueue.add(sourceNodeData);
final Map<T, T> path = new HashMap<T, T>();
final Set<NodeData<T>> closedList = new HashSet<NodeData<T>>();
while (!openQueue.isEmpty()) {
final NodeData<T> nodeData = openQueue.poll();
if (nodeData.getNodeId().equals(destination)) {
return path(path, destination);
}
closedList.add(nodeData);
for (Entry<NodeData<T>, Double> neighborEntry :
graph.edgesFrom(nodeData.getNodeId()).entrySet()) {
NodeData<T> neighbor = neighborEntry.getKey();
if (closedList.contains(neighbor)) continue;
double distanceBetweenTwoNodes = neighborEntry.getValue();
double tentativeG = distanceBetweenTwoNodes + nodeData.getG();
if (tentativeG < neighbor.getG()) {
neighbor.setG(tentativeG);
neighbor.calcF(destination);
path.put(neighbor.getNodeId(), nodeData.getNodeId());
if (!openQueue.contains(neighbor)) {
openQueue.add(neighbor);
}
}
}
}
return null;
}
private List<T> path(Map<T, T> path, T destination) {
assert path != null;
assert destination != null;
final List<T> pathList = new ArrayList<T>();
pathList.add(destination);
while (path.containsKey(destination)) {
destination = path.get(destination);
pathList.add(destination);
}
Collections.reverse(pathList);
return pathList;
}
public static void main(String[] args) {
Map<String, Map<String, Double>> euristic = new HashMap<String, Map<String, Double>>();
Map<String, Double> mapA = new HashMap<String, Double>();
mapA.put(“A”, 0.0);
mapA.put(“B”, 10.0);
mapA.put(“C”, 20.0);
mapA.put(“E”, 100.0);
mapA.put(“F”, 110.0);
Map<String, Double> mapB = new HashMap<String, Double>();
mapB.put(“A”, 10.0);
mapB.put(“B”, 0.0);
mapB.put(“C”, 10.0);
mapB.put(“E”, 25.0);
mapB.put(“F”, 40.0);
Map<String, Double> mapC = new HashMap<String, Double>();
mapC.put(“A”, 20.0);
mapC.put(“B”, 10.0);
mapC.put(“C”, 0.0);
mapC.put(“E”, 10.0);
mapC.put(“F”, 30.0);
Map<String, Double> mapX = new HashMap<String, Double>();
mapX.put(“A”, 100.0);
mapX.put(“B”, 25.0);
mapX.put(“C”, 10.0);
mapX.put(“E”, 0.0);
mapX.put(“F”, 10.0);
Map<String, Double> mapZ = new HashMap<String, Double>();
mapZ.put(“A”, 110.0);
mapZ.put(“B”, 40.0);
mapZ.put(“C”, 30.0);
mapZ.put(“E”, 10.0);
mapZ.put(“F”, 0.0);
euristic.put(“A”, mapA);
euristic.put(“B”, mapB);
euristic.put(“C”, mapC);
euristic.put(“E”, mapX);
euristic.put(“F”, mapZ);
GraphAStar<String> graph = new GraphAStar<String>(euristic);
graph.addNode(“A”);
graph.addNode(“B”);
graph.addNode(“C”);
graph.addNode(“E”);
graph.addNode(“F”);
graph.addEdge(“A”, “B”, 10);
graph.addEdge(“A”, “E”, 100);
graph.addEdge(“B”, “C”, 10);
graph.addEdge(“C”, “E”, 10);
graph.addEdge(“C”, “F”, 30);
graph.addEdge(“E”, “F”, 10);
Astar<String> aStar = new Astar<String>(graph);
for (String path : aStar.astar(“A”, “F”)) {
System.out.println(path);
}
}
}
Algoritma A *
package aStar;
import java.util.ArrayList;
import java.util.Collections;
import aStar.heuristics.AStarHeuristic;
import aStar.utils.Logger;
public class AStar {
private AreaMap map;
private AStarHeuristic heuristic;
//private int startX;
//private int startY;
//private int goalX;
//private int goalY;
/**
* closedList The list of Nodes not searched yet, sorted by their distance to
the goal as guessed by our heuristic.
*/
private ArrayList<Node> closedList;
private SortedNodeList openList;
private Path shortestPath;
Logger log = new Logger();
AStar(AreaMap map, AStarHeuristic heuristic) {
this.map = map;
this.heuristic = heuristic;
closedList = new ArrayList<Node>();
openList = new SortedNodeList();
}
public Path calcShortestPath(int startX, int startY, int goalX, int goalY) {
//this.startX = startX;
//this.startY = startY;
//this.goalX = goalX;
//this.goalY = goalY;
//mark start and goal node
map.setStartLocation(startX, startY);
map.setGoalLocation(goalX, goalY);
//Check if the goal node is blocked (if it is, it is impossible to
find a path there)
if (map.getNode(goalX, goalY).isObstacle) {
return null;
}
map.getStartNode().setDistanceFromStart(0);
closedList.clear();
openList.clear();
openList.add(map.getStartNode());
//while we haven't reached the goal yet
while(openList.size() != 0) {
//get the first Node from non-searched Node list, sorted by
lowest distance from our goal as guessed by our heuristic
Node current = openList.getFirst();
// check if our current Node location is the goal Node. If it
is, we are done.
if(current.getX() == map.getGoalLocationX() && current.getY()
== map.getGoalLocationY()) {
return reconstructPath(current);
}
//move current Node to the closed (already searched) list
openList.remove(current);
closedList.add(current);
//go through all the current Nodes neighbors and calculate if
one should be our next step
for(Node neighbor : current.getNeighborList()) {
boolean neighborIsBetter;
//if we have already searched this Node, don't bother
and continue to the next one
if (closedList.contains(neighbor))
continue;
//also just continue if the neighbor is an obstacle
if (!neighbor.isObstacle) {
// calculate how long the path is if we
choose this neighbor as the next step in the path
float neighborDistanceFromStart =
(current.getDistanceFromStart() + map.getDistanceBetween(current, neighbor));
//add neighbor to the open list if it is not
there
if(!openList.contains(neighbor)) {
openList.add(neighbor);
neighborIsBetter = true;
//if neighbor is closer to start it
could also be better
} else if(neighborDistanceFromStart <
current.getDistanceFromStart()) {
neighborIsBetter = true;
} else {
neighborIsBetter = false;
}
// set neighbors parameters if it is better
if (neighborIsBetter) {
neighbor.setPreviousNode(current);
neighbor.setDistanceFromStart(neighbo
rDistanceFromStart);
neighbor.setHeuristicDistanceFromGoal
(heuristic.getEstimatedDistanceToGoal(neighbor.getX(), neighbor.getY(),
map.getGoalLocationX(), map.getGoalLocationY()));
}
}
}
}
return null;
}
public void printPath() {
Node node;
for(int x=0; x<map.getMapWith(); x++) {
if (x==0) {
for (int i=0; i<=map.getMapWith(); i++)
System.out.print("-");
System.out.println();
}
System.out.print("|");
for(int y=0; y<map.getMapHeight(); y++) {
node = map.getNode(x, y);
if (node.isObstacle) {
System.out.print("X");
} else if (node.isStart) {
System.out.print("s");
} else if (node.isGoal) {
System.out.print("g");
} else if (shortestPath.contains(node.getX(),
node.getY())) {
System.out.print("¤");
} else {
System.out.print(" ");
}
if (y==map.getMapHeight())
System.out.print("_");
}
System.out.print("|");
System.out.println();
}
for (int i=0; i<=map.getMapWith(); i++)
System.out.print("-");
}
private Path reconstructPath(Node node) {
Path path = new Path();
while(!(node.getPreviousNode() == null)) {
path.prependWayPoint(node);
node = node.getPreviousNode();
}
this.shortestPath = path;
return path;
}
private class SortedNodeList {
private ArrayList<Node> list = new ArrayList<Node>();
public Node getFirst() {
return list.get(0);
}
public void clear() {
list.clear();
}
public void add(Node node) {
list.add(node);
Collections.sort(list);
}
public void remove(Node n) {
list.remove(n);
}
public int size() {
return list.size();
}
public boolean contains(Node n) {
return list.contains(n);
}
}
}
© Copyright 2026 Paperzz