Skip to content
Snippets Groups Projects
Commit f0f34d1e authored by SCHILLING Juliette's avatar SCHILLING Juliette
Browse files

graphe + dijkstra ok

parent 57b7d097
No related branches found
No related tags found
No related merge requests found
......@@ -316,6 +316,8 @@ public class CreationOuvertureActivity extends AppCompatActivity{
Ouverture ouverture = new Ouverture(pieceDepart.getMurOrientation(orientationPieceDepart), pieceArrivee.getMurOrientation(orientationPieceArrivee), rectDepart, rectArrivee);
habitat.addOuverture(ouverture);
Log.i("testECrea", habitat.getOuvertures().toString());
Toast.makeText(getBaseContext(), "Ouverture créée !", Toast.LENGTH_SHORT).show();
enregistrement();
......
......@@ -13,6 +13,7 @@ import habitat.Piece;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import outils.GrapheHabitat;
import java.io.*;
......@@ -35,6 +36,10 @@ public class MainActivity extends AppCompatActivity {
ouvrirJSON();
//Graphe graphe = new Graphe(habitat);
GrapheHabitat grapheHabitat = new GrapheHabitat(habitat);
Button b = findViewById(R.id.buttonImmersion);
if(habitat.getPieces().size() == 0) {
b.setEnabled(false);
......
......@@ -18,6 +18,8 @@ import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import habitat.*;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import outils.GestionnaireEditHabitat;
......@@ -67,7 +69,7 @@ public class ModeConceptionActivity extends AppCompatActivity {
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
//On récupère les données de habitat
//On récupère les données de la photo
Intent intent = result.getData();
if(intent != null) {
Bundle extras = intent.getExtras();
......@@ -106,6 +108,17 @@ public class ModeConceptionActivity extends AppCompatActivity {
);
}
@Override
protected void onStart() {
Log.i("testOnStart", "je vais dans le OnStart");
Log.i("testEConception", habitat.getOuvertures().toString());
//On recupere habitat quand on revient de CreationOuvertureActivity
ouvrirJSON();
Log.i("testEConception", habitat.getOuvertures().toString());
super.onStart();
}
/**
* Permet d'afficher l'ensemble des pieces de l'habitat
*/
......@@ -263,4 +276,69 @@ public class ModeConceptionActivity extends AppCompatActivity {
intent.putExtra("Habitat", habitat);
startActivity(intent);
}
/**
* Permet de recuperer l'habitat enregistre
*/
public void ouvrirJSON(){
habitat = new Habitat();
FileInputStream fis = null;
try {
fis = openFileInput("enregistrement.json");
} catch (FileNotFoundException e) {
//throw new RuntimeException(e);
}
if (fis != null) {
String json = getFileContent(fis);
try {
JSONObject enregistrement = new JSONObject(json);
JSONArray pieces = enregistrement.getJSONArray("Pieces");
for(int i=0; i<pieces.length(); i++){
JSONObject Jpiece = (JSONObject) pieces.get(i);
Piece piece = new Piece(Jpiece);
habitat.addPiece(piece);
}
JSONArray ouvertures = enregistrement.getJSONArray("Ouvertures");
for(int i=0; i<ouvertures.length(); i++){
JSONObject Jouverture = (JSONObject) ouvertures.get(i);
Ouverture ouverture = new Ouverture(Jouverture);
habitat.addOuverture(ouverture);
}
Log.i("testJSONouverture", habitat.toString());
} catch (JSONException e) {
//throw new RuntimeException(e);
}
Log.i("testJSON", json);
}else{
Log.i("testJSON", "pbm ouverture");
}
//textView.setText(habitat.toString());
}
/**
* Fonction permettant de recuperer le texte dans un texte
* @param fis
* @return
*/
public String getFileContent(FileInputStream fis) {
StringBuilder sb = new StringBuilder();
Reader r = null;
try {
r = new InputStreamReader(fis, "UTF-8");
int ch = r.read();
while(ch >= 0) {
sb.append((char)ch);
ch = r.read();
}
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
return sb.toString();
}
}
\ No newline at end of file
......@@ -219,6 +219,6 @@ public class Piece implements Parcelable {
*/
@Override
public int hashCode() {
return Objects.hash(nom, murs);
return Objects.hash(nom);
}
}
package outils;
import android.util.Log;
import habitat.Habitat;
import habitat.Ouverture;
import habitat.Piece;
import java.util.*;
public class GrapheHabitat {
// A class to store a graph edge
class Edge
{
int source, dest, weight;
public Edge(int source, int dest, int weight)
{
this.source = source;
this.dest = dest;
this.weight = weight;
}
}
// A class to store a heap node
class Node
{
int vertex, weight;
public Node(int vertex, int weight)
{
this.vertex = vertex;
this.weight = weight;
}
}
// A class to represent a graph object
class Graph
{
// A list of lists to represent an adjacency list
List<List<Edge>> adjList = null;
// Constructor
Graph(List<Edge> edges, int n)
{
adjList = new ArrayList<>();
for (int i = 0; i < n; i++) {
adjList.add(new ArrayList<>());
}
// add edges to the directed graph
for (Edge edge: edges) {
adjList.get(edge.source).add(edge);
}
}
}
private Habitat habitat;
private HashMap<Integer, Piece> hmap;
private HashMap<Piece, Integer> hmapGet;
//private HashMap<Piece, List<Piece>> res;
private HashMap<Piece, HashMap<Piece, ArrayList<Piece>>> res;
public GrapheHabitat(Habitat habitat) {
this.habitat = habitat;
res = new HashMap<>();
//On initialise le num correspondant aux pieces
hmap = new HashMap<>();
hmapGet = new HashMap<>();
int i = 0;
for(Piece piece : habitat.getPieces()){
hmap.put(i, piece);
hmapGet.put(piece, i);
i++;
res.put(piece, new HashMap<>());
}
//Initialisation des resultats
for(Piece piece : habitat.getPieces()){
for(Piece pieceArrivee : habitat.getPieces()){
if(!pieceArrivee.equals(piece)){
res.get(piece).put(pieceArrivee, new ArrayList<>());
}
}
}
//On créé la list à partir des ouvertures
List<Edge> edges = new ArrayList<>();
for(Ouverture ouverture : habitat.getOuvertures()){
edges.add(new Edge(hmapGet.get(ouverture.getMurDepart().getPiece()), hmapGet.get(ouverture.getMurArrivee().getPiece()), 1));
edges.add(new Edge(hmapGet.get(ouverture.getMurArrivee().getPiece()), hmapGet.get(ouverture.getMurDepart().getPiece()), 1));
}
// total number of nodes in the graph (labelled from 0 to 4)
int n = habitat.getOuvertures().size()*2;
// construct graph
Graph graph = new Graph(edges, n);
// run the Dijkstra’s algorithm from every node
for (int source = 0; source < n; source++) {
findShortestPaths(graph, source, n);
}
//For test
/*
for(Piece p1 : habitat.getPieces()){
for(Piece p2 : habitat.getPieces()){
if(!p1.equals(p2)){
getPlusCourtChemin(p1, p2);
}
}
}
*/
}
private void getRoute(int[] prev, int i, List<Piece> route)
{
if (i >= 0)
{
getRoute(prev, prev[i], route);
route.add(hmap.get(i));
}
}
// Run Dijkstra’s algorithm on a given graph
public void findShortestPaths(Graph graph, int source, int n)
{
// create a min-heap and push source node having distance 0
PriorityQueue<Node> minHeap;
minHeap = new PriorityQueue<>(Comparator.comparingInt(node -> node.weight));
minHeap.add(new Node(source, 0));
// set initial distance from the source to `v` as infinity
List<Integer> dist;
dist = new ArrayList<>(Collections.nCopies(n, Integer.MAX_VALUE));
// distance from the source to itself is zero
dist.set(source, 0);
// boolean array to track vertices for which minimum
// cost is already found
boolean[] done = new boolean[n];
done[source] = true;
// stores predecessor of a vertex (to a print path)
int[] prev = new int[n];
prev[source] = -1;
// run till min-heap is empty
while (!minHeap.isEmpty())
{
// Remove and return the best vertex
Node node = minHeap.poll();
// get the vertex number
int u = node.vertex;
// do for each neighbor `v` of `u`
for (Edge edge: graph.adjList.get(u))
{
int v = edge.dest;
int weight = edge.weight;
// Relaxation step
if (!done[v] && (dist.get(u) + weight) < dist.get(v))
{
dist.set(v, dist.get(u) + weight);
prev[v] = u;
minHeap.add(new Node(v, dist.get(v)));
}
}
// mark vertex `u` as done so it will not get picked up again
done[u] = true;
}
List<Piece> routePiece = new ArrayList<>();
for (int i = 0; i < n; i++)
{
if (i != source && dist.get(i) != Integer.MAX_VALUE)
{
getRoute(prev, i, routePiece);
res.get(hmap.get(source)).get(hmap.get(i)).addAll(routePiece);
routePiece.clear();
}
}
}
public void getPlusCourtChemin(Piece pieceDepart, Piece pieceArrivee){
Log.i("testGraphe", "Path (" + pieceDepart.getNom() + " -> " + pieceArrivee.getNom() + ", Route = " + res.get(pieceDepart).get(pieceArrivee));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment