📄 javaants.java~
字号:
} catch (Exception e) {
System.err.println("Exception in Main Routine");
System.err.println(e.getMessage());
System.exit(0);
}
}
/**************** Equivalent initialisation once some form of data is provided ********************************/
private static void init(boolean old, String filename) {
DistanceMatrix d = null;
keywords = new String[data.getnkeys()];
documents = new Document[data.getndocs()];
System.out.println("Keywords: "+ data.getnkeys());
System.out.println("Documents: "+ data.getndocs());
// store data
keywords = data.getKeys();
documents = data.getDocs();
configuration.setndocs(data.getndocs());
configuration.setnkeys(data.getnkeys());
// remove old maps
if (map != null) frame.remove(map);
// create the new topic map
map = new TopicMap(configuration, keywords, documents, antMode, d);
// update frame
frame.add(map, BorderLayout.CENTER);
frame.pack();
frame.setSize(configuration.getxsize()*5+10,configuration.getysize()*5+48);
frame.show();
frame.setVisible(true);
init = true;
if ((antMode == false) || (old == true)) ;
else new Thread(map).start();
}
/************** I/O Functions **********************************************************************/
/** Save a document collection to disk
* @param data the provided document collections
* @param filename the name of the file to write to
*/
public static void save(Data data, String filename) {
try {
ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(filename));
output.writeInt(data.getndocs());
output.writeInt(data.getnkeys());
output.writeObject(data.getDocs());
output.writeObject(data.getKeys());
output.flush();
output.close();
} catch (IOException e) {
System.out.println("Error occured when writing from file " + filename);
System.out.println(e.getMessage());
}
return;
}
/** Function to read a document collection from disk
* @param filename the name of the file to read from
* @return the loaded document collection
*/
public static Data read(String filename) {
Data data = null;
Document [] docs;
String [] keys;
int ndocs, nkeys;
try {
ObjectInputStream input = new ObjectInputStream(new FileInputStream(filename));
ndocs = input.readInt();
nkeys = input.readInt();
docs = (Document [])input.readObject();
keys = (String [])input.readObject();
input.close();
data = new Data(nkeys, ndocs, keys, docs);
} catch (Exception e) {
System.out.println("Error occured when reading from file " + filename);
System.out.println(e.getMessage());
}
return data;
}
/*********** hardcoded generation of artificial data *************************************/
/** Generate the hardcoded data for the test distributions
* @param dist switch between the three possible distributions (0, 1 or 2)
* @return the generated test data
*/
public static Data generate(int dist) {
int ndocs = 400;
int nkeys = 2;
String [] keys = new String[2];
keys[0] = new String("ycoord");
keys[1] = new String("xcoord");
Document[] docs = null;
// Hard-coded test distribution (Uniform Distribution)
if (dist == 1) {
ndocs = 450;
docs = new Document[ndocs + nkeys];
Random generator = new Random();
Position center[] = new Position[9];
center[0] = new Position(0, 0);
center[1] = new Position(0, 8);
center[2] = new Position(8, 0);
center[3] = new Position(8, 8);
center[4] = new Position(0, 16);
center[5] = new Position(16, 0);
center[6] = new Position(16, 8);
center[7] = new Position(8, 16);
center[8] = new Position(16, 16);
Position var[] = new Position [10];
var[0] = new Position(2, 2);
var[1] = new Position(2, 2);
var[2] = new Position(2, 2);
var[3] = new Position(2, 2);
var[4] = new Position(2, 2);
var[5] = new Position(2, 2);
var[6] = new Position(2, 2);
var[7] = new Position(2, 2);
var[8] = new Position(2, 2);
double data[] = new double[2];
Position pos;
// create individual vectors distributed randomly around the given mean
for (int j=0; j< 50; j++) {
for (int i = 0; i< 9; i++) {
data[0] = Math.random() * 4*var[i].getY() + center[i].getY();
data[1] = Math.random() * 4*var[i].getX() + center[i].getX();
pos = new Position((int)Math.round(data[0]), (int)Math.round(data[1]));
docs[j*9+i] = new Document(data, 2, pos, i+1);
}
}
}
// Hard-coded test distribution (Normal Distribution)
else if (dist == 0) {
ndocs = 800;
nkeys = 2;
docs = new Document[ndocs + nkeys];
Random generator = new Random();
Position center[] = new Position[4];
center[0] = new Position(0, 0);
center[1] = new Position(0, 8);
center[2] = new Position(8, 0);
center[3] = new Position(8, 8);
Position var[] = new Position [4];
var[0] = new Position(2, 2);
var[1] = new Position(2, 2);
var[2] = new Position(2, 2);
var[3] = new Position(2, 2);
double data[] = new double[2];
Position pos;
for (int i = 0; i< 4; i++) {
for (int j=0; j< 200 ; j++) {
data[0] = generator.nextGaussian() * var[i].getY() + center[i].getY();
data[1] = generator.nextGaussian() * var[i].getX() + center[i].getX();
pos = new Position((int)Math.round(data[0]), (int)Math.round(data[1]));
docs[i*200+j] = new Document(data, 2, pos, i+1);
}
}
}
else if (dist == 2) {
ndocs = 90;
nkeys = 20;
docs = new Document[ndocs+nkeys];
keys = new String[nkeys];
keys[0] = "Computer Graphics";
keys[1] = "Splines";
keys[2] = "Mesh";
keys[3] = "Visualization";
keys[4] = "Topic Maps";
keys[5] = "Fisheye Views";
keys[6] = "Graphs";
keys[7] = "Artificial Life";
keys[8] = "Game of Life";
keys[9] = "Turing";
keys[10] = "Chaos";
keys[11] = "Self-organisation";
keys[12] = "Artificial Intelligence";
keys[13] = "Bayesian Reasoning";
keys[14] = "Symbolic";
keys[15] = "Expert System";
keys[16] = "Neural Network";
keys[17] = "Knowledge";
keys[18] = "Representation";
keys[19] = "Robot";
double data[] = new double[nkeys];
for (int j=0; j<ndocs; j++) {
int color = 1;
if (j < 30) {
for (int k=0; k<7; k++) {
data[k] = (int)Math.floor(3.0+Math.random()*3.0);
}
for (int k=7; k<20; k++) {
data[k] = (int)Math.floor(Math.random()*3.0);
}
color = 2;
}
else if (j < 60) {
for (int k=0; k<7; k++) {
data[k] = (int)Math.floor(Math.random()*3.0);
}
for (int k=7; k<12; k++) {
data[k] = (int)Math.floor(3.0+Math.random()*10.0);
}
for (int k=12; k<20; k++) {
data[k] = (int)Math.floor(Math.random()*3.0);
}
color = 3;
}
else {
for (int k=0; k<12; k++) {
data[k] = (int)Math.floor(Math.random()*3.0);
}
for (int k=12; k<20; k++) {
data[k] = (int)Math.floor(30.0+Math.random()*5.0);
}
color = 4;
}
docs[j] = new Document(color);
docs[j].setVector(nkeys, data);
}
}
return new Data(nkeys, ndocs, keys, docs);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -