diff --git a/jgnuplot.xml b/jgnuplot.xml index bbc4967319ee432994a3c1ebb02de96007493f21..fddd11f8c80f540341c9b85efc7fc0fce1dbe407 100644 --- a/jgnuplot.xml +++ b/jgnuplot.xml @@ -1,14 +1,19 @@ <?xml version="1.0" encoding="utf-8"?> <JGnuplot> <!-- the symbol ~ here at beginning of each code block is cirtical! It allows the code to be loaded without evaluation $$ and ## sections. --> -<style2d>~ +<style>~ $beforeStyle$ $beforeStyle2$ -ls(i)=value(sprintf("ls%i",i)); -lw=2;ps=1;pi=1;axiscolor='gray30';gridcolor='gray30'; +ls(i)=value(sprintf("ls%i",i));lt(i)=value(sprintf("lt%i",i));pt(i)=value(sprintf("pt%i",i));lc(i)=value(sprintf("lc%i",i)); +lw=1; +ps=1; +pi=4; +axiscolor='gray30'; +gridcolor='gray30'; +clw=1; lc1='dark-blue';lc2='dark-red';lc3='dark-green';lc4='orange';lc5='dark-pink';lc6='purple';lc7='olive';lc8='slategray';lc9='steelblue';lc10='black'; lt1=1;lt2=2;lt3=3;lt4=4;lt5=5;lt6=6;lt7=7;lt8=8;lt9=9;lt10=10; -pt1=5;pt2=7;pt3=9;pt4=11;pt5=13;pt6=15;pt7=17;pt8=19;pt9=21;pt10=23; +pt1=4;pt2=5;pt3=6;pt4=7;pt5=13;pt6=15;pt7=17;pt8=19;pt9=21;pt10=23; ls1=1;ls2=2;ls3=3;ls4=4;ls5=5;ls6=6;ls7=7;ls8=8;ls9=9;ls10=10;ls11=11;ls12=12;ls13=13;ls14=14;ls15=15;ls16=16;ls17=17;ls18=18;ls19=19;ls20=20; $beforeStyleVar$ $beforeStyleVar2$ @@ -17,66 +22,53 @@ ps1=ps;ps2=ps;ps3=ps;ps4=ps;ps5=ps;ps6=ps;ps7=ps;ps8=ps;ps9=ps;ps10=ps; pi1=pi;pi2=pi;pi3=pi;pi4=pi;pi5=pi;pi6=pi;pi7=pi;pi8=pi;pi9=pi;pi10=pi; $afterStyleVar$ $afterStyleVar2$ -# Define axis -# remove border on top and right and set color to gray -set style line 11 lc rgb axiscolor lt 1; -set border 3 back ls 11; +#Line styles: +set style line 1 lt 8 lw 2 pt 7 ps 0.4 pi -1; +set style line 2 lt 6 lw 2 pt 6 ps 0.4 pi -1; +set style line 3 lt 7 lw 2 pt 2 ps 0.4 pi -1; +set style line 4 lt 10 lw 2 pt 3 ps 0.4 pi -1; +set style line 101 lc rgb axiscolor lt 1 lw 1; +set style line 102 lc rgb gridcolor lt 0 lw 1; +#End common style +</style> +<style2d>~ +$style$ +#Define axis +set border 3 back ls 101;# remove border on top and right and set color to gray set tics nomirror; -# Define grid -set style line 12 lc rgb gridcolor lt 0 lw 1; -set grid back ls 12; +set grid back ls 102; set termoption dashed; -# Line styles: -set style line 1 lc rgb lc1 lt lt1 lw lw1 ps ps1 pt pt1 pi pi1; -set style line 2 lc rgb lc2 lt lt2 lw lw2 ps ps2 pt pt2 pi pi2; -set style line 3 lc rgb lc3 lt lt3 lw lw3 ps ps3 pt pt3 pi pi3; -set style line 4 lc rgb lc4 lt lt4 lw lw4 ps ps4 pt pt4 pi pi4; -set style line 5 lc rgb lc5 lt lt5 lw lw5 ps ps5 pt pt5 pi pi5; -set style line 6 lc rgb lc6 lt lt6 lw lw6 ps ps6 pt pt6 pi pi6; -set style line 7 lc rgb lc7 lt lt7 lw lw7 ps ps7 pt pt7 pi pi7; -set style line 8 lc rgb lc8 lt lt8 lw lw8 ps ps8 pt pt8 pi pi8; -set style line 9 lc rgb lc9 lt lt9 lw lw9 ps ps9 pt pt9 pi pi9; -set style line 10 lc rgb lc10 lt lt10 lw lw10 ps ps10 pt pt10 pi pi10; $afterStyle$ $afterStyle2$ </style2d> <style3d>~ -$beforeStyle$ -$beforeStyle2$ -axiscolor='gray30';gridcolor='gray30'; -$beforeStyleVar$ -$beforeStyleVar2$ -$afterStyleVar$ -$afterStyleVar2$ -set style line 11 lc rgb axiscolor lt 1; -set style line 12 lc rgb gridcolor lt 0 lw 1; -set border 1+2+4+8+16+32+64+256+512 back ls 11; +$style$ +set border 1+2+4+8+16+32+64+256+512 back ls 101; set xtics border out nomirror set ytics border out nomirror set ztics border out nomirror -set grid x y z back ls 12; +set grid x y z back ls 102; set xyplane 0; -#colorbox +#Colorbox #set format cb "%4.1f"; #set colorbox user size .03, .6; set cbtics scale 0; set palette negative rgb 21,22,23 #Reverse hot color palette; -set pm3d implicit at s; -unset surface; -unset key; $afterStyle$ $afterStyle2$ </style3d> <header>~ $beforeHeader$ $beforeHeader2$ -# Header start -set macros;# Enable the use of macros +#Header start +#set macros;#Enable the use of macros +set terminal wxt size 480,288;#corresponding to the default size of pdf terminal 5in,3in. +set terminal wxt title "$info$"; set terminal $terminal$; -set output $output$; -set xlabel $xlabel$; -set ylabel $ylabel$; -set zlabel $zlabel$; +set output "$output$"; +set xlabel "$xlabel$"; +set ylabel "$ylabel$"; +set zlabel "$zlabel$"; set xrange $xrange$; set yrange $yrange$; set zrange $zrange$; @@ -88,57 +80,84 @@ $extra2$ $extra3$ $afterHeader$ $afterHeader2$ -# Header end +#Header end </header> <plot2d>~ $style2d$ $header$ set title "$info(1)$"; -$beforePlot$plot for [i=1:$size(1)$] '-' title info2(1,i) w lp ls ls(i); +$bp$$bp2$plot for [i=1:$size(1)$] '-' title info2(1,i) w lp ls ls(i); $data(1,2d)$ </plot2d> -<plot2dx>~ +<plot2dBar>~ $style2d$ -$header$ +set style fill solid noborder; +$header$ set title "$info(1)$"; -$beforePlot$plot '-' $info(1,1)$ - '-' $info(1,2)$ - '-' $info(1,3)$ - '-' $info(1,4)$ - '-' $info(1,5)$ - '-' $info(1,6)$ - '-' $info(1,7)$ - '-' $info(1,8)$ - '-' $info(1,9)$ - '-' $info(1,10)$ -$data(1,2d)$ -</plot2dx> +$bp$$bp2$plot for [i=2:$nColumn(1,1)$] '$data2f(1,2d)$' using i:xtic(1) title columnhead w histograms ls ls(i-1); +</plot2dBar> <plot3d>~ $style3d$ $header$ set title "$info(1)$"; -$beforePlot$splot for [i=1:$size(1)$] "-" using 1:2:3 title info2(1,i); +$bp$$bp2$splot for [i=1:$size(1)$] "-" using 1:2:3 title info2(1,i) w l ls ls(i); $data(1,3d)$ </plot3d> +<plotDensity>~ +$style3d$ +set pm3d; +set pm3d interpolate 0,0;#set pm3d interpolate -1,-1;#To turn off interpolate +set pm3d map; +set size square; +#set contour; +#set cntrparam bspline; +unset clabel; +unset surface; +unset key; +unset grid; +set xtics border in mirror; +set ytics border in mirror; +set lmargin at screen 0.1; +set rmargin at screen 0.9; +set bmargin at screen 0.2; +set tmargin at screen 0.9; +set cblabel "$zlabel$"; +$header$ +set title "$info(1)$"; +$bp$$bp2$splot for [i=1:$size(1)$] "-" using 1:2:3 title info2(1,i) w l ls ls(i) lw clw; +$data(1,3d)$ +</plotDensity> +<plotImage>~ +$style3d$ +set xtics in mirror; +set ytics in mirror; +set cblabel "$zlabel$"; +$header$ +unset key; +set view map; +set title "$info(1)$"; +$bp$$bp2$plot '-' using 1:2:3 with image; +$data(1,3d)$ +</plotImage> <multiplot>~ $style2d$ $header$ set multiplot layout $size()$,1; set title "$info(1)$"; -$beforePlot$plot for [i=1:$size(1)$] '-' title info2(1,i) w lp ls ls(i); +$bp$$bp2$plot for [i=1:$size(1)$] '-' title info2(1,i) w lp ls ls(i); $data(1,2d)$ set title "$info(2)$"; -$beforePlot$plot for [i=1:$size(2)$] '-' title info2(2,i) w lp ls ls(i); +$bp$$bp2$plot for [i=1:$size(2)$] '-' title info2(2,i) w lp ls ls(i); $data(2,2d)$ set title "$info(3)$"; -$beforePlot$plot for [i=1:$size(3)$] '-' title info2(3,i) w lp ls ls(i); +$bp$$bp2$plot for [i=1:$size(3)$] '-' title info2(3,i) w lp ls ls(i); $data(3,2d)$ set title "$info(4)$"; -$beforePlot$plot for [i=1:$size(4)$] '-' title info2(4,i) w lp ls ls(i); +$bp$$bp2$plot for [i=1:$size(4)$] '-' title info2(4,i) w lp ls ls(i); $data(4,2d)$ set title "$info(5)$"; -$beforePlot$plot for [i=1:$size(5)$] '-' title info2(5,i) w lp ls ls(i); +$bp$$bp2$plot for [i=1:$size(5)$] '-' title info2(5,i) w lp ls ls(i); $data(5,2d)$ unset multiplot; </multiplot> -</JGnuplot> \ No newline at end of file +</JGnuplot> diff --git a/src/algoritmos/LazyGreedy.java b/src/algoritmos/LazyGreedy.java index 63cabe4ee6fc048cbd240ff57fe0aa36b010a2fc..0204b4e37af9ec38413078ad91c1eeb02851f51f 100644 --- a/src/algoritmos/LazyGreedy.java +++ b/src/algoritmos/LazyGreedy.java @@ -61,9 +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); + System.out.println(semente.size()+"\t"+MaxSpread); spreadData[semente.size()] = MaxSpread; - callData[semente.size()] = cont; +// callData[semente.size()] = cont; break; } else { double sigma = cascata(max.getVertice(), semente); diff --git a/src/algoritmos/PrevalentSeed.java b/src/algoritmos/PrevalentSeed.java index 64b827cc3f72e6708de7f508e33135cc2082ebb0..f368691966085802a0053f1ceacae874640ac8ae 100644 --- a/src/algoritmos/PrevalentSeed.java +++ b/src/algoritmos/PrevalentSeed.java @@ -123,8 +123,8 @@ public class PrevalentSeed implements SeedChooser<Actor> { if (max.isValid() == true) { semente.add(max.getVertice()); maxSpread = maxSpread + max.getGain(); -// System.out.println(semente.size()+"\t"+maxSpread); - callData[semente.size()] = cont; + System.out.println(semente.size()+"\t"+maxSpread); +// callData[semente.size()] = cont; spreadData[semente.size()] = maxSpread; break; } else { diff --git a/src/plot/JGnuplot.java b/src/plot/JGnuplot.java deleted file mode 100644 index d7f1edb82050fcc0c50e8c465ca882650e388f11..0000000000000000000000000000000000000000 --- a/src/plot/JGnuplot.java +++ /dev/null @@ -1,356 +0,0 @@ -package plot; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Date; -import java.util.List; -import java.util.Scanner; - -import org.leores.ecpt.TRuntimeException; -import org.leores.math.Randomer; -import org.leores.util.DataTable; -import org.leores.util.DataTableSet; -import org.leores.util.Logger; -import org.leores.util.SysUtil; -import org.leores.util.SysUtil.Command; -import org.leores.util.U; -import org.leores.util.able.Processable2; - -/** - /** - * We do not check the null of parameters intentionally to make the program stop - * as soon as error occurs. - * - * @author leoking - * - */ -public class JGnuplot extends Logger { - public String style2d, style3d, header, extra; - public String beforeStyle, beforeStyleVar, afterStyleVar, afterStyle, beforeHeader, afterHeader, beforePlot; - public String plot2d, plot2dx, plot3d, multiplot, plotx; - public String numOutPattern; - protected String sFTemp; - public String sFLoad; - protected static JGnuplot tJGnuplot; - - public static final int JG_InNewThread = 0x00000001; - public static final int JG_DeleteTempFile = 0x00000002; - public static final int JG_Pause = 0x00000004; - public static final int JG_Log = 0x00000008; - public static final int JG_Default = JG_DeleteTempFile | JG_Pause | JG_Log; - public static final int JG_All = ~0x00000000; - - public JGnuplot(boolean bCopyLoadFromFile) { - initialize(bCopyLoadFromFile); - } - - public JGnuplot() { - initialize(false); - } - - public static synchronized JGnuplot tJGnuplot() { - if (tJGnuplot == null) { - tJGnuplot = new JGnuplot(true); - } - return tJGnuplot; - } - - public static int delAllTempFiles() { - return U.deleleFiles("JGnuplotTemp.*plt"); - } - - public boolean initialize(boolean bCopyLoadFromFile) { - boolean rtn = false; - beforePlot = ""; - sFLoad = "jgnuplot.xml"; - Date startDate = new Date(); - Randomer rnd = new Randomer(); - String sFTempEnd = U.format(startDate, "_HHmm_") + Math.abs((rnd.nextInt() % 100000)); - sFTemp = "JGnuplotTemp" + sFTempEnd + ".plt"; - if (bCopyLoadFromFile) { - rtn = copyLoadFromFile(); - } else { - JGnuplot jg = tJGnuplot(); - header = jg.header; - extra = jg.extra; - style2d = jg.style2d; - style3d = jg.style3d; - plot2d = jg.plot2d; - plot3d = jg.plot3d; - multiplot = jg.multiplot; - numOutPattern = jg.numOutPattern; - rtn = true; - } - rtn = rtn && initialize(); - return rtn; - } - - public boolean initialize() { - return true; - } - - public boolean copyLoadFromFile() { - U.copyFileFromClassPath(JGnuplot.class, sFLoad, sFLoad, false);//this also works in a jar file - return U.loadFromXML(this, sFLoad, false); - } - - public void afterLoadObj(String sLoadMethod) { - U.processFields(this, U.modAll, new Processable2.TrimStringField()); - } - - protected boolean prep() { - return true; - } - - public boolean compile(Plot plot, String code, String sFile) { - boolean rtn = false; - - if (plot != null && code != null && sFile != null && prep()) { - String codeEvaled = code; - for (int i = 0; i < 3; i++) { - codeEvaled = U.eval(codeEvaled, this, U.EVAL_InvalidIgnore | U.EVAL_NullIgnore); - } - rtn = plot.compile(codeEvaled, sFile, numOutPattern); - } - - return rtn; - } - - public boolean compile(Plot plot, String code) { - return compile(plot, code, plot.info + ".plt"); - } - - protected String sCommand(String sFile, boolean bPause) { - String rtn = "gnuplot " + sFile; - if (bPause) { - rtn += " -e \"pause mouse key\""; - } - return rtn; - } - - /** - * flags:<br> - * <b>JG_InNewThread</b> <br> - * <b>JG_DeleteTempFile:</b> use ~JG_DeleteTempFile flag to keep the temp - * file. This is useful when doing multi-thread plotting. The temp files - * shold be deleted at the end all together (delAllTempFiles()) to guarantee - * all figures can be plotted before temp files are deleted.<br> - * <b>JG_Pause</b><br> - * <b>JG_Log</b><br> - * <b>JG_Default</b> = JG_DeleteTempFile | JG_Pause | JG_Log<br> - * <b>JG_All</b> - * - * @param plot - * @param code - * @param flags - * @return - */ - public Command execute(Plot plot, String code, int flags) { - Command rtn = null; - if (compile(plot, code, sFTemp)) { - String sCmd = sCommand(sFTemp, (flags & JG_Pause) > 0); - rtn = SysUtil.execCmd(sCmd, (flags & JG_Log) > 0, (flags & JG_InNewThread) > 0); - if ((flags & JG_DeleteTempFile) > 0) { - U.deleteFile(sFTemp); - } - } - return rtn; - } - - public Command execute(Plot plot, String code) { - return execute(plot, code, JG_Default); - } - - /** - * Execute the plot in a new thread so that the current thread can go on - * without waiting. The execution results can be obtained through calling - * waitForRtn() of the return Command obj (this will cause the current - * thread to wait). - * - * @param plot - * @param code - * @return - */ - public Command executeA(Plot plot, String code) { - return execute(plot, code, JG_All); - } - - public static class Plot { - public String info; - protected List<DataTableSet> lDataTableSet; - protected String numOutPattern; - protected String sFile; - - public Plot(String info) { - this.info = info; - lDataTableSet = new ArrayList<DataTableSet>(); - } - - public DataTableSet addNewDataTableSet(String info) { - DataTableSet dataTableSet = new DataTableSet(info); - add(dataTableSet); - return dataTableSet; - } - - public boolean add(DataTableSet dataTableSet) { - boolean rtn = false; - if (dataTableSet != null) { - rtn = lDataTableSet.add(dataTableSet); - } - - return rtn; - } - - public boolean addAll(Collection<? extends DataTableSet> dataTableSets) { - boolean rtn = false; - if (dataTableSets != null) { - rtn = lDataTableSet.addAll(dataTableSets); - } - return rtn; - } - - public DataTableSet getDataTableSet(Integer i) { - return lDataTableSet.get(i); - } - - public List<DataTableSet> getDataTableSets() { - return lDataTableSet; - } - - protected boolean prep() { - return true; - } - - public boolean compile(String code, String sFile, String numOutPattern) { - boolean rtn = false; - if (code != null && prep()) { - this.sFile = sFile; - this.numOutPattern = numOutPattern; - U.deleteFile(sFile); - Scanner scanner = new Scanner(code); - while (scanner.hasNextLine()) { - String line = scanner.nextLine(); - try { - //Those lines with invalid/null fields/methods/expressions will be cause a runtime exception here. - String lineEvaled = line; - for (int i = 0; i < 3; i++) { - lineEvaled = U.eval(lineEvaled, this, U.EVAL_InvalidException | U.EVAL_NullException); - } - if (lineEvaled.length() > 0) { - U.appendToFile(sFile, lineEvaled + "\n"); - } - rtn = true; - } catch (TRuntimeException exception) { - - } - } - scanner.close(); - } - return rtn; - } - - public int size() { - return lDataTableSet.size(); - } - - public Integer size(String dataTableSetNum) { - Integer rtn = null; - int iDataTableSet = Integer.parseInt(dataTableSetNum) - 1; - if (iDataTableSet >= 0 && iDataTableSet < lDataTableSet.size()) { - rtn = lDataTableSet.get(iDataTableSet).size(); - } - return rtn; - } - - public String info(String dataTableSetNum) { - String rtn = null; - int iDataTableSet = Integer.parseInt(dataTableSetNum) - 1; - if (iDataTableSet >= 0 && iDataTableSet < lDataTableSet.size()) { - DataTableSet dts = lDataTableSet.get(iDataTableSet); - rtn = dts.info; - } - return rtn; - } - - public String info(String dataTableSetNum, String dataTableNum) { - String rtn = null; - int iDataTableSet = Integer.parseInt(dataTableSetNum) - 1; - if (iDataTableSet >= 0 && iDataTableSet < lDataTableSet.size()) { - DataTableSet dts = lDataTableSet.get(iDataTableSet); - int iDataTable = Integer.parseInt(dataTableNum) - 1; - if (iDataTable >= 0 && iDataTable < dts.size()) { - rtn = dts.get(iDataTable).info; - } - } - return rtn; - } - - public String infos() { - StringBuffer sBuffer = new StringBuffer(); - for (int i = 0, mi = lDataTableSet.size(); i < mi; i++) { - DataTableSet dts = lDataTableSet.get(i); - sBuffer.append("info" + (i + 1) + "=\"" + dts.info + "\"\n"); - for (int j = 0, mj = dts.size(); j < mj; j++) { - DataTable dt = dts.get(j); - sBuffer.append("info" + (i + 1) + "_" + (j + 1) + "=\"" + dt.info + "\"\n"); - } - } - String sInfos = sBuffer.toString(); - sInfos = sInfos.substring(0, sInfos.length() - 1);//remove the last line break; - return sInfos; - } - - protected void saveToFile(String sFile, DataTable dt, String type) { - String sLastColumn1 = null; - for (int i = 0, mj = dt.nRow(); i < mj; i++) { - Object[] row = dt.getRow(i); - String[] sRow = U.toStrArray(Arrays.asList(row), numOutPattern); - if ("3d".equals(type)) { - if (sLastColumn1 == null) { - sLastColumn1 = sRow[0]; - } - if (!sLastColumn1.equals(sRow[0])) { - U.appendToFile(sFile, "\n"); - sLastColumn1 = sRow[0]; - } - } - String line = U.wrap(sRow, "", "", " "); - U.appendToFile(sFile, line + "\n"); - - } - U.appendToFile(sFile, "e\n"); - } - - public String data(String dataTableSetNum, String dataTableNum, String type) { - String rtn = null; - int iDataTableSet = Integer.parseInt(dataTableSetNum) - 1; - if (iDataTableSet >= 0 && iDataTableSet < lDataTableSet.size() && type != null) { - DataTableSet dts = lDataTableSet.get(iDataTableSet); - if (dataTableNum == null) { - for (int i = 0, mi = dts.size(); i < mi; i++) { - DataTable dt = dts.get(i); - saveToFile(sFile, dt, type); - } - rtn = ""; - } else { - int iDataTable = Integer.parseInt(dataTableNum) - 1; - if (iDataTable >= 0 && iDataTable < dts.size()) { - DataTable dt = dts.get(iDataTable); - saveToFile(sFile, dt, type); - rtn = ""; - } - } - } - return rtn; - } - - public String data(String dataTableSetNum, String type) { - return data(dataTableSetNum, null, type); - } - - public String data(String dataTableSetNum) { - return data(dataTableSetNum, null, "2d"); - } - } -} diff --git a/src/plot/MeuPlot.java b/src/plot/MeuPlot.java index 9143bf25e7e79e5d0bdacee7b8e8918d9cb67d59..83e2bd358f0772dcbee5bb37a1fe07e070246b53 100644 --- a/src/plot/MeuPlot.java +++ b/src/plot/MeuPlot.java @@ -1,63 +1,136 @@ package plot; -import org.leores.util.DataTableSet; +import org.leores.util.data.DataTable; +import org.leores.util.data.DataTableSet; public class MeuPlot extends JGnuplot { //windows terminal is only available to windows. You might get error output from gnuplot if you are not using windows. // 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 terminal = "windows enhanced"; +// String output = null; // String beforeStyle="linewidth=4"; - public void plotPropagacao(double[] x, 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){ +// double[] x2 = suavisalinhas(x); +// double[] z1 = suavisalinhas(y1); +// double[] z2 = suavisalinhas(y2); +// double[] z3 = suavisalinhas(y3); +// double[] z4 = suavisalinhas(y4); +// +// JGnuplot plot = new JGnuplot(); +// Plot plotPropagacao = new Plot("Propagação Esperada") { +// { +// xlabel = "'|S|'"; +// ylabel = "'sigma(S)'"; +// extra = "set key top left"; +// } +// }; +// +// DataTableSet dts = plotPropagacao.addNewDataTableSet("Propagação Esperada"); +// dts.addNewDataTable("PrevalentSeed", x2, z1); +// dts.addNewDataTable("CELF", x2, z2); +// dts.addNewDataTable("HighDegree", x2, z3); +// dts.addNewDataTable("RandomSeed", x2, z4); +// +// plotPropagacao.add(dts); +// String saida = outDir+"propagacao_esperada.plt"; +// plot.compile(plotPropagacao, plot.plot2d, saida); +// +//// 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 plotPropagacao(double[] x, double[] y1, double[] y2, String outDir){ + double[] x2 = suavisalinhas(x); + double[] z1 = suavisalinhas(y1); + double[] z2 = suavisalinhas(y2); + JGnuplot plot = new JGnuplot(); Plot plotPropagacao = new Plot("Propagação Esperada") { - String xlabel = "'|S|'", ylabel = "'sigma(S)'"; - String extra = "set key top left"; + { + xlabel = "'|S|'"; + ylabel = "'sigma(S)'"; + extra = "set key top left"; + } }; DataTableSet dts = plotPropagacao.addNewDataTableSet("Propagação Esperada"); - dts.addNewDataTable("PrevalentSeed", x, y1); - dts.addNewDataTable("CELF", x, y2); - dts.addNewDataTable("HighDegree", x, y3); - dts.addNewDataTable("RandomSeed", x, y4); + dts.addNewDataTable("PrevalentSeed", x2, z1); + dts.addNewDataTable("CELF", x2, z2); plotPropagacao.add(dts); - plot.compile(plotPropagacao, plot.plot2d, "propagacao_esperada.plt"); + String saida = outDir+"propagacao_esperada.plt"; + plot.compile(plotPropagacao, plot.plot2d, saida); // this.terminal = "epslatex color colortext dashed"; // this.output = outDir+"propagacao.tex'"; this.terminal = "eps color dashed"; - this.output = outDir+"propagacao.eps'"; + this.output = outDir+"propagacao.eps"; this.execute(plotPropagacao, this.plot2d); } - public void plotChamadas(double[] x, double[] y1, double[] y2, String outDir){ - JGnuplot plot = new JGnuplot(); - Plot p = new Plot("Chamadas à sigma") { - String xlabel = "'|S|'", ylabel = "'n. chamadas'"; - String extra = "set key top left"; - }; - - DataTableSet dts = p.addNewDataTableSet("Chamadas à sigma"); - dts.addNewDataTable("PrevalentSeed", x, y1); - dts.addNewDataTable("CELF", x, y2); + /** + * Reduz um vetor x para 1/5 do tamanho original + * @param x[]: vetor do tipo double + * @return x2[]: vetor do tipo double + */ + private double[] suavisalinhas(double[] x) { + double[] x2 = new double[x.length/5 + 1]; + for (int i = 0; i < x2.length; i++) { + x2[i] = x[i*5]; + } + return x2; + } + + public void plotChamadas(double[] y1, double[] y2, String outDir){ + JGnuplot jg = new JGnuplot(); + Plot plot = new Plot("") { + { + xlabel = "x"; + ylabel = "y"; + yrange = "[0:15]"; + extra2 = "set key top left"; + } + }; + double[] x = { 1, 2, 3, 4, 5 }; + DataTableSet dts = plot.addNewDataTableSet("2D Bar"); + DataTable dt = dts.addNewDataTable("", x, y1, y2); + dt.insert(0, new String[] { "", "y1=2x", "y2=3x" }); + jg.execute(plot, jg.plot2dBar); - p.add(dts); - plot.compile(p, plot.plot2d, "chamadas.plt"); -// 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); +// JGnuplot plot = new JGnuplot(); +// Plot p = new Plot("Chamadas à sigma") { +// String xlabel = "'|S|'", ylabel = "'n. chamadas'"; +// String extra = "set key top left"; +// }; +// +// DataTableSet dts = p.addNewDataTableSet("Chamadas à sigma"); +// dts.addNewDataTable("PrevalentSeed", x, y1); +// dts.addNewDataTable("CELF", x, y2); +// +// p.add(dts); +// plot.compile(p, plot.plot2d, "chamadas.plt"); +// +//// 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){ JGnuplot plot = new JGnuplot(); - Plot plotTempo = new Plot("Tempo de execução") { - String xlabel = "'|S|'", ylabel = "'Tempo de execução'"; - String extra = "set key top left"; + + Plot plotTempo = new Plot("Tempo de execução"){ + { + xlabel = "'|S|'"; + ylabel = "'Tempo de execução'"; + extra = "set key top left"; + } }; DataTableSet dts2 = plotTempo.addNewDataTableSet("Tempo de execução"); diff --git a/src/simulacao/Experimentacao.java b/src/simulacao/Experimentacao.java index 7694956870959bbb3a3568cb1d9361b2b9810344..6e8b280de6b96b5f396b926c11cebb5d1457b520 100644 --- a/src/simulacao/Experimentacao.java +++ b/src/simulacao/Experimentacao.java @@ -4,6 +4,8 @@ import java.io.File; import java.io.FileWriter; import java.io.IOException; +import org.jgrapht.graph.DefaultWeightedEdge; + import algoritmos.HightDegree; import algoritmos.LazyGreedy; import algoritmos.PrevalentSeed; @@ -11,74 +13,234 @@ import algoritmos.RandomSeed; import geradores.SocialNetworkGenerate; import grafos.DirectedSocialNetwork; import plot.MeuPlot; +import readgraph.GraphReader; public class Experimentacao { public static void main(String[] args) { - int k = 50; - grafosSinteticos(k); + int k = 30; +// sinteticGraphSimulate(k); + //Simulações a serem feita com probabilidade 0.01 + simularHep(k); + simularPhy(k); + simularAmazon(k); + simularDblp(k); + // Simulações a serem feita com probabilidade 0.0025 + simularEnron(k); + simularEpinions(k); + + + } + + private static void simularHep(int k) { + System.out.println("Hep.txt"); + DirectedSocialNetwork g; + g = new GraphReader().readHep(); + System.out.println("|V(G)| = " + g.vertexSet().size()); + System.out.println("|E(G)| = " + g.edgeSet().size()); + + String out = "plots/hep/"; + + monteCarloSimulation(g, out, k); } + private static void simularPhy(int k) { + DirectedSocialNetwork g; + g = new GraphReader().readPhy(); + System.out.println("Phy.txt"); + System.out.println("|V(G)| = " + g.vertexSet().size()); + System.out.println("|E(G)| = " + g.edgeSet().size()); - 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]; + String out = "plots/phy/"; + + monteCarloSimulation(g, out, k); + } - DirectedSocialNetwork g = new SocialNetworkGenerate().gerarGrafo(200, 2.5); - mostrarTamanho(g); + private static void simularDblp(int k) { + System.out.println("dblp.txt"); + DirectedSocialNetwork g; + g = new GraphReader().readDblp(); + System.out.println("|V(G)| = " + g.vertexSet().size()); + System.out.println("|E(G)| = " + g.edgeSet().size()); + + String out = "plots/dblp/"; + + monteCarloSimulation(g, out, k); + } + + private static void simularEpinions(int k) { + System.out.println("Epinions.txt"); + DirectedSocialNetwork g; + g = new GraphReader().readEpinions(); + + System.out.println("|V(G)| = " + g.vertexSet().size()); + System.out.println("|E(G)| = " + g.edgeSet().size()); + + String out = "plots/epinions/"; + + monteCarloSimulation(g, out, k); + } + + private static void simularAmazon(int k) { + System.out.println("Amazon.txt"); + DirectedSocialNetwork g; + g = new GraphReader().amazon(); + + System.out.println("|V(G)| = " + g.vertexSet().size()); + System.out.println("|E(G)| = " + g.edgeSet().size()); + + String out = "plots/amazon/"; + + monteCarloSimulation(g, out, k); + } + + private static void simularEnron(int k) { + System.out.println("enron.txt"); + DirectedSocialNetwork g; + g = new GraphReader().enron(); + + System.out.println("|V(G)| = " + g.vertexSet().size()); + System.out.println("|E(G)| = " + g.edgeSet().size()); + + String out = "plots/enron/"; + + monteCarloSimulation(g, out, k); + } + + private static void monteCarloSimulation(DirectedSocialNetwork g, String out, int k) { + double[] spreadPS,spreadCelf, spreadHD, spreadRS; + double[] x; 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]); - } + spreadCelf = celf.getSpreadData(); 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]); + spreadPS = ps.getSpreadData(); + +// // Testa a heuristica de grau +// HightDegree hd = new HightDegree(g); +// System.out.println("#HightDegree"); +// hd.escolher(k); +// spreadHD = hd.getSpreadData(); +// +// // Testa a heurística semente aleatória +// RandomSeed rs = new RandomSeed(g); +// System.out.println("#RandomSeed"); +// rs.escolher(k); +// spreadRS = rs.getSpreadData(); + + x = eixox(k + 1); + MeuPlot meuplot = new MeuPlot(); + meuplot.plotPropagacao(x, spreadPS, spreadCelf, out); + } + + private static void sinteticGraphSimulate(int k) { + int n = 2; + + while (n <= 64) { + String out = "plots/rand/" + n + "/"; + sinteticGraph(n, out, k); + System.out.println("\t----------"); + n = n * 2; } + } + + /** + * Faz um conjunto de simulações para grafos aleatórios de tamanho n e gera + * grafos com os resultados da simulação + * + * @param n + * multiplo de 1000 para o tamanho dos grafos gerados + * @param out + * arquivo de saída para o grafico plotado + * @param k + * tamanho do conjunto semente + */ + private static void sinteticGraph(int n, String out, int k) { + double[] spreadPS = new double[k + 1]; + double[] spreadCelf = new double[k + 1]; + double[] spreadHD = new double[k + 1]; + double[] spreadRS = new double[k + 1]; + double[] mediaCelf = new double[k + 1], mediaPS = new double[k + 1], mediaHD = new double[k + 1], + mediaRS = new double[k + 1]; + double[] x; + int numSimulacoes = 10; + + for (int i = 0; i < numSimulacoes; i++) { + DirectedSocialNetwork g = new SocialNetworkGenerate().gerarGrafo(n * 1000, 2.5); + System.out.println("\nGrafo n. " + (i + 1)); + mostrarTamanho(g); + + LazyGreedy celf = new LazyGreedy(g); + System.out.println("#Celf"); + celf.escolher(k); + spreadCelf = somaVetores(spreadCelf, celf.getSpreadData()); + // spreadCelf = 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); + spreadPS = somaVetores(spreadPS, 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 heuristica de grau + HightDegree hd = new HightDegree(g); + System.out.println("#HightDegree"); + hd.escolher(k); + spreadHD = somaVetores(spreadHD, 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); + spreadRS = somaVetores(spreadRS, rs.getSpreadData()); + // for (int i = 0; i < spreadData4.length; i++) { + // System.out.println(i + "\t" + spreadData4[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]); + // TODO tirar a média das 20 simulações e plotar a media + for (int i = 0; i < spreadPS.length; i++) { + mediaPS[i] = spreadPS[i] / numSimulacoes; + mediaCelf[i] = spreadCelf[i] / numSimulacoes; + mediaHD[i] = spreadHD[i] / numSimulacoes; + mediaRS[i] = spreadRS[i] / numSimulacoes; } - + + x = eixox(k + 1); // preenche o vetor do eixo x MeuPlot meuplot = new MeuPlot(); - meuplot.plotPropagacao(x, spreadData1, spreadData2, spreadData3, spreadData4, - "'"); - meuplot.plotChamadas(x, sigmaCalls, sigmaCalls2, "'"); - +// meuplot.plotPropagacao(x, mediaPS, mediaCelf, mediaHD, mediaRS, out); + // meuplot.plotChamadas(sigmaCalls, sigmaCalls2, "'"); + + } + + private static double[] somaVetores(double[] a, double[] b) { + double[] soma = new double[a.length]; + for (int i = 0; i < soma.length; i++) { + soma[i] = a[i] + b[i]; + } + return soma; } /** * Preenche o vetor para o eixo x do grafico a ser plotado no intervalo * [0..50] * - * @param l: tamanho do vetor + * @param l: + * tamanho do vetor * @return o vetor preenchido */ private static double[] eixox(int l) { @@ -89,28 +251,6 @@ public class Experimentacao { 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()); }