diff --git a/src/algoritmos/HightDegree.java b/src/algoritmos/HightDegree.java index 417d6d5d6ebab0a40c79cbb56299c5627978851f..dbee7c97c737d9fda10026cec96b4eb13036d6e4 100644 --- a/src/algoritmos/HightDegree.java +++ b/src/algoritmos/HightDegree.java @@ -17,15 +17,17 @@ import interfaces.SeedChooser; public class HightDegree implements SeedChooser<Actor> { public DirectedSocialNetwork grafo = null; + private double[] spreadData; // histograma da propagação esperada - public HightDegree(DirectedSocialNetwork grafo) { + public HightDegree(DirectedSocialNetwork g) { super(); - this.grafo = grafo; + this.grafo = g; } @Override public HashSet<Actor> escolher(int k) { HashSet<Actor> semente = new HashSet<>(); + spreadData = new double[k+1]; Set<Actor> vSet = grafo.vertexSet(); @@ -43,9 +45,14 @@ public class HightDegree implements SeedChooser<Actor> { while (semente.size() < k) { Actor maior = heapMinMax.removeLast(); semente.add(maior); + spreadData[semente.size()] = grafo.espectedSpread(semente, true); } return semente; } + + public double[] getSpreadData(){ + return this.spreadData; + } public static void main(String[] args) { DirectedSocialNetwork g = new SocialNetworkGenerate() diff --git a/src/algoritmos/LazyGreedy.java b/src/algoritmos/LazyGreedy.java index d1565fb50a60a7ec9f7d94a55a77b315d61bd48c..63cabe4ee6fc048cbd240ff57fe0aa36b010a2fc 100644 --- a/src/algoritmos/LazyGreedy.java +++ b/src/algoritmos/LazyGreedy.java @@ -5,11 +5,17 @@ import grafos.Actor; import grafos.DirectedSocialNetwork; import interfaces.SeedChooser; +import java.io.FileWriter; +import java.io.IOException; import java.util.HashSet; import java.util.PriorityQueue; public class LazyGreedy implements SeedChooser<Actor> { private DirectedSocialNetwork grafo = null; + private int cont; + private double[] spreadData; + private double[] callData; + public LazyGreedy(DirectedSocialNetwork g) { this.grafo = g; @@ -35,6 +41,9 @@ public class LazyGreedy implements SeedChooser<Actor> { @Override public HashSet<Actor> escolher(int k) { HashSet<Actor> semente = new HashSet<>(); + cont = 0; + spreadData = new double[k+1]; + callData = new double[k+1]; // create priority queue of all nodes, with marginal gain delta +inf PriorityQueue<MarginalGain> fila = priorityQueueOfGains(); @@ -52,6 +61,9 @@ public class LazyGreedy implements SeedChooser<Actor> { if (max.isValid() == true) { semente.add(max.getVertice()); MaxSpread = MaxSpread + max.getGain(); +// System.out.println(semente.size()+"\t"+MaxSpread); + spreadData[semente.size()] = MaxSpread; + callData[semente.size()] = cont; break; } else { double sigma = cascata(max.getVertice(), semente); @@ -59,11 +71,58 @@ public class LazyGreedy implements SeedChooser<Actor> { max.setGain(delta); max.setValid(true); fila.add(max); + cont++; } } -// System.out.println("spread: " + MaxSpread); } + System.out.println("chamadas: " + cont); + return semente; + } + + public int callToSigma(){ + return this.cont; + } + public double[] getSpreadData(){ + return this.spreadData; + } + + public double[] getCallData(){ + return this.callData; + } + + public HashSet<Actor> toFile(int k, FileWriter writer) throws IOException { + HashSet<Actor> semente = new HashSet<>(); + cont = 0; + + // create priority queue of all nodes, with marginal gain delta +inf + PriorityQueue<MarginalGain> fila = priorityQueueOfGains(); + + double MaxSpread = 0; + + while (semente.size() < k) { + // set all gains invalid + for (MarginalGain mg : fila) { + mg.setValid(false); + } + while (true) { + MarginalGain max = fila.poll(); + if (max.isValid() == true) { + semente.add(max.getVertice()); + MaxSpread = MaxSpread + max.getGain(); + writer.write(semente.size()+"\t"+MaxSpread); + break; + } else { + double sigma = cascata(max.getVertice(), semente); + double delta = sigma - MaxSpread; + max.setGain(delta); + max.setValid(true); + fila.add(max); + cont++; + } + } + } +// System.out.println("chamadas: " + cont); return semente; } @@ -76,7 +135,7 @@ public class LazyGreedy implements SeedChooser<Actor> { public static void main(String[] args) { DirectedSocialNetwork g; - g = new SocialNetworkGenerate().gerarGrafo(500, 2.8); + g = new SocialNetworkGenerate().gerarGrafo(2000, 2.5); long startTime = 0; startTime = System.nanoTime(); diff --git a/src/algoritmos/PrevalentSeed.java b/src/algoritmos/PrevalentSeed.java index f882993e2e82bfc3672ee00be539490a6349b4f4..64b827cc3f72e6708de7f508e33135cc2082ebb0 100644 --- a/src/algoritmos/PrevalentSeed.java +++ b/src/algoritmos/PrevalentSeed.java @@ -23,11 +23,15 @@ import interfaces.SeedChooser; */ public class PrevalentSeed implements SeedChooser<Actor> { private DirectedSocialNetwork grafo; + private int cont; // número de chamadas a sigma + private double[] spreadData; // histograma da propagação esperada + private double[] callData; + public PrevalentSeed(DirectedSocialNetwork g) { this.grafo = g; } - + public HashSet<Actor> preSelecao(DirectedSocialNetwork grafo) { int n = grafo.vertexSet().size(); @@ -38,15 +42,14 @@ public class PrevalentSeed implements SeedChooser<Actor> { ComparaPorGrau comp = new ComparaPorGrau(grafo); MinMaxPriorityQueue<Actor> heapMinMax = null; - heapMinMax = MinMaxPriorityQueue.orderedBy(comp).maximumSize(n) - .create(); + heapMinMax = MinMaxPriorityQueue.orderedBy(comp).maximumSize(n).create(); for (Actor v : grafo.vertexSet()) { heapMinMax.add(v); } Set<Actor> cobertos = new HashSet<>(); while (cobertos.size() < n) { - + Actor v = heapMinMax.removeLast(); Set<Actor> vizinhos = grafo.outNeighborsOf(v); @@ -57,7 +60,7 @@ public class PrevalentSeed implements SeedChooser<Actor> { } return candidatos; } - + public HashSet<Actor> preSelecaoOriginal(DirectedSocialNetwork grafo) { int n = grafo.vertexSet().size(); @@ -68,22 +71,21 @@ public class PrevalentSeed implements SeedChooser<Actor> { ComparaPorGrau comp = new ComparaPorGrau(grafo); MinMaxPriorityQueue<Actor> heapMinMax = null; - heapMinMax = MinMaxPriorityQueue.orderedBy(comp).maximumSize(n) - .create(); + heapMinMax = MinMaxPriorityQueue.orderedBy(comp).maximumSize(n).create(); for (Actor v : grafo.vertexSet()) { heapMinMax.add(v); } Set<Actor> cobertos = new HashSet<>(); while (!heapMinMax.isEmpty()) { - + Actor v = heapMinMax.removeLast(); Set<Actor> vizinhos = grafo.outNeighborsOf(v); if (cobertos.addAll(vizinhos)) { candidatos.add(v); + cobertos.add(v); } - cobertos.add(v); } return candidatos; } @@ -92,8 +94,11 @@ public class PrevalentSeed implements SeedChooser<Actor> { public HashSet<Actor> escolher(int k) { HashSet<Actor> semente = new HashSet<Actor>(); HashSet<Actor> candidatos = new HashSet<Actor>(); + spreadData = new double[k+1]; + callData = new double[k+1]; candidatos = preSelecaoOriginal(grafo); +// System.out.println("|C| = "+candidatos.size()); if (candidatos.size() < k) { System.out.println("Erro: o cojunto de candidatos é menor que K"); @@ -103,7 +108,9 @@ public class PrevalentSeed implements SeedChooser<Actor> { // create priority queue of all nodes, with marginal gain delta +inf PriorityQueue<MarginalGain> fila = priorityQueueOfGains(candidatos); - double MaxSpread = 0; + double maxSpread = 0; + spreadData[0] = 0; + cont = 0; while (semente.size() < k) { // set all gains invalid @@ -115,38 +122,53 @@ public class PrevalentSeed implements SeedChooser<Actor> { MarginalGain max = fila.poll(); if (max.isValid() == true) { semente.add(max.getVertice()); - MaxSpread = MaxSpread + max.getGain(); + maxSpread = maxSpread + max.getGain(); +// System.out.println(semente.size()+"\t"+maxSpread); + callData[semente.size()] = cont; + spreadData[semente.size()] = maxSpread; break; } else { double sigma = cascata(max.getVertice(), semente); - double delta = sigma - MaxSpread; + double delta = sigma - maxSpread; max.setGain(delta); max.setValid(true); fila.add(max); + cont++; // Conta o numero de chamadas à sigma } } } - + System.out.println("chamadas: "+cont); return semente; } - - public HashSet<Actor> escolher2(int k) { + + public int callToSigma(){ + return this.cont; + } + public double[] getSpreadData(){ + return this.spreadData; + } + + public double[] getCallData(){ + return this.callData; + } + + public HashSet<Actor> toFile(int k) { HashSet<Actor> semente = new HashSet<Actor>(); - HashSet<Actor> minSet = new HashSet<Actor>(); + HashSet<Actor> candidatos = new HashSet<Actor>(); + cont = 0; - MinDominatingSet ds = new MinDominatingSet(); - minSet = ds.fastGreedy2(grafo); + candidatos = preSelecaoOriginal(grafo); +// System.out.println("|C| = "+candidatos.size()); - System.out.println("|DS| = " + minSet.size()); - if (minSet.size() < k) { - System.out.println("Erro: o cojunto domintante é menor que K"); + if (candidatos.size() < k) { + System.out.println("Erro: o cojunto de candidatos é menor que K"); return null; } // create priority queue of all nodes, with marginal gain delta +inf - PriorityQueue<MarginalGain> fila = priorityQueueOfGains(minSet); + PriorityQueue<MarginalGain> fila = priorityQueueOfGains(candidatos); - double MaxSpread = 0; + double maxSpread = 0; while (semente.size() < k) { // set all gains invalid @@ -158,28 +180,32 @@ public class PrevalentSeed implements SeedChooser<Actor> { MarginalGain max = fila.poll(); if (max.isValid() == true) { semente.add(max.getVertice()); - MaxSpread = MaxSpread + max.getGain(); + maxSpread = maxSpread + max.getGain(); + System.out.println(semente.size()+"\t"+maxSpread); break; } else { double sigma = cascata(max.getVertice(), semente); - double delta = sigma - MaxSpread; + double delta = sigma - maxSpread; max.setGain(delta); max.setValid(true); fila.add(max); + cont++; // Conta o numero de chamadas à sigma } } } - + System.out.println("chamadas: "+cont); return semente; } - public HashSet<Actor> escolherGreedy(int k) { + public HashSet<Actor> escolher2(int k) { HashSet<Actor> semente = new HashSet<Actor>(); HashSet<Actor> minSet = new HashSet<Actor>(); + cont=0; MinDominatingSet ds = new MinDominatingSet(); - minSet = ds.greedy(grafo); + minSet = ds.fastGreedy2(grafo); + System.out.println("|DS| = " + minSet.size()); if (minSet.size() < k) { System.out.println("Erro: o cojunto domintante é menor que K"); return null; @@ -201,6 +227,7 @@ public class PrevalentSeed implements SeedChooser<Actor> { if (max.isValid() == true) { semente.add(max.getVertice()); MaxSpread = MaxSpread + max.getGain(); + System.out.println("|S| = "+semente.size()+", |A| = "+MaxSpread); break; } else { double sigma = cascata(max.getVertice(), semente); @@ -208,30 +235,28 @@ public class PrevalentSeed implements SeedChooser<Actor> { max.setGain(delta); max.setValid(true); fila.add(max); + cont++; } } } - + System.out.println("chamadas: "+cont); return semente; } - public HashSet<Actor> escolherRandom(int k) { + public HashSet<Actor> escolherGreedy(int k) { HashSet<Actor> semente = new HashSet<Actor>(); - Set<Actor> vSet = grafo.vertexSet(); - HashSet<Actor> rSet = new HashSet<Actor>(); + HashSet<Actor> minSet = new HashSet<Actor>(); - Actor[] vertices = new Actor[vSet.size()]; - int i = 0; - for (Actor a : vSet) - vertices[i++] = a; + MinDominatingSet ds = new MinDominatingSet(); + minSet = ds.greedy(grafo); - Random rand = new Random(); - while (rSet.size() < vSet.size() / 3) { - rSet.add(vertices[rand.nextInt(vertices.length)]); + if (minSet.size() < k) { + System.out.println("Erro: o cojunto domintante é menor que K"); + return null; } // create priority queue of all nodes, with marginal gain delta +inf - PriorityQueue<MarginalGain> fila = priorityQueueOfGains(rSet); + PriorityQueue<MarginalGain> fila = priorityQueueOfGains(minSet); double MaxSpread = 0; @@ -269,8 +294,7 @@ public class PrevalentSeed implements SeedChooser<Actor> { * @return uma fila de prioridade com o ganho marginal de todos os vértices */ - private PriorityQueue<MarginalGain> priorityQueueOfGains( - HashSet<Actor> minSet) { + private PriorityQueue<MarginalGain> priorityQueueOfGains(HashSet<Actor> minSet) { PriorityQueue<MarginalGain> fila = new PriorityQueue<MarginalGain>(); @@ -289,15 +313,15 @@ public class PrevalentSeed implements SeedChooser<Actor> { } public static void main(String[] args) { - DirectedSocialNetwork g = new SocialNetworkGenerate().gerarGrafo(2000, - 2.5); + DirectedSocialNetwork g = new SocialNetworkGenerate().gerarGrafo(2000, 2.5); + System.out.println("|V(G)| = " + g.vertexSet().size()); long startTime = 0; startTime = System.nanoTime(); HashSet<Actor> seed = new PrevalentSeed(g).escolher2(15); System.out.println("Tempo: " + (System.nanoTime() - startTime) / 1000); HashSet<Actor> ativos = g.indepCascadeDiffusion(seed); - System.out.println("|V(G)| = " + g.vertexSet().size()); + // System.out.println("|S| = " + seed.size()); System.out.println("|A| = " + ativos.size()); g.deactivate(ativos); @@ -309,16 +333,16 @@ public class PrevalentSeed implements SeedChooser<Actor> { System.out.println("Tempo: " + (System.nanoTime() - startTime) / 1000); HashSet<Actor> ativos2 = g.indepCascadeDiffusion(seed2); -// System.out.println("\n|S| = " + seed2.size()); + // System.out.println("\n|S| = " + seed2.size()); System.out.println("|A| = " + ativos2.size()); g.deactivate(ativos2); -// HashSet<Actor> seed3 = new DominatingSeed(g).escolherRandom(15); -// -// HashSet<Actor> ativos3 = g.indepCascadeDiffusion(seed3); -//// System.out.println("\n|S| = " + seed3.size()); -// System.out.println("|A| = "+ativos3.size()); - + // HashSet<Actor> seed3 = new DominatingSeed(g).escolherRandom(15); + // + // HashSet<Actor> ativos3 = g.indepCascadeDiffusion(seed3); + //// System.out.println("\n|S| = " + seed3.size()); + // System.out.println("|A| = "+ativos3.size()); + } } diff --git a/src/algoritmos/RandomSeed.java b/src/algoritmos/RandomSeed.java index 48f28fc000458ab41e4c54a5e667ca69c59009df..eeb0ddefc0f317720e211144dfb2c95c4107a68b 100644 --- a/src/algoritmos/RandomSeed.java +++ b/src/algoritmos/RandomSeed.java @@ -13,9 +13,10 @@ import grafos.DirectedSocialNetwork; import interfaces.SeedChooser; public class RandomSeed implements SeedChooser<Actor>{ - public Graph<Actor, DefaultWeightedEdge> grafo; + public DirectedSocialNetwork grafo = null; + private double[] spreadData; // histograma da propagação esperada - public RandomSeed(Graph<Actor, DefaultWeightedEdge> grafo) { + public RandomSeed(DirectedSocialNetwork grafo) { this.grafo = grafo; } @@ -23,6 +24,7 @@ public class RandomSeed implements SeedChooser<Actor>{ public HashSet<Actor> escolher(int k) { HashSet<Actor> semente = new HashSet<>(); Set<Actor> vSet = grafo.vertexSet(); + spreadData = new double[k+1]; Actor[] vertices = new Actor[vSet.size()]; int i = 0; @@ -34,6 +36,7 @@ public class RandomSeed implements SeedChooser<Actor>{ while (semente.size() < k) { if (!semente.contains(sorteado)) { semente.add(sorteado); + spreadData[semente.size()] = grafo.espectedSpread(semente, true); } sorteado = vertices[rand.nextInt(vertices.length)]; } @@ -41,6 +44,10 @@ public class RandomSeed implements SeedChooser<Actor>{ return semente; } + public double[] getSpreadData(){ + return this.spreadData; + } + public static void main(String[] args) { DirectedSocialNetwork g = new SocialNetworkGenerate().gerarGrafo(50, 2); HashSet<Actor> seed = new RandomSeed(g).escolher(5); diff --git a/src/geradores/GeradorGrafoPowerLaw.java b/src/geradores/GeradorGrafoPowerLaw.java index c7668d1cd14a4b9a4f37454ecf12e0a207b141a8..1538a3c74c15e3ab59439172b6262c6c0bc814ef 100644 --- a/src/geradores/GeradorGrafoPowerLaw.java +++ b/src/geradores/GeradorGrafoPowerLaw.java @@ -43,7 +43,7 @@ public class GeradorGrafoPowerLaw { boolean direcionado) { //Devido a futura remoção dos vertices isolados if (direcionado) { - n = (int) (n + n * 0.7); + n = (int) (n + n * 0.3); }else n = (int) (n + n * 0.5); @@ -135,7 +135,7 @@ public class GeradorGrafoPowerLaw { int n, double beta, boolean direcionado) { if (direcionado) { - n = (int) (n + n * 0.65); + n = (int) (n + n * 0.13); }else n = (int) (n + n * 0.5); diff --git a/src/geradores/SocialNetworkGenerate.java b/src/geradores/SocialNetworkGenerate.java index 0f0d715dd6d78a078043e38253f5c94501e0baa8..fdfb29c7d14a61fe7216ed657020eb2649965870 100644 --- a/src/geradores/SocialNetworkGenerate.java +++ b/src/geradores/SocialNetworkGenerate.java @@ -31,7 +31,8 @@ public class SocialNetworkGenerate { boolean direcionado = true; boolean comPeso = true; - return gerarModeloLT(n, beta, direcionado, comPeso); +// return gerarModeloLT(n, beta, direcionado, comPeso); + return gerarGrafoInteiro(n, beta, direcionado, comPeso); } // Por padrão a rede resultante é direcionada e com pesos @@ -130,7 +131,7 @@ public class SocialNetworkGenerate { public static void main(String[] args) { // DirectedSocialNetwork g = new SocialNetworkGenerate().gerarGrafo(50000, 2.5); - DirectedSocialNetwork g = new SocialNetworkGenerate().gerarGrafoInteiro(30000, 2.5, true, true); + DirectedSocialNetwork g = new SocialNetworkGenerate().gerarGrafoInteiro(50000, 2.5, true, true); System.out.println("|V(G)|"+g.vertexSet().size()); System.out.println("|E(G)|"+g.edgeSet().size()); Histograma histograma = new Histograma(); diff --git a/src/grafos/DirectedSocialNetwork.java b/src/grafos/DirectedSocialNetwork.java index cc808956f1c6694e30e8818f0b54bdf73684e2c4..fb8691370f4a4f648183f515134366257cf08d90 100644 --- a/src/grafos/DirectedSocialNetwork.java +++ b/src/grafos/DirectedSocialNetwork.java @@ -186,7 +186,7 @@ public class DirectedSocialNetwork extends public double espectedSpread(HashSet<Actor> seed, boolean ic) { double media = 0; int soma = 0; - int repeticoes = 500; + int repeticoes = 5000; if (!ic) { HashSet<Actor> ativados = linearThresholdDiffusion(seed); diff --git a/src/plot/MeuPlot.java b/src/plot/MeuPlot.java index a45716e1269c25a294b858dc795cfcb66fd5fd42..9143bf25e7e79e5d0bdacee7b8e8918d9cb67d59 100644 --- a/src/plot/MeuPlot.java +++ b/src/plot/MeuPlot.java @@ -7,9 +7,9 @@ public class MeuPlot extends JGnuplot { // String terminal = "windows enhanced dashed title 'id=100 hello there' size 600,600"; String terminal = "windows enhanced dashed title 'id=100 hello there'"; String output = null; - //String beforeStyle="linewidth=4"; +// String beforeStyle="linewidth=4"; - public void plotPropagacao(double[] tamSemente, double[] y1, double[] y2, double[] y3, double[] y4, String outDir){ + public void plotPropagacao(double[] x, double[] y1, double[] y2, double[] y3, double[] y4, String outDir){ JGnuplot plot = new JGnuplot(); Plot plotPropagacao = new Plot("Propagação Esperada") { String xlabel = "'|S|'", ylabel = "'sigma(S)'"; @@ -17,39 +17,40 @@ public class MeuPlot extends JGnuplot { }; DataTableSet dts = plotPropagacao.addNewDataTableSet("Propagação Esperada"); - dts.addNewDataTable("DominatingSeed", tamSemente, y1); - dts.addNewDataTable("CELF", tamSemente, y2); - dts.addNewDataTable("HighDegree", tamSemente, y3); - dts.addNewDataTable("RandomSeed", tamSemente, y4); + dts.addNewDataTable("PrevalentSeed", x, y1); + dts.addNewDataTable("CELF", x, y2); + dts.addNewDataTable("HighDegree", x, y3); + dts.addNewDataTable("RandomSeed", x, y4); plotPropagacao.add(dts); plot.compile(plotPropagacao, plot.plot2d, "propagacao_esperada.plt"); - this.terminal = "epslatex color colortext dashed"; -// this.terminal = "eps color dashed"; - this.output = outDir+"propagacao.tex'"; +// this.terminal = "epslatex color colortext dashed"; +// this.output = outDir+"propagacao.tex'"; + this.terminal = "eps color dashed"; + this.output = outDir+"propagacao.eps'"; this.execute(plotPropagacao, this.plot2d); } - public void plotarPropagacaoEsperadaLT(double[] tamSemente, double[] y1, double[] y2, double[] y3, double[] y4){ + public void plotChamadas(double[] x, double[] y1, double[] y2, String outDir){ JGnuplot plot = new JGnuplot(); - Plot plotPropagacao = new Plot("Propagação Esperada") { - String xlabel = "'Tam. Semente'", ylabel = "'Propagacao Esperada'"; + Plot p = new Plot("Chamadas à sigma") { + String xlabel = "'|S|'", ylabel = "'n. chamadas'"; + String extra = "set key top left"; }; - DataTableSet dts = plotPropagacao.addNewDataTableSet("Propagacao"); - dts.addNewDataTable("HighDegree", tamSemente, y1); - dts.addNewDataTable("BestNeighbors", tamSemente, y2); - dts.addNewDataTable("RandomSeed", tamSemente, y3); -// dts.addNewDataTable("DominatingSeed", tamSemente, y4); - dts.addNewDataTable("CELF", tamSemente, y4); + DataTableSet dts = p.addNewDataTableSet("Chamadas à sigma"); + dts.addNewDataTable("PrevalentSeed", x, y1); + dts.addNewDataTable("CELF", x, y2); - plotPropagacao.add(dts); - plot.compile(plotPropagacao, plot.plot2d, "propagacao_esperada.plt"); + p.add(dts); + plot.compile(p, plot.plot2d, "chamadas.plt"); - this.terminal = "pngcairo enhanced dashed"; - this.output = "'plots/LT/propagacao_esperada.png'"; - this.execute(plotPropagacao, this.plot2d); +// this.terminal = "epslatex color colortext dashed"; + this.terminal = "eps color dashed"; +// this.output = outDir+"chamadas.tex'"; + this.output = outDir+"chamadas.eps'"; + this.execute(p, this.plot2d); } public void plotTempoExecucao(double[] tamSemente, double[] y1, double[] y2, double[] y3, double[] y4, String outDir){ @@ -60,7 +61,7 @@ public class MeuPlot extends JGnuplot { }; DataTableSet dts2 = plotTempo.addNewDataTableSet("Tempo de execução"); - dts2.addNewDataTable("DominatingSeed", tamSemente, y1); + dts2.addNewDataTable("PrevalentSeed", tamSemente, y1); dts2.addNewDataTable("CELF", tamSemente, y2); dts2.addNewDataTable("HighDegree", tamSemente, y3); dts2.addNewDataTable("RandomSeed", tamSemente, y4); @@ -72,25 +73,4 @@ public class MeuPlot extends JGnuplot { this.output = outDir+"tempo.tex'"; this.execute(plotTempo, this.plot2d); } - - public void plotarTempoExecucaoLT(double[] tamSemente, double[] y1, double[] y2, double[] y3, double[] y4){ - JGnuplot plot = new JGnuplot(); - Plot plotTempo = new Plot("Tempo de execução") { - String xlabel = "'Tam. Semente'", ylabel = "'Tempo de execução'"; - }; - - DataTableSet dts2 = plotTempo.addNewDataTableSet("TempoExecucao"); - dts2.addNewDataTable("HighDegree", tamSemente, y1); - dts2.addNewDataTable("BestNeighbors", tamSemente, y2); - dts2.addNewDataTable("RandomSeed", tamSemente, y3); -// dts2.addNewDataTable("DominatingSeed", tamSemente, y4); - dts2.addNewDataTable("CELF", tamSemente, y4); - plotTempo.add(dts2); - plot.compile(plotTempo, plot.plot2d, "tempo_execucao.plt"); - -// MeuPlot jg2 = new MeuPlot(); - this.terminal = "pngcairo enhanced dashed"; - this.output = "'plots/LT/tempo_execucao.png'"; - this.execute(plotTempo, this.plot2d); - } } diff --git a/src/readgraph/GraphReader.java b/src/readgraph/GraphReader.java index 418afb27e85ee5cb59e315c4ecc2ecf27a55d03b..5aa0f6642dd84512bfc5f7e876922ca75e02dd69 100644 --- a/src/readgraph/GraphReader.java +++ b/src/readgraph/GraphReader.java @@ -89,8 +89,8 @@ public class GraphReader { // uniform IC model // return (double)10/100; // return 0.025; -// return Math.random()/4; - return 0.001; + return Math.random()/4; +// return 0.0025; } public DirectedSocialNetwork readEpinions() { diff --git a/src/simulacao/Experimentacao.java b/src/simulacao/Experimentacao.java new file mode 100644 index 0000000000000000000000000000000000000000..7694956870959bbb3a3568cb1d9361b2b9810344 --- /dev/null +++ b/src/simulacao/Experimentacao.java @@ -0,0 +1,118 @@ +package simulacao; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; + +import algoritmos.HightDegree; +import algoritmos.LazyGreedy; +import algoritmos.PrevalentSeed; +import algoritmos.RandomSeed; +import geradores.SocialNetworkGenerate; +import grafos.DirectedSocialNetwork; +import plot.MeuPlot; + +public class Experimentacao { + + public static void main(String[] args) { + int k = 50; + grafosSinteticos(k); + } + + private static void grafosSinteticos(int k) { + double[] spreadData1 = new double[k+1], tempo1 = new double[5]; + double[] spreadData2 = new double[k+1], tempo2 = new double[5]; + double[] spreadData3 = new double[k+1], tempo3 = new double[5]; + double[] spreadData4 = new double[k+1], tempo4 = new double[5]; + double[] x = eixox(k + 1); // preenche o vetor do eixo x + double[] sigmaCalls = new double [k+1]; + double[] sigmaCalls2 = new double [k+1]; + + DirectedSocialNetwork g = new SocialNetworkGenerate().gerarGrafo(200, 2.5); + mostrarTamanho(g); + + LazyGreedy celf = new LazyGreedy(g); + System.out.println("#Celf"); + celf.escolher(k); + spreadData2 = celf.getSpreadData(); + sigmaCalls2 = celf.getCallData(); + for (int i = 0; i < spreadData2.length; i++) { + System.out.println(i + "\t" + spreadData2[i]); + } + + PrevalentSeed ps = new PrevalentSeed(g); + System.out.println("#PrevalentSeed"); + ps.escolher(k); + spreadData1 = ps.getSpreadData(); + sigmaCalls = ps.getCallData(); + for (int i = 0; i < spreadData1.length; i++) { + System.out.println(i + "\t" + spreadData1[i]); + } + + // Testa a heuristica de grau + HightDegree hd = new HightDegree(g); + System.out.println("#HightDegree"); + hd.escolher(k); + spreadData3 = hd.getSpreadData(); + for (int i = 0; i < spreadData3.length; i++) { + System.out.println(i + "\t" + spreadData3[i]); + } + + // Testa a heurística semente aleatória + RandomSeed rs = new RandomSeed(g); + System.out.println("#RandomSeed"); + rs.escolher(k); + spreadData4 = rs.getSpreadData(); + for (int i = 0; i < spreadData4.length; i++) { + System.out.println(i + "\t" + spreadData4[i]); + } + + MeuPlot meuplot = new MeuPlot(); + meuplot.plotPropagacao(x, spreadData1, spreadData2, spreadData3, spreadData4, + "'"); + meuplot.plotChamadas(x, sigmaCalls, sigmaCalls2, "'"); + + } + + /** + * Preenche o vetor para o eixo x do grafico a ser plotado no intervalo + * [0..50] + * + * @param l: tamanho do vetor + * @return o vetor preenchido + */ + private static double[] eixox(int l) { + double[] x = new double[l]; + for (int j = 0; j < x.length; j++) { + x[j] = j; + } + return x; + } + + /** + * Salva os resultados em arquivos de log + * + * @param g + * uma rede social direcionada + * @param k + * um inteiro + * @throws IOException + */ + private static void lazyGreedyToFile(DirectedSocialNetwork g, int k) throws IOException { + File sigma1 = new File("sigma1.txt"); + sigma1.createNewFile(); + FileWriter writer = new FileWriter(sigma1); + + LazyGreedy celf = new LazyGreedy(g); + writer.write("#Celf\n"); + celf.toFile(k, writer); + + writer.flush(); + writer.close(); + } + + private static void mostrarTamanho(DirectedSocialNetwork g) { + System.out.println("Grafo direcionado: |V| = " + g.vertexSet().size() + " |E| = " + g.edgeSet().size()); + } + +} diff --git a/src/simulacao/Simulacao.java b/src/simulacao/Simulacao.java index 88d2c73582146a8de7d59677a9d2e6a9c28e21b6..06acdc21bb53c0981b69dfa7fac1dd1e59136303 100644 --- a/src/simulacao/Simulacao.java +++ b/src/simulacao/Simulacao.java @@ -84,7 +84,7 @@ public class Simulacao { double[] sigma2 = new double[5], tempo2 = new double[5]; double[] sigma3 = new double[5], tempo3 = new double[5]; double[] sigma4 = new double[5], tempo4 = new double[5]; - int k, repeticoes = 10; + int k, repeticoes = 1; double[] ssigma1 = new double[5], stempo1 = new double[5]; double[] ssigma2 = new double[5], stempo2 = new double[5]; @@ -109,12 +109,12 @@ public class Simulacao { excutionTime += System.currentTimeMillis(); tempo1[i] = (excutionTime / 1000.0f); sigma1[i] = g.espectedSpread(seed1, true); - System.out.println("DominatingSeed: sigma = " + sigma1[i] + System.out.println("PrevalentSeed: sigma = " + sigma1[i] + ", tempo = " + tempo1[i] + " segundos"); ssigma1[i] += sigma1[i]; stempo1[i] += tempo1[i]; - /*excutionTime = System.currentTimeMillis() * -1; + excutionTime = System.currentTimeMillis() * -1; HashSet<Actor> seed2 = new LazyGreedy(g).escolher(k); excutionTime += System.currentTimeMillis(); tempo2[i] = (excutionTime / 1000.0f); @@ -122,17 +122,17 @@ public class Simulacao { System.out.println("LazyGreedy: sigma = " + sigma2[i] + ", tempo = " + tempo2[i] + " segundos"); ssigma2[i] += sigma2[i]; - stempo2[i] += tempo2[i];*/ - - excutionTime = System.currentTimeMillis() * -1; - HashSet<Actor> seed2 = new PrevalentSeed(g).escolher2(k); - excutionTime += System.currentTimeMillis(); - tempo2[i] = (excutionTime / 1000.0f); - sigma2[i] = g.espectedSpread(seed2, true); - System.out.println("preSelect: sigma = " + sigma2[i] - + ", tempo = " + tempo2[i] + " segundos"); - ssigma2[i] += sigma2[i]; stempo2[i] += tempo2[i]; + +// excutionTime = System.currentTimeMillis() * -1; +// HashSet<Actor> seed2 = new PrevalentSeed(g).escolher2(k); +// excutionTime += System.currentTimeMillis(); +// tempo2[i] = (excutionTime / 1000.0f); +// sigma2[i] = g.espectedSpread(seed2, true); +// System.out.println("preSelecaoOriginal: sigma = " + sigma2[i] +// + ", tempo = " + tempo2[i] + " segundos"); +// ssigma2[i] += sigma2[i]; +// stempo2[i] += tempo2[i]; excutionTime = System.currentTimeMillis() * -1; HashSet<Actor> seed3 = new HightDegree(g).escolher(k); @@ -229,7 +229,7 @@ public class Simulacao { private static void simularArtificial() { int i = 2; - while (i <= 4) { + while (i <= 64) { String outDir = "'plots/rand/"+i+"/"; simularRandomGraphIC(i, outDir); System.out.println("\t----------"); @@ -241,9 +241,9 @@ public class Simulacao { DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); Date data = new Date(); System.out.println("Data de início: " + dateFormat.format(data)); -// simularArtificial(); + simularArtificial(); // simularPhy(); - simularEpinions(); +// simularEpinions(); // simularHep(); // simularDblp(); }