commit
05655d98df
@ -1,51 +0,0 @@
|
||||
package net.yacy.cider;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.FilterOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.Writer;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
public class AugmentedHtmlStream extends FilterOutputStream {
|
||||
private Writer out;
|
||||
private ByteArrayOutputStream buffer;
|
||||
private Charset charset;
|
||||
|
||||
public AugmentedHtmlStream(OutputStream out, Charset charset) {
|
||||
super(out);
|
||||
this.out = new BufferedWriter(new OutputStreamWriter(out, charset));
|
||||
this.buffer = new ByteArrayOutputStream();
|
||||
this.charset = charset;
|
||||
}
|
||||
|
||||
public void write(int b) throws IOException {
|
||||
this.buffer.write(b);
|
||||
}
|
||||
|
||||
public void write(byte[] b, int off, int len) throws IOException {
|
||||
this.buffer.write(b, off, len);
|
||||
}
|
||||
|
||||
public void close() throws IOException {
|
||||
StringBuffer b = new StringBuffer(this.buffer.toString(charset.name()));
|
||||
b = process(b);
|
||||
out.write(b.toString());
|
||||
System.out.println(b);
|
||||
out.close();
|
||||
}
|
||||
|
||||
public StringBuffer process(StringBuffer data) {
|
||||
System.out.println("got something!");
|
||||
data.append("It works!");
|
||||
return data;
|
||||
}
|
||||
|
||||
public static boolean supportsMime(String mime) {
|
||||
System.out.println("mime" +mime);
|
||||
return mime.split(";")[0].equals("text/html");
|
||||
}
|
||||
|
||||
}
|
@ -1,332 +0,0 @@
|
||||
/**
|
||||
* ConnectFour.java
|
||||
* Copyright 2009 by Michael Peter Christen, Frankfurt a. M., Germany
|
||||
* First published 03.12.2009 at http://yacy.net
|
||||
*
|
||||
* $LastChangedDate$
|
||||
* $LastChangedRevision$
|
||||
* $LastChangedBy$
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program in the file lgpl21.txt
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.yacy.cora.ai.example;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import net.yacy.cora.ai.greedy.AbstractFinding;
|
||||
import net.yacy.cora.ai.greedy.AbstractModel;
|
||||
import net.yacy.cora.ai.greedy.Battle;
|
||||
import net.yacy.cora.ai.greedy.ContextFactory;
|
||||
import net.yacy.cora.ai.greedy.Finding;
|
||||
import net.yacy.cora.ai.greedy.Goal;
|
||||
import net.yacy.cora.ai.greedy.Model;
|
||||
import net.yacy.cora.ai.greedy.Role;
|
||||
|
||||
public class ConnectFour {
|
||||
|
||||
static final int width = 7;
|
||||
static final int height = 6;
|
||||
|
||||
public static enum Coin implements Role {
|
||||
red('*'),
|
||||
blue('#');
|
||||
private final String c;
|
||||
Coin(char c) {this.c = String.valueOf(c);}
|
||||
@Override
|
||||
public Coin nextRole() {
|
||||
return (this == red) ? blue : red;
|
||||
}
|
||||
@Override
|
||||
public String toString() {return this.c;}
|
||||
public final static Coin[] allCoins = {red, blue};
|
||||
}
|
||||
|
||||
public static class Move extends AbstractFinding<Coin> implements Finding<Coin> {
|
||||
protected final int column;
|
||||
public Move(Coin role, int column) {
|
||||
super(role, (column > (width / 2)) ? (width - column - 1) : column);
|
||||
this.column = column;
|
||||
}
|
||||
@Override
|
||||
public Object clone() {
|
||||
return new Move(this.getRole(), this.column);
|
||||
}
|
||||
public int getColumn() {
|
||||
return this.column;
|
||||
}
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
if (!(other instanceof Move)) return false;
|
||||
Move m = (Move) other;
|
||||
return this.column == m.column;
|
||||
}
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return this.column;
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return super.getRole().toString() + ":" + Integer.toString(this.column);
|
||||
}
|
||||
}
|
||||
|
||||
public static class Board extends AbstractModel<Coin, Move> implements Model<Coin, Move>, Cloneable {
|
||||
|
||||
Coin[] b; // 2 dimensions folded: array starts in the bottom left and first position in second row is at index position <width>
|
||||
|
||||
/**
|
||||
* create a board with start configuration: empty board
|
||||
* @param nextRole
|
||||
*/
|
||||
public Board(Coin startPlayer) {
|
||||
super(startPlayer);
|
||||
this.b = new Coin[height * width];
|
||||
for (int i = 0; i < this.b.length; i++) this.b[i] = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* clone a board and return a new board
|
||||
* @param board
|
||||
*/
|
||||
public Board(Board board) {
|
||||
super(board.currentRole());
|
||||
this.b = new Coin[board.b.length];
|
||||
System.arraycopy(board.b, 0, this.b, 0, this.b.length);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object clone() {
|
||||
return new Board(this);
|
||||
}
|
||||
|
||||
public boolean columnFull(int column) {
|
||||
return this.b[(height - 1) * width + column] != null;
|
||||
}
|
||||
|
||||
public Coin getCell(int column, int row) {
|
||||
return this.b[row * width + column];
|
||||
}
|
||||
|
||||
@Override
|
||||
public void applyFinding(Move nextStep) {
|
||||
int column = nextStep.getColumn();
|
||||
int row = 0;
|
||||
while (row < height && this.b[row * width + column] != null) row++;
|
||||
if (row == height) throw new RuntimeException("column " + column + " is full");
|
||||
this.b[row * width + column] = nextStep.getRole();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int c = 0;
|
||||
Coin x;
|
||||
for (int i = 0; i < this.b.length; i++) {
|
||||
x = this.b[i];
|
||||
if (x != null) c += (i + 1) * (x.ordinal() + 1);
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (!(o instanceof Board)) return false;
|
||||
Board om = (Board) o;
|
||||
Coin c0, c1;
|
||||
for (int i = 0; i < this.b.length; i++) {
|
||||
c0 = this.b[i];
|
||||
c1 = om.b[i];
|
||||
if (!(c0 == null ? c1 == null : c0.equals(c1))) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean count4(int x, int y, int xi, int yi, Coin c) {
|
||||
int steps = 4;
|
||||
Coin cell;
|
||||
while (steps-- > 0) {
|
||||
cell = this.b[y * width + x];
|
||||
if (cell == null || !cell.equals(c)) return false;
|
||||
x += xi;
|
||||
y += yi;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private int countSame(int x, int y, int xi, int yi, Coin c) {
|
||||
int rb = 0;
|
||||
int steps = 4;
|
||||
Coin cell;
|
||||
while (steps-- > 0) {
|
||||
cell = this.b[y * width + x];
|
||||
if (cell != null) {
|
||||
if (cell.equals(c)) rb++; else return rb;
|
||||
}
|
||||
x += xi;
|
||||
y += yi;
|
||||
}
|
||||
return rb;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Coin isTermination() {
|
||||
for (Coin coin: Coin.allCoins) if (isTermination(coin)) return coin;
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isTermination(Coin coin) {
|
||||
// waagerecht
|
||||
for (int x = 0; x < width - 3; x++)
|
||||
for (int y = 0; y < height; y++)
|
||||
if (count4(x, y, 1, 0, coin)) return true;
|
||||
// senkrecht
|
||||
for (int x = 0; x < width; x++)
|
||||
for (int y = 0; y < height - 3; y++)
|
||||
if (count4(x, y, 0, 1, coin)) return true;
|
||||
// slash-schraeg
|
||||
for (int x = 0; x < width - 3; x++)
|
||||
for (int y = 0; y < height - 3; y++)
|
||||
if (count4(x, y, 1, 1, coin)) return true;
|
||||
// backslash-schraeg
|
||||
for (int x = 0; x < width - 3; x++)
|
||||
for (int y = 3; y < height; y++)
|
||||
if (count4(x, y, 1, -1, coin)) return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getRanking(int findings, Coin coin) {
|
||||
return 2 * getRankingSingle(coin) - getRankingSingle(coin.nextRole()) - 3 * findings;
|
||||
}
|
||||
|
||||
private int getRankingSingle(Coin coin) {
|
||||
int r = 0;
|
||||
// waagerecht
|
||||
for (int x = 0; x < width - 3; x++)
|
||||
for (int y = 0; y < height; y++)
|
||||
r += countSame(x, y, 1, 0, coin);
|
||||
// senkrecht
|
||||
for (int x = 0; x < width; x++)
|
||||
for (int y = 0; y < height - 3; y++)
|
||||
r += countSame(x, y, 0, 1, coin);
|
||||
// slash-schraeg
|
||||
for (int x = 0; x < width - 3; x++)
|
||||
for (int y = 0; y < height - 3; y++)
|
||||
r += countSame(x, y, 1, 1, coin);
|
||||
// backslash-schraeg
|
||||
for (int x = 0; x < width - 3; x++)
|
||||
for (int y = 3; y < height; y++)
|
||||
r += countSame(x, y, 1, -1, coin);
|
||||
return r;
|
||||
}
|
||||
|
||||
private int getPriority(Move finding) {
|
||||
if (finding.column <= width / 2) return finding.column;
|
||||
return width - 1 - finding.column;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Move> explore() {
|
||||
ArrayList<Move> moves = new ArrayList<Move>();
|
||||
for (int i = 0; i < width; i++) {
|
||||
Move move = new Move(this.currentRole(), i);
|
||||
move.setPriority(getPriority(move));
|
||||
if (!columnFull(i)) moves.add(move);
|
||||
}
|
||||
return moves;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder s = new StringBuilder((width + 1) * height);
|
||||
Coin coin;
|
||||
for (int row = height - 1; row >= 0; row--) {
|
||||
s.append('\"');
|
||||
for (int column = 0; column < width; column++) {
|
||||
coin = this.b[row * width + column];
|
||||
s.append((coin == null) ? " " : coin.toString());
|
||||
}
|
||||
if (row == 0) s.append('\"'); else s.append("\"+\n");
|
||||
}
|
||||
return s.toString();
|
||||
}
|
||||
}
|
||||
|
||||
public static class Strategy implements Goal<Coin, Move, Board> {
|
||||
|
||||
public Strategy() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isFulfilled(Board model) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSnapshot(Board model) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean pruning(Board model) {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static void battle() {
|
||||
Map<Coin, ContextFactory<Coin, Move, Board>> strategies = new HashMap<Coin, ContextFactory<Coin, Move, Board>>();
|
||||
ContextFactory<Coin, Move, Board> redFactroy = new ContextFactory<Coin, Move, Board>(new Strategy(), 3000, false, false);
|
||||
ContextFactory<Coin, Move, Board> blueFactroy = new ContextFactory<Coin, Move, Board>(new Strategy(), 50, false, false);
|
||||
strategies.put(Coin.red, redFactroy);
|
||||
strategies.put(Coin.blue, blueFactroy);
|
||||
/*Battle<Coin, Move, Board> battle =*/ new Battle<Coin, Move, Board>(new Board(Coin.red), strategies, 2000);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
battle();
|
||||
/*
|
||||
int cores = Runtime.getRuntime().availableProcessors();
|
||||
Engine<Coin, Move, Board> engine = new Engine<Coin, Move, Board>(cores);
|
||||
Agent<Coin, Move, Board> agent;
|
||||
engine.start();
|
||||
long comptime = 60;
|
||||
long relaxtime = 20;
|
||||
|
||||
agent = new Agent<Coin, Move, Board>(new Board(Coin.red), new Strategy(comptime, false, false)); // red begins
|
||||
engine.inject(agent);
|
||||
agent.getTeorem().getGoal().awaitTermination(relaxtime);
|
||||
System.out.println("=========== terminated ==========");
|
||||
agent.getTeorem().printReport(100);
|
||||
*/
|
||||
/*
|
||||
agent = new Agent<Coin, Move, Board>(new Board(), Coin.red, new Strategy(comptime, false, true)); // red begins
|
||||
engine.inject(agent);
|
||||
agent.getGoal().awaitTermination(relaxtime);
|
||||
System.out.println("=========== terminated ==========");
|
||||
agent.printReport(10);
|
||||
*/
|
||||
|
||||
//engine.stop();
|
||||
}
|
||||
|
||||
}
|
@ -1,251 +0,0 @@
|
||||
/**
|
||||
* Hanoi.java
|
||||
* Copyright 2009 by Michael Peter Christen, Frankfurt a. M., Germany
|
||||
* First published 03.12.2009 at http://yacy.net
|
||||
*
|
||||
* $LastChangedDate$
|
||||
* $LastChangedRevision$
|
||||
* $LastChangedBy$
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program in the file lgpl21.txt
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.yacy.cora.ai.example;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import net.yacy.cora.ai.greedy.AbstractFinding;
|
||||
import net.yacy.cora.ai.greedy.AbstractModel;
|
||||
import net.yacy.cora.ai.greedy.Agent;
|
||||
import net.yacy.cora.ai.greedy.Challenge;
|
||||
import net.yacy.cora.ai.greedy.Context;
|
||||
import net.yacy.cora.ai.greedy.ContextFactory;
|
||||
import net.yacy.cora.ai.greedy.Engine;
|
||||
import net.yacy.cora.ai.greedy.Finding;
|
||||
import net.yacy.cora.ai.greedy.Goal;
|
||||
import net.yacy.cora.ai.greedy.Model;
|
||||
import net.yacy.cora.ai.greedy.Unirole;
|
||||
|
||||
public class Hanoi {
|
||||
|
||||
public static class Coin {
|
||||
public final int size;
|
||||
public Coin(int size) {
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
return (other != null && other instanceof Coin && this.size == ((Coin) other).size);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return this.size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return Integer.toString(size);
|
||||
}
|
||||
}
|
||||
|
||||
public static class Move extends AbstractFinding<Unirole> implements Finding<Unirole> {
|
||||
public final int from, to;
|
||||
public Move(int from, int to) {
|
||||
super(Unirole.unirole);
|
||||
this.from = from;
|
||||
this.to = to;
|
||||
}
|
||||
|
||||
public Move(int from, int to, int prio) {
|
||||
super(Unirole.unirole, prio);
|
||||
this.from = from;
|
||||
this.to = to;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object clone() {
|
||||
return new Move(this.from, this.to, this.getPriority());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
return (other != null
|
||||
&& other instanceof Move
|
||||
&& this.from == ((Move) other).from
|
||||
&& this.to == ((Move) other).to);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return 3 * this.from + 7 * this.to;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return from + " -> " + to;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Board extends AbstractModel<Unirole, Move> implements Model<Unirole, Move>, Cloneable {
|
||||
private final List<Coin>[] stacks;
|
||||
public int moves;
|
||||
@SuppressWarnings("unchecked")
|
||||
public Board(int height) {
|
||||
super(Unirole.unirole);
|
||||
stacks = new ArrayList[3];
|
||||
for (int i = 0; i < 3; i++) this.stacks[i] = new ArrayList<Coin>();
|
||||
while (height > 0) {
|
||||
this.stacks[0].add(new Coin(height--));
|
||||
}
|
||||
this.moves = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder s = new StringBuilder();
|
||||
s.append(stacks[0].toString());
|
||||
s.append(",");
|
||||
s.append(stacks[1].toString());
|
||||
s.append(",");
|
||||
s.append(stacks[2].toString());
|
||||
return s.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object clone() {
|
||||
Board b = new Board(0);
|
||||
for (int i = 0; i < 3; i++) {
|
||||
for (Coin s: this.stacks[i]) b.stacks[i].add(s);
|
||||
}
|
||||
b.moves = this.moves;
|
||||
return b;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
if (other == null || !(other instanceof Board)) return false;
|
||||
Board b = (Board) other;
|
||||
for (int i = 0; i < 3; i++) {
|
||||
if (this.stacks[i].size() != b.stacks[i].size()) return false;
|
||||
for (int j = 0; j < this.stacks[i].size(); j++) {
|
||||
if (!this.stacks[i].get(j).equals(b.stacks[i].get(j))) return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int c = 0;
|
||||
for (int i = 0; i < 3; i++) {
|
||||
for (Coin s: this.stacks[i]) c += s.hashCode();
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
public void applyFinding(Move finding) {
|
||||
this.stacks[finding.to].add(this.stacks[finding.from].remove(this.stacks[finding.from].size() - 1));
|
||||
this.moves++;
|
||||
}
|
||||
|
||||
private int getPriority(Move finding) {
|
||||
int p = 1000 - this.moves;
|
||||
if (finding.from == 0) p += 2;
|
||||
if (finding.from == 1) p--;
|
||||
if (finding.from == 2) p++;
|
||||
if (finding.to == 0) p--;
|
||||
if (finding.to == 1) p += 2;
|
||||
if (finding.to == 2) p++;
|
||||
return p;
|
||||
}
|
||||
|
||||
public List<Move> explore() {
|
||||
final List<Move> moves = new ArrayList<Move>();
|
||||
for (int from = 0; from < 3; from++) {
|
||||
toloop: for (int to = 0; to < 3; to++) {
|
||||
if (from == to) continue toloop;
|
||||
Coin fromCoin = (this.stacks[from].isEmpty()) ? null : this.stacks[from].get(this.stacks[from].size() - 1);
|
||||
if (fromCoin == null) continue toloop;
|
||||
Coin toCoin = (this.stacks[to].isEmpty()) ? null : this.stacks[to].get(this.stacks[to].size() - 1);
|
||||
if (toCoin != null && fromCoin.size >= toCoin.size) continue toloop;
|
||||
Move move = new Move(from, to);
|
||||
move.setPriority(getPriority(move));
|
||||
moves.add(new Move(from, to));
|
||||
}
|
||||
}
|
||||
return moves;
|
||||
}
|
||||
|
||||
public int getRanking(int findings, Unirole role) {
|
||||
return stacks[1].size() - stacks[0].size();
|
||||
}
|
||||
|
||||
public boolean isTermination(Unirole role) {
|
||||
return stacks[0].isEmpty() && stacks[2].isEmpty();
|
||||
}
|
||||
|
||||
public Unirole isTermination() {
|
||||
return (stacks[0].isEmpty() && stacks[2].isEmpty()) ? Unirole.unirole : null;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Strategy implements Goal<Unirole, Move, Board> {
|
||||
|
||||
// we check if there was ever a fulfilled status
|
||||
// in case of a problem-solving algorithm we want to stop
|
||||
// as soon as a fulfilled status is reached.
|
||||
// therefore we use that status for the pruning method to prune all
|
||||
// tasks that may follow to the fullfillment board
|
||||
|
||||
public Strategy() {
|
||||
}
|
||||
|
||||
public boolean isFulfilled(Board model) {
|
||||
return model.isTermination(Unirole.unirole);
|
||||
}
|
||||
|
||||
public boolean isSnapshot(Board model) {
|
||||
return false; // no snapshots
|
||||
}
|
||||
|
||||
public boolean pruning(Board model) {
|
||||
// if the have ever found a solution then prune all other findings
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Engine<Unirole, Move, Board> engine = new Engine<Unirole, Move, Board>(Runtime.getRuntime().availableProcessors() + 1);
|
||||
engine.start();
|
||||
ContextFactory<Unirole, Move, Board> cfactory = new ContextFactory<Unirole, Move, Board>(new Strategy(), Long.MAX_VALUE, false, false);
|
||||
Context<Unirole, Move, Board> context = cfactory.produceContext(new Board(3));
|
||||
Agent<Unirole, Move, Board> agent = new Agent<Unirole, Move, Board>(context);
|
||||
engine.inject(agent);
|
||||
agent.getContext().awaitTermination(1000000, false);
|
||||
Challenge<Unirole, Move, Board> result = agent.getContext().takeResult();
|
||||
agent.getContext().announceCompletion();
|
||||
engine.stop();
|
||||
Finding<Unirole>[] moves = result.getAgent().getFindings();
|
||||
for (int i = 0; i < moves.length; i++) {
|
||||
System.out.println(i + ": " + moves[i].toString());
|
||||
}
|
||||
System.out.println("terminated");
|
||||
}
|
||||
}
|
@ -1,217 +0,0 @@
|
||||
package net.yacy.cora.ai.example;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
import net.yacy.cora.ai.greedy.AbstractFinding;
|
||||
import net.yacy.cora.ai.greedy.AbstractModel;
|
||||
import net.yacy.cora.ai.greedy.Finding;
|
||||
import net.yacy.cora.ai.greedy.Model;
|
||||
import net.yacy.cora.ai.greedy.Role;
|
||||
|
||||
public class SchwarzerPeter {
|
||||
|
||||
public static enum Kartentyp {
|
||||
A, B, C, D, E, F, G, H, P;
|
||||
}
|
||||
|
||||
public static enum Kartenzaehler {
|
||||
p, q;
|
||||
}
|
||||
|
||||
public static class Karte {
|
||||
private final Kartentyp kartentyp;
|
||||
private final Kartenzaehler kartenzaehler;
|
||||
public Karte(Kartentyp kartentyp, Kartenzaehler kartenzaehler) {
|
||||
this.kartentyp = kartentyp; this.kartenzaehler = kartenzaehler;
|
||||
}
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
return obj != null
|
||||
&& obj instanceof Karte
|
||||
&& this.kartentyp == ((Karte) obj).kartentyp
|
||||
&& this.kartenzaehler == ((Karte) obj).kartenzaehler;
|
||||
}
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return this.kartentyp.hashCode() + 16 + this.kartenzaehler.hashCode();
|
||||
}
|
||||
public boolean istSchwarzerPeter() {
|
||||
return this.kartentyp == Kartentyp.P;
|
||||
}
|
||||
public static boolean istPaar(Karte k1, Karte k2) {
|
||||
return k1.kartentyp == k2.kartentyp;
|
||||
}
|
||||
}
|
||||
|
||||
public static final List<Karte> alleKarten;
|
||||
static {
|
||||
alleKarten = new ArrayList<Karte>(33);
|
||||
for (Kartentyp typ: Kartentyp.values()) {
|
||||
alleKarten.add(new Karte(typ, Kartenzaehler.p));
|
||||
alleKarten.add(new Karte(typ, Kartenzaehler.q));
|
||||
}
|
||||
alleKarten.add(new Karte(Kartentyp.P, Kartenzaehler.p));
|
||||
}
|
||||
|
||||
public static final List<Karte> neuerStapel(Random r) {
|
||||
List<Karte> stapel0 = new ArrayList<Karte>();
|
||||
for (Karte karte: alleKarten) stapel0.add(karte);
|
||||
List<Karte> stapel1 = new ArrayList<Karte>();
|
||||
while (!stapel0.isEmpty()) stapel1.add(stapel0.remove(r.nextInt(stapel0.size())));
|
||||
return stapel1;
|
||||
}
|
||||
|
||||
public static class Spieler implements Role {
|
||||
|
||||
private final int spielernummer;
|
||||
private final int spieleranzahl;
|
||||
|
||||
public Spieler(int spielernummer, int spieleranzahl) {
|
||||
this.spielernummer = spielernummer;
|
||||
this.spieleranzahl = spieleranzahl;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Spieler nextRole() {
|
||||
int n = (this.spielernummer == this.spieleranzahl - 1) ? 0 : this.spielernummer + 1;
|
||||
return new Spieler(n, this.spieleranzahl);
|
||||
}
|
||||
public Spieler linkerNachbar() {
|
||||
int n = (this.spielernummer == 0) ? this.spieleranzahl - 1 : this.spielernummer - 1;
|
||||
return new Spieler(n, this.spieleranzahl);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
return obj != null
|
||||
&& obj instanceof Spieler
|
||||
&& this.spielernummer == ((Spieler) obj).spielernummer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return this.spielernummer;
|
||||
}
|
||||
}
|
||||
|
||||
public static enum Strategy {
|
||||
nichtsortieren_linksziehen,
|
||||
nichtsortieren_zufallsziehen,
|
||||
sortieren_linksziehen,
|
||||
sortieren_zufallsziehen;
|
||||
}
|
||||
|
||||
public static class Hand extends ArrayList<Karte> {
|
||||
private static final long serialVersionUID = -5274023237476645059L;
|
||||
private final Strategy strategy;
|
||||
public Hand(Strategy strategy) {
|
||||
this.strategy = strategy;
|
||||
}
|
||||
public void annehmen(Random r, Karte karte) {
|
||||
if (this.strategy == Strategy.nichtsortieren_linksziehen || this.strategy == Strategy.nichtsortieren_zufallsziehen) {
|
||||
this.add(this.set(r.nextInt(this.size()), karte));
|
||||
} else {
|
||||
this.add(karte);
|
||||
}
|
||||
}
|
||||
public Karte abgeben(Random r) {
|
||||
if (this.strategy == Strategy.nichtsortieren_linksziehen || this.strategy == Strategy.sortieren_linksziehen) {
|
||||
return this.remove(0);
|
||||
}
|
||||
return this.remove(r.nextInt(this.size()));
|
||||
}
|
||||
public boolean paerchenAblegen() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Zug extends AbstractFinding<Spieler> implements Finding<Spieler> {
|
||||
|
||||
public Zug(Spieler spieler, int priority) {
|
||||
super(spieler, priority);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object clone() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class Spiel extends AbstractModel<Spieler, Zug> implements Model<Spieler, Zug>, Cloneable {
|
||||
|
||||
private final Hand[] haende;
|
||||
private final Random random;
|
||||
|
||||
public Spiel(Spieler spieler, Random r) {
|
||||
super(spieler);
|
||||
this.random = r;
|
||||
this.haende = new Hand[spieler.spieleranzahl];
|
||||
for (int i = 0; i < spieler.spieleranzahl; i++) this.haende[i] = new Hand(Strategy.nichtsortieren_linksziehen);
|
||||
List<Karte> geben = neuerStapel(r);
|
||||
while (!geben.isEmpty()) {
|
||||
this.haende[spieler.spielernummer].annehmen(r, geben.remove(0));
|
||||
spieler = spieler.nextRole();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Zug> explore() {
|
||||
return new ArrayList<Zug>(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void applyFinding(Zug finding) {
|
||||
this.haende[this.currentRole().spielernummer].annehmen(this.random, this.haende[this.currentRole().linkerNachbar().spielernummer].abgeben(this.random));
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getRanking(int findings, Spieler role) {
|
||||
// TODO Auto-generated method stub
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isTermination(Spieler role) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Spieler isTermination() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object clone() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
// TODO Auto-generated method stub
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,61 +0,0 @@
|
||||
/**
|
||||
* testorder.java
|
||||
* Copyright 2009 by Michael Peter Christen, Frankfurt a. M., Germany
|
||||
* First published 03.12.2009 at http://yacy.net
|
||||
*
|
||||
* $LastChangedDate$
|
||||
* $LastChangedRevision$
|
||||
* $LastChangedBy$
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program in the file lgpl21.txt
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.yacy.cora.ai.example;
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.PriorityBlockingQueue;
|
||||
|
||||
public class testorder implements Comparable<testorder> {
|
||||
|
||||
public int x;
|
||||
public testorder(int x) {
|
||||
this.x = x;
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return Integer.toString(this.x);
|
||||
}
|
||||
|
||||
public int compareTo(testorder o) {
|
||||
if (this.x > o.x) return 1;
|
||||
if (this.x < o.x) return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
PriorityBlockingQueue<testorder> q = new PriorityBlockingQueue<testorder>();
|
||||
Random r = new Random();
|
||||
for (int i = 0; i < 10; i++) {
|
||||
q.add(new testorder(r.nextInt(20)));
|
||||
}
|
||||
while (!q.isEmpty())
|
||||
try {
|
||||
System.out.println(q.take().toString());
|
||||
} catch (final InterruptedException e) {
|
||||
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,98 +0,0 @@
|
||||
/**
|
||||
* AbstractFinding
|
||||
* Copyright 2009 by Michael Peter Christen, Frankfurt a. M., Germany
|
||||
* First published 03.12.2009 at http://yacy.net
|
||||
*
|
||||
* $LastChangedDate$
|
||||
* $LastChangedRevision$
|
||||
* $LastChangedBy$
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program in the file lgpl21.txt
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.yacy.cora.ai.greedy;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
public abstract class AbstractFinding<SpecificRole extends Role> implements Finding<SpecificRole>, Comparator<Finding<SpecificRole>>, Comparable<Finding<SpecificRole>>, Cloneable {
|
||||
|
||||
private final SpecificRole role;
|
||||
private int priority;
|
||||
|
||||
/**
|
||||
* create a new finding for a given role
|
||||
* the priority can be fixed in the beginning
|
||||
* @param role
|
||||
* @param priority
|
||||
*/
|
||||
public AbstractFinding(SpecificRole role, int priority) {
|
||||
this.role = role;
|
||||
this.priority = priority;
|
||||
}
|
||||
|
||||
/**
|
||||
* create a new finding for a given role
|
||||
* the priority should be assigned afterward
|
||||
* @param role
|
||||
*/
|
||||
public AbstractFinding(SpecificRole role) {
|
||||
this.role = role;
|
||||
this.priority = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public abstract Object clone();
|
||||
|
||||
/**
|
||||
* get the finding priority
|
||||
* @return
|
||||
*/
|
||||
public int getPriority() {
|
||||
return this.priority;
|
||||
}
|
||||
|
||||
/**
|
||||
* set the current priority
|
||||
* This may only be used internally as part of the engine process to create a result queue
|
||||
*/
|
||||
public void setPriority(final int newPriority) {
|
||||
this.priority = newPriority;
|
||||
}
|
||||
|
||||
/**
|
||||
* get the finding role
|
||||
* @return
|
||||
*/
|
||||
public SpecificRole getRole() {
|
||||
return this.role;
|
||||
}
|
||||
|
||||
public int compare(final Finding<SpecificRole> f1, final Finding<SpecificRole> f2) {
|
||||
final int p1 = f1.getPriority();
|
||||
final int p2 = f2.getPriority();
|
||||
if (p1 < p2) return 1;
|
||||
if (p1 > p2) return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int compareTo(final Finding<SpecificRole> o) {
|
||||
return compare(this, o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public abstract boolean equals(Object other);
|
||||
@Override
|
||||
public abstract int hashCode();
|
||||
}
|
@ -1,60 +0,0 @@
|
||||
/**
|
||||
* AbstractModel.java
|
||||
* Copyright 2009 by Michael Peter Christen, Frankfurt a. M., Germany
|
||||
* First published 03.12.2009 at http://yacy.net
|
||||
*
|
||||
* $LastChangedDate$
|
||||
* $LastChangedRevision$
|
||||
* $LastChangedBy$
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program in the file lgpl21.txt
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.yacy.cora.ai.greedy;
|
||||
|
||||
public abstract class AbstractModel<SpecificRole extends Role, SpecificFinding extends Finding<SpecificRole>> implements
|
||||
Model<SpecificRole, SpecificFinding>, Cloneable {
|
||||
|
||||
private SpecificRole currentRole;
|
||||
|
||||
public AbstractModel(SpecificRole currentRole) {
|
||||
this.currentRole = currentRole;
|
||||
}
|
||||
|
||||
@Override
|
||||
public abstract Object clone();
|
||||
|
||||
/**
|
||||
* the model contains a status about roles that may act next
|
||||
* @return the next role
|
||||
*/
|
||||
public SpecificRole currentRole() {
|
||||
return this.currentRole;
|
||||
}
|
||||
|
||||
/**
|
||||
* switch to the next role. The current role is migrated to the
|
||||
* new role status, or the current rule is replaced with a new role
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public void nextRole() {
|
||||
this.currentRole = (SpecificRole) this.currentRole.nextRole();
|
||||
}
|
||||
|
||||
@Override
|
||||
public abstract boolean equals(Object other);
|
||||
@Override
|
||||
public abstract int hashCode();
|
||||
}
|
@ -1,267 +0,0 @@
|
||||
/**
|
||||
* Agent.java
|
||||
* Copyright 2009 by Michael Peter Christen, Frankfurt a. M., Germany
|
||||
* First published 03.12.2009 at http://yacy.net
|
||||
*
|
||||
* $LastChangedDate$
|
||||
* $LastChangedRevision$
|
||||
* $LastChangedBy$
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program in the file lgpl21.txt
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.yacy.cora.ai.greedy;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* the greedy agent
|
||||
* this is the main object that contains all elements of a greedy computation
|
||||
* To use an agent, one must define
|
||||
* - a Model (for the problem that shall be solved) and
|
||||
* - a Goal (specifies when an Agent reached a solution for the problem)
|
||||
* Then instances of the Model and the Goal must be handed over to the Agent,
|
||||
* and the Agent must be feeded into the Engine.
|
||||
* The Engine solves the problem and returns the solution as sequence of Findings
|
||||
* in this object instance that can be retrieved with takeBestResult()
|
||||
*
|
||||
* @param <SpecificRole>
|
||||
* @param <SpecificFinding>
|
||||
* @param <SpecificModel>
|
||||
*/
|
||||
public class Agent<
|
||||
SpecificRole extends Role,
|
||||
SpecificFinding extends Finding<SpecificRole>,
|
||||
SpecificModel extends Model<SpecificRole, SpecificFinding>
|
||||
> implements
|
||||
Comparator<Agent<SpecificRole, SpecificFinding, SpecificModel>>,
|
||||
Comparable<Agent<SpecificRole, SpecificFinding, SpecificModel>> {
|
||||
|
||||
private final Context<SpecificRole, SpecificFinding, SpecificModel> context;
|
||||
private final SpecificModel model;
|
||||
private final SpecificFinding finding;
|
||||
private boolean findingFail; // a flag set by child nodes that signal that a specific role has terminated the branch below the current node
|
||||
private final Agent<SpecificRole, SpecificFinding, SpecificModel> parentAgent; // the next parent node
|
||||
private final int pathlength;
|
||||
|
||||
/**
|
||||
* create a poison agent
|
||||
*/
|
||||
protected Agent() {
|
||||
context = null;
|
||||
model = null;
|
||||
finding = null;
|
||||
findingFail = false;
|
||||
parentAgent = null;
|
||||
pathlength = 0;
|
||||
}
|
||||
|
||||
public Agent(Context<SpecificRole, SpecificFinding, SpecificModel> context) {
|
||||
this.context = context;
|
||||
this.model = context.getInitialModel();
|
||||
this.finding = null;
|
||||
this.findingFail = false;
|
||||
this.parentAgent = null;
|
||||
this.pathlength = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a clone of the current agent with an loaded finding as attached resource.
|
||||
* This is used to branch into alternatives of the given agent configuration.
|
||||
* Some elements of the agent must be cloned, other must be extended and one must be referenced without cloning:
|
||||
* - challenge: is cloned with an attached finding. This is the actual branching into alternatives
|
||||
* - findings: is cloned and extended at the same time. This carries the history of the branch
|
||||
* - result: is just referenced because it is used to place a feed-back to the original goal request
|
||||
* - initialModel: just referenced
|
||||
* - goal: just referenced
|
||||
* @param finding
|
||||
* @return
|
||||
*/
|
||||
public Agent(Agent<SpecificRole, SpecificFinding, SpecificModel> parentAgent, SpecificModel newModel, SpecificFinding newFinding) {
|
||||
this.parentAgent = parentAgent;
|
||||
this.context = parentAgent.context;
|
||||
this.model = newModel;
|
||||
this.context.addModel(newModel);
|
||||
this.finding = newFinding;
|
||||
this.findingFail = false;
|
||||
this.pathlength = parentAgent.pathlength + 1;
|
||||
}
|
||||
|
||||
public void checkInstanceCount() {
|
||||
// in case that there are no agents left, store the current state
|
||||
// and fire a shutdown signal
|
||||
if (this.context.getInstanceCount() > 0) return;
|
||||
//addResult();
|
||||
//if (this.getContext().countResults() > 0) this.context.getGoal().announceFullfillment();
|
||||
}
|
||||
|
||||
public void incInstances() {
|
||||
this.context.incInstances();
|
||||
}
|
||||
|
||||
public int decInstances() {
|
||||
return this.context.decInstances();
|
||||
}
|
||||
|
||||
public Context<SpecificRole, SpecificFinding, SpecificModel> getContext() {
|
||||
return this.context;
|
||||
}
|
||||
|
||||
public SpecificModel getModel() {
|
||||
return this.model;
|
||||
}
|
||||
|
||||
public Finding<SpecificRole> getFinding() {
|
||||
return this.finding;
|
||||
}
|
||||
|
||||
public int getPathLength() {
|
||||
return this.pathlength;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void addResult() {
|
||||
// create a challenge that contains the ranking of the current model with the initial role
|
||||
// as priority setting attached to the initial move that the user must do to reach the current status
|
||||
// find the first move.
|
||||
if (this.finding == null) return;
|
||||
if (!this.context.getInitialModel().currentRole().equals(this.finding.getRole())) return;
|
||||
SpecificFinding finding = null;
|
||||
try {
|
||||
// because several branches may find the same finding at the root
|
||||
// they will attempt to assign different priorities as rankings from the
|
||||
// leaf of the search tree. Therefore the findings must be cloned.
|
||||
finding = (SpecificFinding) getResultFinding().clone();
|
||||
} catch (final CloneNotSupportedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
finding.setPriority(this.model.getRanking(this.pathlength, this.context.initialRole()));
|
||||
assert this.finding != null;
|
||||
assert this.finding.getRole() != null;
|
||||
//System.out.println("finding: " + finding);
|
||||
this.context.registerResult(this, finding);
|
||||
}
|
||||
|
||||
|
||||
public void setFindingFail() {
|
||||
assert this.finding != null;
|
||||
assert this.finding.getRole() != null;
|
||||
this.findingFail = true;
|
||||
}
|
||||
|
||||
public boolean getFindingFail() {
|
||||
return this.findingFail;
|
||||
}
|
||||
|
||||
public SpecificFinding isPrunedByTerminationInHistory() {
|
||||
Agent<SpecificRole, SpecificFinding, SpecificModel> a = this;
|
||||
while (a != null) {
|
||||
if (a.findingFail) return a.finding;
|
||||
// step up in the tree
|
||||
a = a.parentAgent;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean isPrunedByTerminationInHistory(SpecificRole role) {
|
||||
Agent<SpecificRole, SpecificFinding, SpecificModel> a = this;
|
||||
while (a != null) {
|
||||
assert a != null;
|
||||
//assert a.finding != null;
|
||||
//assert a.finding.getRole() != null;
|
||||
if (a.findingFail && a.finding.getRole().equals(role)) return true;
|
||||
// step up in the tree
|
||||
a = a.parentAgent;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public List<SpecificFinding> listPrunedByTerminationInHistory() {
|
||||
ArrayList<SpecificFinding> list = new ArrayList<SpecificFinding>(this.pathlength);
|
||||
Agent<SpecificRole, SpecificFinding, SpecificModel> a = this;
|
||||
while (a != null) {
|
||||
if (a.findingFail) list.add(a.finding);
|
||||
// step up in the tree
|
||||
a = a.parentAgent;
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return this.model.hashCode();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public boolean equals(Object om) {
|
||||
if (!(om instanceof Agent)) return false;
|
||||
Agent<SpecificRole, SpecificFinding, SpecificModel> a = (Agent<SpecificRole, SpecificFinding, SpecificModel>) om;
|
||||
return this.model.equals(a.model);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public SpecificFinding[] getFindings() {
|
||||
SpecificFinding[] findings = (SpecificFinding[]) new Finding[this.pathlength];
|
||||
int l = this.pathlength - 1;
|
||||
Agent<SpecificRole, SpecificFinding, SpecificModel> a = this;
|
||||
while (a != null && l >= 0) {
|
||||
findings[l--] = a.finding;
|
||||
a = a.parentAgent;
|
||||
}
|
||||
return findings;
|
||||
}
|
||||
|
||||
public SpecificFinding getResultFinding() {
|
||||
int l = this.pathlength - 1;
|
||||
Agent<SpecificRole, SpecificFinding, SpecificModel> a = this;
|
||||
while (a != null && l >= 0) {
|
||||
if (l-- == 0) return a.finding;
|
||||
a = a.parentAgent;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public int getRanking(SpecificRole role) {
|
||||
return this.model.getRanking(this.pathlength, role);
|
||||
}
|
||||
|
||||
public int compare(
|
||||
Agent<SpecificRole, SpecificFinding, SpecificModel> a1,
|
||||
Agent<SpecificRole, SpecificFinding, SpecificModel> a2) {
|
||||
|
||||
// order of poison agents: they are the largest
|
||||
if (a1.context == null) return 1;
|
||||
if (a2.context == null) return -1;
|
||||
|
||||
// by default order by ranking of the model
|
||||
SpecificRole role = a1.model.currentRole();
|
||||
if (!a2.model.currentRole().equals(role)) return 0;
|
||||
int r1 = a1.model.getRanking(a1.pathlength, role);
|
||||
int r2 = a2.model.getRanking(a2.pathlength, role);
|
||||
|
||||
// reverse ordering to get the largest elements at the head of sort queues
|
||||
if (r1 < r2) return 1;
|
||||
if (r1 > r2) return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int compareTo(Agent<SpecificRole, SpecificFinding, SpecificModel> o) {
|
||||
return compare(this, o);
|
||||
}
|
||||
|
||||
}
|
@ -1,65 +0,0 @@
|
||||
/**
|
||||
* Asset.java
|
||||
* Copyright 2009 by Michael Peter Christen, Frankfurt a. M., Germany
|
||||
* First published 03.12.2009 at http://yacy.net
|
||||
*
|
||||
* $LastChangedDate$
|
||||
* $LastChangedRevision$
|
||||
* $LastChangedBy$
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program in the file lgpl21.txt
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.yacy.cora.ai.greedy;
|
||||
|
||||
public class Asset<
|
||||
SpecificRole extends Role,
|
||||
SpecificFinding extends Finding<SpecificRole>,
|
||||
SpecificModel extends Model<SpecificRole, SpecificFinding>
|
||||
> {
|
||||
|
||||
private final SpecificModel model;
|
||||
private final SpecificFinding finding;
|
||||
|
||||
public Asset(final SpecificModel model, final SpecificFinding finding) {
|
||||
this.model = model;
|
||||
this.finding = finding;
|
||||
}
|
||||
|
||||
public SpecificModel getModel() {
|
||||
return this.model;
|
||||
}
|
||||
|
||||
public SpecificFinding getFinding() {
|
||||
return this.finding;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return (this.finding == null) ? this.model.hashCode() : this.model.hashCode() + this.finding.hashCode();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
if (!(other instanceof Asset)) return false;
|
||||
Asset<SpecificRole, SpecificFinding, SpecificModel> a = (Asset<SpecificRole, SpecificFinding, SpecificModel>) other;
|
||||
if (!this.model.equals(a.model)) return false;
|
||||
if (this.finding == null && a.finding == null) return true;
|
||||
if (!this.finding.equals(a.finding)) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
@ -1,85 +0,0 @@
|
||||
/**
|
||||
* Attempts.java
|
||||
* Copyright 2009 by Michael Peter Christen, Frankfurt a. M., Germany
|
||||
* First published 03.12.2009 at http://yacy.net
|
||||
*
|
||||
* $LastChangedDate$
|
||||
* $LastChangedRevision$
|
||||
* $LastChangedBy$
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program in the file lgpl21.txt
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.yacy.cora.ai.greedy;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
|
||||
/**
|
||||
* the attempts object is a stack of Challenges that are ordered by the
|
||||
* priority of the findings within the single challenges
|
||||
* @param <SpecificRole>
|
||||
*/
|
||||
public class Attempts<SpecificRole extends Role> {
|
||||
|
||||
Map<Long, BlockingQueue<Finding<SpecificRole>>> stack;
|
||||
|
||||
public Attempts() {
|
||||
this.stack = new TreeMap<Long, BlockingQueue<Finding<SpecificRole>>>();
|
||||
}
|
||||
|
||||
public int size() {
|
||||
int s = 0;
|
||||
for (BlockingQueue<Finding<SpecificRole>> q: stack.values()) s += q.size();
|
||||
return s;
|
||||
}
|
||||
/*
|
||||
public void push(final Finding element) {
|
||||
BlockingQueue<Finding> q = this.stack.get(element.getPriority());
|
||||
if (q == null) synchronized (this) {
|
||||
q = this.stack.get(weight);
|
||||
if (q == null) q = new LinkedBlockingQueue<E>();
|
||||
try {
|
||||
q.put(element);
|
||||
this.stack.put(weight, q);
|
||||
} catch (final InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public stackElement top() {
|
||||
}
|
||||
|
||||
public stackElement pop() {
|
||||
}
|
||||
|
||||
public boolean exists(final E element) {
|
||||
}
|
||||
|
||||
public boolean exists(final int hashcode) {
|
||||
}
|
||||
|
||||
public stackElement get(final int hashcode) {
|
||||
}
|
||||
|
||||
public stackElement remove(final int hashcode) {
|
||||
}
|
||||
|
||||
public boolean bottom(final long weight) {
|
||||
}
|
||||
*/
|
||||
}
|
@ -1,69 +0,0 @@
|
||||
/**
|
||||
* Battle.java
|
||||
* Copyright 2009 by Michael Peter Christen, Frankfurt a. M., Germany
|
||||
* First published 03.12.2009 at http://yacy.net
|
||||
*
|
||||
* $LastChangedDate$
|
||||
* $LastChangedRevision$
|
||||
* $LastChangedBy$
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program in the file lgpl21.txt
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.yacy.cora.ai.greedy;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
public class Battle<
|
||||
SpecificRole extends Role,
|
||||
SpecificFinding extends Finding<SpecificRole>,
|
||||
SpecificModel extends Model<SpecificRole, SpecificFinding>
|
||||
>{
|
||||
|
||||
|
||||
public Battle(
|
||||
SpecificModel startModel,
|
||||
Map<SpecificRole, ContextFactory<SpecificRole, SpecificFinding, SpecificModel>> contexts,
|
||||
long relaxtime) {
|
||||
int cores = Runtime.getRuntime().availableProcessors();
|
||||
Engine<SpecificRole, SpecificFinding, SpecificModel> engine = new Engine<SpecificRole, SpecificFinding, SpecificModel>(cores);
|
||||
Agent<SpecificRole, SpecificFinding, SpecificModel> agent;
|
||||
engine.start();
|
||||
SpecificModel currentModel = startModel;
|
||||
ContextFactory<SpecificRole, SpecificFinding, SpecificModel> cfactroy;
|
||||
while (true) {
|
||||
cfactroy = contexts.get(currentModel.currentRole());
|
||||
agent = new Agent<SpecificRole, SpecificFinding, SpecificModel>(cfactroy.produceContext(currentModel));
|
||||
engine.inject(agent);
|
||||
agent.getContext().awaitTermination(relaxtime, false);
|
||||
if (agent.getContext().hasNoResults()) {
|
||||
System.out.println("battle terminated, "+ agent.getModel().currentRole() + " found no finding");
|
||||
break;
|
||||
}
|
||||
Challenge<SpecificRole, SpecificFinding, SpecificModel> challenge = agent.getContext().takeResult();
|
||||
if (challenge == null) {
|
||||
// lost the game
|
||||
System.out.println("lost the game: " + agent.getModel().currentRole());
|
||||
System.exit(1);
|
||||
}
|
||||
System.out.println("finding " + challenge.getFinding().toString());
|
||||
currentModel.applyFinding(challenge.getFinding());
|
||||
currentModel.nextRole();
|
||||
System.out.println(currentModel.toString());
|
||||
}
|
||||
engine.stop();
|
||||
}
|
||||
}
|
@ -1,91 +0,0 @@
|
||||
/**
|
||||
* Challenge.java
|
||||
* Copyright 2009 by Michael Peter Christen, Frankfurt a. M., Germany
|
||||
* First published 03.12.2009 at http://yacy.net
|
||||
*
|
||||
* $LastChangedDate$
|
||||
* $LastChangedRevision$
|
||||
* $LastChangedBy$
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program in the file lgpl21.txt
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.yacy.cora.ai.greedy;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
public class Challenge<
|
||||
SpecificRole extends Role,
|
||||
SpecificFinding extends Finding<SpecificRole>,
|
||||
SpecificModel extends Model<SpecificRole, SpecificFinding>
|
||||
> implements
|
||||
Comparator<Challenge<SpecificRole, SpecificFinding, SpecificModel>>,
|
||||
Comparable<Challenge<SpecificRole, SpecificFinding, SpecificModel>> {
|
||||
|
||||
private final Agent<SpecificRole,SpecificFinding,SpecificModel> agent;
|
||||
private final SpecificFinding finding;
|
||||
|
||||
public Challenge() {
|
||||
this.agent = null;
|
||||
this.finding = null;
|
||||
}
|
||||
|
||||
public Challenge(Agent<SpecificRole,SpecificFinding,SpecificModel> agent, SpecificFinding finding) {
|
||||
assert agent != null;
|
||||
assert finding != null;
|
||||
this.agent = agent;
|
||||
this.finding = finding;
|
||||
}
|
||||
|
||||
public Agent<SpecificRole,SpecificFinding,SpecificModel> getAgent() {
|
||||
return this.agent;
|
||||
}
|
||||
|
||||
public SpecificFinding getFinding() {
|
||||
return this.finding;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return this.agent.hashCode() + this.finding.hashCode();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
if (!(other instanceof Challenge)) return false;
|
||||
Challenge<SpecificRole, SpecificFinding, SpecificModel> c = (Challenge<SpecificRole, SpecificFinding, SpecificModel>) other;
|
||||
if (!this.agent.equals(c.agent)) return false;
|
||||
if (!this.finding.equals(c.finding)) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public int compare(
|
||||
Challenge<SpecificRole, SpecificFinding, SpecificModel> c1,
|
||||
Challenge<SpecificRole, SpecificFinding, SpecificModel> c2) {
|
||||
|
||||
// order of poison agents: they are the largest
|
||||
if (c1.agent == null) return 1;
|
||||
if (c2.agent == null) return -1;
|
||||
|
||||
// compare based on priority of the finding
|
||||
return c1.finding.compareTo(c2.finding);
|
||||
}
|
||||
|
||||
public int compareTo(Challenge<SpecificRole, SpecificFinding, SpecificModel> o) {
|
||||
return compare(this, o);
|
||||
}
|
||||
}
|
@ -1,253 +0,0 @@
|
||||
/**
|
||||
* Context.java
|
||||
* Copyright 2009 by Michael Peter Christen, Frankfurt a. M., Germany
|
||||
* First published 03.12.2009 at http://yacy.net
|
||||
*
|
||||
* $LastChangedDate$
|
||||
* $LastChangedRevision$
|
||||
* $LastChangedBy$
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program in the file lgpl21.txt
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.yacy.cora.ai.greedy;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.PriorityBlockingQueue;
|
||||
import java.util.concurrent.Semaphore;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class Context<
|
||||
SpecificRole extends Role,
|
||||
SpecificFinding extends Finding<SpecificRole>,
|
||||
SpecificModel extends Model<SpecificRole, SpecificFinding>
|
||||
>{
|
||||
|
||||
private static final Object PRESENT = "";
|
||||
private final Goal<SpecificRole, SpecificFinding, SpecificModel> goal;
|
||||
private final SpecificModel initialModel;
|
||||
private final SpecificRole initialRole;
|
||||
private final PriorityBlockingQueue<Challenge<SpecificRole, SpecificFinding, SpecificModel>> result;
|
||||
private final ConcurrentHashMap<SpecificModel, Object> models; // caches all observed models for a double-check
|
||||
private final ConcurrentHashMap<SpecificRole, Integer> bestMove;
|
||||
private final AtomicInteger instances;
|
||||
private final long timeoutForSnapshot;
|
||||
private final Semaphore termination;
|
||||
private final boolean feedAssetCache, useAssetCache;
|
||||
private long startTime;
|
||||
private boolean fullfilled;
|
||||
|
||||
protected Context(
|
||||
Goal<SpecificRole, SpecificFinding, SpecificModel> goal,
|
||||
SpecificModel initialModel,
|
||||
long timeoutForSnapshot, boolean feedAssetCache, boolean useAssetCache) {
|
||||
this.goal = goal;
|
||||
this.initialModel = initialModel;
|
||||
this.initialRole = initialModel.currentRole();
|
||||
this.models = new ConcurrentHashMap<SpecificModel, Object>();
|
||||
this.result = new PriorityBlockingQueue<Challenge<SpecificRole, SpecificFinding, SpecificModel>>();
|
||||
this.bestMove = new ConcurrentHashMap<SpecificRole, Integer>();
|
||||
this.instances = new AtomicInteger(0);
|
||||
this.timeoutForSnapshot = timeoutForSnapshot;
|
||||
this.startTime = System.currentTimeMillis();
|
||||
this.fullfilled = false;
|
||||
this.termination = new Semaphore(0);
|
||||
this.feedAssetCache = feedAssetCache;
|
||||
this.useAssetCache = useAssetCache;
|
||||
}
|
||||
|
||||
public int getInstanceCount() {
|
||||
return this.instances.get();
|
||||
}
|
||||
|
||||
public void incInstances() {
|
||||
this.instances.incrementAndGet();
|
||||
}
|
||||
|
||||
public int decInstances() {
|
||||
return this.instances.decrementAndGet();
|
||||
}
|
||||
|
||||
|
||||
public boolean setBestMove(SpecificRole role, int ranking) {
|
||||
Integer b = this.bestMove.get(role);
|
||||
if (b == null) {
|
||||
this.bestMove.put(role, ranking);
|
||||
System.out.println("first move");
|
||||
return true;
|
||||
}
|
||||
if (b.intValue() < ranking) {
|
||||
System.out.println("best next move");
|
||||
this.bestMove.put(role, ranking);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public int getBestMove(SpecificRole role) {
|
||||
Integer b = this.bestMove.get(role);
|
||||
if (b == null) return Integer.MIN_VALUE;
|
||||
return b.intValue();
|
||||
}
|
||||
|
||||
public void addModel(SpecificModel model) {
|
||||
if (model.toString().equals("[],[3, 2, 1],[]")) {
|
||||
System.out.println("target");
|
||||
}
|
||||
this.models.put(model, PRESENT);
|
||||
}
|
||||
|
||||
public Goal<SpecificRole, SpecificFinding, SpecificModel> getGoal() {
|
||||
return this.goal;
|
||||
}
|
||||
|
||||
public SpecificModel getInitialModel() {
|
||||
return this.initialModel;
|
||||
}
|
||||
|
||||
public boolean isKnownModel(SpecificModel model) {
|
||||
return this.models.containsKey(model);
|
||||
}
|
||||
|
||||
public int getKnownModelsCount() {
|
||||
return this.models.size();
|
||||
}
|
||||
|
||||
public void registerResult(Agent<SpecificRole, SpecificFinding, SpecificModel> agent, SpecificFinding finding) {
|
||||
assert agent != null;
|
||||
assert agent.getFinding() != null;
|
||||
assert agent.getFinding().getRole() != null;
|
||||
assert finding != null;
|
||||
this.result.offer(new Challenge<SpecificRole, SpecificFinding, SpecificModel>(agent, finding));
|
||||
}
|
||||
|
||||
public SpecificRole initialRole() {
|
||||
return this.initialRole;
|
||||
}
|
||||
|
||||
/**
|
||||
* return one of the results from the problem solving computation.
|
||||
* if there is no result available, then return null.
|
||||
* a null result shows that there is either no solution at all
|
||||
* or it was not possible to find one within the given time-out frame
|
||||
* @return e challenge as a result or null if there is no result.
|
||||
*/
|
||||
public Challenge<SpecificRole, SpecificFinding, SpecificModel> takeResult() {
|
||||
try {
|
||||
while (!this.result.isEmpty()) {
|
||||
Challenge<SpecificRole, SpecificFinding, SpecificModel> resultChallenge = this.result.take();
|
||||
Agent<SpecificRole, SpecificFinding, SpecificModel> resultAgent = resultChallenge.getAgent();
|
||||
if (resultAgent.isPrunedByTerminationInHistory(this.initialRole())) continue;
|
||||
return resultChallenge;
|
||||
}
|
||||
// if this state is reached then all possible findings will cause a lost situation
|
||||
|
||||
return null;
|
||||
} catch (final InterruptedException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean hasNoResults() {
|
||||
return this.result.isEmpty();
|
||||
}
|
||||
|
||||
public int countResults() {
|
||||
return this.result.size();
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
this.startTime = System.currentTimeMillis();
|
||||
this.termination.drainPermits();
|
||||
}
|
||||
|
||||
public boolean feedAssetCache() {
|
||||
return this.feedAssetCache;
|
||||
}
|
||||
|
||||
public boolean useAssetCache() {
|
||||
return this.useAssetCache;
|
||||
}
|
||||
|
||||
public void announceCompletion() {
|
||||
this.fullfilled = true;
|
||||
this.termination.release();
|
||||
}
|
||||
|
||||
public boolean isCompleted() {
|
||||
if (this.fullfilled) return true;
|
||||
//System.out.println("runtime = " + runtime);
|
||||
//boolean expired = System.currentTimeMillis() - this.startTime > this.timeoutForSnapshot;
|
||||
//if (expired) this.termination.release();
|
||||
//return expired;
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isSnapshotTimeout() {
|
||||
if (this.fullfilled) return true;
|
||||
boolean expired = System.currentTimeMillis() - this.startTime > this.timeoutForSnapshot;
|
||||
return expired;
|
||||
}
|
||||
|
||||
public void awaitTermination(long pauseAfterAquire, boolean announceCompletionAfterTimeOut) {
|
||||
try {
|
||||
if (this.termination.tryAcquire(this.timeoutForSnapshot, TimeUnit.MILLISECONDS)) {
|
||||
Thread.sleep(pauseAfterAquire);
|
||||
} else {
|
||||
System.out.println("timed-out termination");
|
||||
}
|
||||
} catch (final InterruptedException e) {}
|
||||
if (announceCompletionAfterTimeOut) announceCompletion();
|
||||
}
|
||||
|
||||
public void printReport(int count) {
|
||||
System.out.println("==== " + this.getKnownModelsCount() + " models computed");
|
||||
Challenge<SpecificRole, SpecificFinding, SpecificModel> resultChallenge;
|
||||
Agent<SpecificRole, SpecificFinding, SpecificModel> resultAgent;
|
||||
SpecificFinding resultFinding;
|
||||
int i = 0;
|
||||
while (count > 0) {
|
||||
if (this.countResults() == 0) break;
|
||||
resultChallenge = this.takeResult();
|
||||
resultAgent = resultChallenge.getAgent();
|
||||
resultFinding = resultChallenge.getFinding();
|
||||
List<SpecificFinding> p = resultAgent.listPrunedByTerminationInHistory();
|
||||
//if (p != null) continue;
|
||||
//if (resultAgent.isPrunedByTerminationInHistory(resultAgent.initialModel.currentRole())) continue;
|
||||
System.out.println("==== result " + i++ + "/" + this.countResults());
|
||||
Finding<SpecificRole>[] moves = resultAgent.getFindings();
|
||||
System.out.print("==== moves: ");
|
||||
if (moves == null) System.out.println("null"); else {
|
||||
for (int j = 0; j < moves.length; j++) {System.out.print(moves[j]); if (j < moves.length - 1) System.out.print(", "); }
|
||||
} System.out.println();
|
||||
System.out.println("==== first move: " + resultFinding);
|
||||
assert resultFinding.getPriority() == resultAgent.getRanking(this.initialRole());
|
||||
System.out.println("==== ranking: " + resultFinding.getPriority());
|
||||
SpecificRole winner = resultAgent.getModel().isTermination();
|
||||
if (winner != null) System.out.println("==== the winner is " + winner.toString());
|
||||
if (p == null) {
|
||||
System.out.println("==== pruning is null");
|
||||
} else {
|
||||
System.out.print("==== fail-moves are ");
|
||||
for (SpecificFinding f: p) System.out.print(f + " ");
|
||||
System.out.println("");
|
||||
}
|
||||
System.out.println(resultAgent.getModel().toString());
|
||||
count--;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,49 +0,0 @@
|
||||
/**
|
||||
* ContextFactory.java
|
||||
* Copyright 2009 by Michael Peter Christen, Frankfurt a. M., Germany
|
||||
* First published 03.12.2009 at http://yacy.net
|
||||
*
|
||||
* $LastChangedDate$
|
||||
* $LastChangedRevision$
|
||||
* $LastChangedBy$
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program in the file lgpl21.txt
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.yacy.cora.ai.greedy;
|
||||
|
||||
public class ContextFactory<
|
||||
SpecificRole extends Role,
|
||||
SpecificFinding extends Finding<SpecificRole>,
|
||||
SpecificModel extends Model<SpecificRole, SpecificFinding>
|
||||
>{
|
||||
|
||||
private final Goal<SpecificRole, SpecificFinding, SpecificModel> goal;
|
||||
private final long timeoutForSnapshot;
|
||||
private final boolean feedAssetCache, useAssetCache;
|
||||
|
||||
public ContextFactory(
|
||||
Goal<SpecificRole, SpecificFinding, SpecificModel> goal,
|
||||
long timeoutForSnapshot, boolean feedAssetCache, boolean useAssetCache) {
|
||||
this.goal = goal;
|
||||
this.timeoutForSnapshot = timeoutForSnapshot;
|
||||
this.feedAssetCache = feedAssetCache;
|
||||
this.useAssetCache = useAssetCache;
|
||||
}
|
||||
|
||||
public Context<SpecificRole, SpecificFinding, SpecificModel> produceContext(SpecificModel startModel) {
|
||||
return new Context<SpecificRole, SpecificFinding, SpecificModel>(this.goal, startModel, timeoutForSnapshot, feedAssetCache, useAssetCache);
|
||||
}
|
||||
}
|
@ -1,265 +0,0 @@
|
||||
/**
|
||||
* Engine.java
|
||||
* Copyright 2009 by Michael Peter Christen, Frankfurt a. M., Germany
|
||||
* First published 03.12.2009 at http://yacy.net
|
||||
*
|
||||
* $LastChangedDate$
|
||||
* $LastChangedRevision$
|
||||
* $LastChangedBy$
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program in the file lgpl21.txt
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.yacy.cora.ai.greedy;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.PriorityQueue;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.PriorityBlockingQueue;
|
||||
|
||||
public class Engine<
|
||||
SpecificRole extends Role,
|
||||
SpecificFinding extends Finding<SpecificRole>,
|
||||
SpecificModel extends Model<SpecificRole, SpecificFinding>
|
||||
> {
|
||||
|
||||
protected final PriorityBlockingQueue<Agent<SpecificRole, SpecificFinding, SpecificModel>> agentQueue;
|
||||
protected final PriorityBlockingQueue<Challenge<SpecificRole, SpecificFinding, SpecificModel>> challengeQueue;
|
||||
protected final Agent<SpecificRole, SpecificFinding, SpecificModel> poisonAgent;
|
||||
protected final Challenge<SpecificRole, SpecificFinding, SpecificModel> poisonChallenge;
|
||||
protected final ConcurrentHashMap<SpecificModel, List<SpecificFinding>> settings;
|
||||
protected final ConcurrentHashMap<Asset<SpecificRole, SpecificFinding, SpecificModel>, SpecificModel> assets;
|
||||
private final int cores;
|
||||
|
||||
|
||||
public Engine(int cores) {
|
||||
this.cores = cores;
|
||||
this.poisonAgent = new Agent<SpecificRole, SpecificFinding, SpecificModel>();
|
||||
this.poisonChallenge = new Challenge<SpecificRole, SpecificFinding, SpecificModel>();
|
||||
this.agentQueue = new PriorityBlockingQueue<Agent<SpecificRole, SpecificFinding, SpecificModel>>();
|
||||
this.challengeQueue = new PriorityBlockingQueue<Challenge<SpecificRole, SpecificFinding, SpecificModel>>();
|
||||
this.settings = new ConcurrentHashMap<SpecificModel, List<SpecificFinding>>();
|
||||
this.assets = new ConcurrentHashMap<Asset<SpecificRole, SpecificFinding, SpecificModel>, SpecificModel>();
|
||||
}
|
||||
|
||||
public void start() {
|
||||
int c = (this.cores == 0) ? Runtime.getRuntime().availableProcessors() : this.cores;
|
||||
for (int i = 0; i < c; i++) {
|
||||
new SettingRunner().start();
|
||||
new AssetRunner().start();
|
||||
}
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
int c = (this.cores == 0) ? Runtime.getRuntime().availableProcessors() : this.cores;
|
||||
for (int i = 0; i < c; i++) {
|
||||
this.agentQueue.put(this.poisonAgent);
|
||||
this.challengeQueue.put(this.poisonChallenge);
|
||||
}
|
||||
}
|
||||
|
||||
public void inject(Agent<SpecificRole, SpecificFinding, SpecificModel> agent) {
|
||||
agent.getContext().reset();
|
||||
this.agentQueue.put(agent);
|
||||
}
|
||||
|
||||
|
||||
public class SettingRunner extends Thread {
|
||||
@Override
|
||||
public void run() {
|
||||
Agent<SpecificRole, SpecificFinding, SpecificModel> agent;
|
||||
Challenge<SpecificRole, SpecificFinding, SpecificModel> challenge;
|
||||
Context<SpecificRole, SpecificFinding, SpecificModel> context;
|
||||
SpecificModel model;
|
||||
List<SpecificFinding> findings;
|
||||
PriorityQueue<Challenge<SpecificRole, SpecificFinding, SpecificModel>> preChallenge = new PriorityQueue<Challenge<SpecificRole, SpecificFinding, SpecificModel>>();
|
||||
try {
|
||||
while ((agent = agentQueue.take()) != poisonAgent) {
|
||||
// check termination of that goal
|
||||
context = agent.getContext();
|
||||
if (context.isCompleted()) continue;
|
||||
|
||||
// produce findings in a setting environment and try to get it from a cache
|
||||
model = agent.getModel();
|
||||
findings = settings.get(model);
|
||||
if (findings == null) {
|
||||
findings = model.explore();
|
||||
settings.put(model, findings);
|
||||
}
|
||||
|
||||
// branch
|
||||
for (SpecificFinding finding: findings) {
|
||||
challenge = new Challenge<SpecificRole, SpecificFinding, SpecificModel>(agent, finding);
|
||||
//System.out.println("finding: " + finding.toString() + ", priority: " + finding.getPriority());
|
||||
preChallenge.add(challenge);
|
||||
}
|
||||
challengefeeder: while (!preChallenge.isEmpty()) {
|
||||
if (context.isCompleted()) break challengefeeder;
|
||||
challengeQueue.put(preChallenge.poll());
|
||||
agent.incInstances();
|
||||
}
|
||||
//while (!challengeQueue.isEmpty()) System.out.println("finding: " + challengeQueue.take().getChallenge().getFinding().toString());
|
||||
}
|
||||
} catch (final InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class AssetRunner extends Thread {
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public void run() {
|
||||
Challenge<SpecificRole, SpecificFinding, SpecificModel> challenge;
|
||||
Agent<SpecificRole, SpecificFinding, SpecificModel> agent, nextAgent;
|
||||
Goal<SpecificRole, SpecificFinding, SpecificModel> goal;
|
||||
Context<SpecificRole, SpecificFinding, SpecificModel> context;
|
||||
Asset<SpecificRole, SpecificFinding, SpecificModel> asset;
|
||||
SpecificRole role;
|
||||
SpecificModel nextModel = null;
|
||||
try {
|
||||
while ((challenge = challengeQueue.take()) != poisonChallenge) {
|
||||
assert challenge != null;
|
||||
agent = challenge.getAgent();
|
||||
agent.decInstances();
|
||||
context = agent.getContext();
|
||||
goal = context.getGoal();
|
||||
role = challenge.getFinding().getRole();
|
||||
|
||||
// check termination by catching the global termination signal
|
||||
// and check expiration before applying finding
|
||||
// this shall not place the model to the results because
|
||||
// it has not the last finding assigned to the current user
|
||||
|
||||
if (context.isCompleted()) continue;
|
||||
|
||||
String debug = agent.getModel().toString() + "\nwill apply " + challenge.getFinding().toString();
|
||||
System.out.println(debug);
|
||||
if (debug.equals("[],[3, 2],[1]\nwill apply 2 -> 1")) {
|
||||
System.out.println("one more please");
|
||||
}
|
||||
// apply finding: compute next model
|
||||
// avoid double computation of findings using cached assets
|
||||
if (context.useAssetCache()) {
|
||||
asset = new Asset<SpecificRole, SpecificFinding, SpecificModel>(agent.getModel(), challenge.getFinding());
|
||||
nextModel = assets.get(asset);
|
||||
if (nextModel == null) {
|
||||
// generate model clone and apply finding
|
||||
try {
|
||||
nextModel = (SpecificModel) agent.getModel().clone();
|
||||
} catch (final CloneNotSupportedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
nextModel.applyFinding(challenge.getFinding());
|
||||
nextModel.nextRole();
|
||||
if (context.feedAssetCache()) assets.put(asset, nextModel);
|
||||
}
|
||||
} else {
|
||||
// generate model clone and apply finding
|
||||
try {
|
||||
nextModel = (SpecificModel) agent.getModel().clone();
|
||||
} catch (final CloneNotSupportedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
nextModel.applyFinding(challenge.getFinding());
|
||||
nextModel.nextRole();
|
||||
if (context.feedAssetCache()) {
|
||||
asset = new Asset<SpecificRole, SpecificFinding, SpecificModel>(agent.getModel(), challenge.getFinding());
|
||||
assets.put(asset, nextModel);
|
||||
}
|
||||
}
|
||||
|
||||
// prune double-occurring models:
|
||||
// while it appears not to be a very good idea to produce models from the
|
||||
// asset cache above and then prune with double appearing models here, this is
|
||||
// still useful if the assets come from an earlier injection of an other agent.
|
||||
// Because the asset cache is a global cache owned by the engine it can produce
|
||||
// ready-computed models that not already exist in the agent cache
|
||||
if (agent.getContext().isKnownModel(nextModel)) {
|
||||
agent.checkInstanceCount();
|
||||
continue;
|
||||
}
|
||||
// place new model into agent and record finding
|
||||
nextAgent = new Agent<SpecificRole, SpecificFinding, SpecificModel>(agent, nextModel, challenge.getFinding());
|
||||
nextAgent.checkInstanceCount();
|
||||
|
||||
// check if we arrived at a termination point
|
||||
SpecificRole terminationRole = nextModel.isTermination();
|
||||
if (terminationRole != null) {
|
||||
// the current role has a termination situation. In case that it is the start user, add a result
|
||||
nextAgent.addResult();
|
||||
if (goal.isFulfilled(nextAgent.getModel())) nextAgent.getContext().announceCompletion();
|
||||
// one of the roles has terminated.
|
||||
// prune this branch for other branches from the parent
|
||||
//System.out.println("terminationRole = " + terminationRole);
|
||||
if (agent.getFinding() == null) {
|
||||
// this is the root of the search tree: a fail in the search
|
||||
//agent.getContext().getGoal().announceFullfillment();
|
||||
} else {
|
||||
assert agent.getFinding() != null;
|
||||
assert agent.getFinding().getRole() != null;
|
||||
agent.setFindingFail();
|
||||
}
|
||||
//System.out.println("found winner model for " + terminationRole.toString() + ", latest finding: " + challenge.getFinding().toString() + "\n" + nextModel.toString());
|
||||
agent.checkInstanceCount();
|
||||
continue;
|
||||
}
|
||||
|
||||
// check time-out for snapshot
|
||||
if (nextAgent.getContext().isSnapshotTimeout()) {
|
||||
nextAgent.addResult();
|
||||
}
|
||||
|
||||
// check pruning
|
||||
|
||||
if (goal.pruning(nextModel)) {
|
||||
agent.checkInstanceCount();
|
||||
continue;
|
||||
}
|
||||
|
||||
// do not follow situations where it is known that somebody has made a fatal move in the past
|
||||
if (agent.isPrunedByTerminationInHistory() != null) {
|
||||
agent.checkInstanceCount();
|
||||
continue;
|
||||
}
|
||||
|
||||
// check best move criteria
|
||||
int ranking = agent.getRanking(role);
|
||||
if (context.setBestMove(role, ranking)) {
|
||||
nextAgent.addResult();
|
||||
}
|
||||
|
||||
// check snapshot
|
||||
if (goal.isSnapshot(nextModel)) {
|
||||
nextAgent.addResult();
|
||||
// no pruning here
|
||||
}
|
||||
|
||||
if (context.hasNoResults()) nextAgent.addResult();
|
||||
if (context.isCompleted()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// stack agent for next loop
|
||||
// using the priority of the next role
|
||||
agentQueue.put(nextAgent);
|
||||
}
|
||||
} catch (final InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,82 +0,0 @@
|
||||
/**
|
||||
* Finding.java
|
||||
* Copyright 2009 by Michael Peter Christen, Frankfurt a. M., Germany
|
||||
* First published 03.12.2009 at http://yacy.net
|
||||
*
|
||||
* $LastChangedDate$
|
||||
* $LastChangedRevision$
|
||||
* $LastChangedBy$
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program in the file lgpl21.txt
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.yacy.cora.ai.greedy;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
/**
|
||||
* a Finding object defines one single alternative that a given role has
|
||||
* within the given current model instance. A set of Finding objects is the
|
||||
* set of possibilities that a specific role has in a given situation as
|
||||
* defined by the model.
|
||||
* Findings can be classified with priorities that may be computed by strategy rules.
|
||||
* A Finding priority does not mean a ranking of the finding outcome, but a ranking
|
||||
* on the set of possible findings.
|
||||
*
|
||||
* @param <SpecificRole> a Role
|
||||
*/
|
||||
public interface Finding<SpecificRole extends Role> extends Comparator<Finding<SpecificRole>>, Comparable<Finding<SpecificRole>>, Cloneable {
|
||||
|
||||
/**
|
||||
* clone the model
|
||||
* @return a top-level cloned model
|
||||
* @throws CloneNotSupportedException
|
||||
*/
|
||||
public Object clone() throws CloneNotSupportedException;
|
||||
|
||||
/**
|
||||
* get the finding priority
|
||||
* @return
|
||||
*/
|
||||
public int getPriority();
|
||||
|
||||
/**
|
||||
* set the current priority
|
||||
* This may only be used internally as part of the engine process to create a result queue
|
||||
*/
|
||||
public void setPriority(int newPriority);
|
||||
|
||||
/**
|
||||
* get the finding role
|
||||
* @return
|
||||
*/
|
||||
public SpecificRole getRole();
|
||||
|
||||
/**
|
||||
* the equals method, necessary to place findings into hashtables
|
||||
* @param other
|
||||
* @return true if this finding is equal to the other finding
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object other);
|
||||
|
||||
/**
|
||||
* the hash code computation, necessary to place findings into hashtables
|
||||
* @return a hash code for this object
|
||||
*/
|
||||
@Override
|
||||
public int hashCode();
|
||||
|
||||
}
|
@ -1,37 +0,0 @@
|
||||
/**
|
||||
* Goal.java
|
||||
* Copyright 2009 by Michael Peter Christen, Frankfurt a. M., Germany
|
||||
* First published 03.12.2009 at http://yacy.net
|
||||
*
|
||||
* $LastChangedDate$
|
||||
* $LastChangedRevision$
|
||||
* $LastChangedBy$
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program in the file lgpl21.txt
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.yacy.cora.ai.greedy;
|
||||
|
||||
|
||||
public interface Goal<
|
||||
SpecificRole extends Role,
|
||||
SpecificFinding extends Finding<SpecificRole>,
|
||||
SpecificModel extends Model<SpecificRole, SpecificFinding>
|
||||
> {
|
||||
|
||||
public boolean pruning(SpecificModel model);
|
||||
public boolean isSnapshot(SpecificModel model);
|
||||
public boolean isFulfilled(SpecificModel model);
|
||||
}
|
@ -1,118 +0,0 @@
|
||||
/**
|
||||
* Model.java
|
||||
* Copyright 2009 by Michael Peter Christen, Frankfurt a. M., Germany
|
||||
* First published 03.12.2009 at http://yacy.net
|
||||
*
|
||||
* $LastChangedDate$
|
||||
* $LastChangedRevision$
|
||||
* $LastChangedBy$
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program in the file lgpl21.txt
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.yacy.cora.ai.greedy;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* a Model is the universe where specific Roles act with specific Findings.
|
||||
* The Model may be a playing field if applied for game playing or it may
|
||||
* be a theorem set if applied for automated theorem proving.
|
||||
* A specific Model should provide a simple initializer that creates a start model,
|
||||
* which may be a startup board in case of game playing or a standard model in case
|
||||
* of automated theorem proving.
|
||||
* Models provide exploration (creation of possible Findings) and ranking (evaluation
|
||||
* of a model situation to produce a ordering on possible model instances)
|
||||
*
|
||||
* @param <SpecificRole>
|
||||
* @param <SpecificFinding>
|
||||
*/
|
||||
public interface Model<SpecificRole extends Role, SpecificFinding extends Finding<SpecificRole>> extends Cloneable {
|
||||
|
||||
/**
|
||||
* Create a list of possible findings in the current model instance.
|
||||
* This may be the list of possible moves for the current role/player in case of
|
||||
* game playing or the list of possible inference rule applications in case of
|
||||
* automated theorem proving.
|
||||
* @return an iterator on the list of all possible findings for the current model situation
|
||||
*/
|
||||
public List<SpecificFinding> explore();
|
||||
|
||||
/**
|
||||
* apply a finding to the current model.
|
||||
*/
|
||||
public void applyFinding(SpecificFinding finding);
|
||||
|
||||
/**
|
||||
* compute the ranking for the given role, the higher the better.
|
||||
* @param findings the number of findings that have applied so far
|
||||
* @param role the role for which the ranking shall be computed
|
||||
* @return the ranking for the given finding size and role
|
||||
*/
|
||||
public int getRanking(int findings, SpecificRole role);
|
||||
|
||||
/**
|
||||
* the model contains a status about roles that may act next
|
||||
* @return the next role
|
||||
*/
|
||||
public SpecificRole currentRole();
|
||||
|
||||
/**
|
||||
* switch to the next role. The current role is migrated to the
|
||||
* new role status, or the current rule is replaced with a new role
|
||||
*/
|
||||
public void nextRole();
|
||||
|
||||
/**
|
||||
* based on the model content and the model next-role compute a hash code
|
||||
* do not include a computation based on latest actions
|
||||
* @return a hash code
|
||||
*/
|
||||
@Override
|
||||
public int hashCode();
|
||||
|
||||
/**
|
||||
* equal mathod according to hash method
|
||||
* @param om
|
||||
* @return true if other model is equal to the current model.
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object om);
|
||||
|
||||
/**
|
||||
* clone the model
|
||||
* @return a top-level cloned model
|
||||
* @throws CloneNotSupportedException
|
||||
*/
|
||||
public Object clone() throws CloneNotSupportedException;
|
||||
|
||||
/**
|
||||
* check if this model is in a termination status for a given role
|
||||
*
|
||||
* @param role the role that is checked
|
||||
* @return true if the role caused termination
|
||||
*/
|
||||
public boolean isTermination(SpecificRole role);
|
||||
|
||||
/**
|
||||
* check if this model is in a termination status for anyone
|
||||
*
|
||||
* @return the role for which this is a termination
|
||||
* or null if there is no termination yet
|
||||
*/
|
||||
public SpecificRole isTermination();
|
||||
|
||||
}
|
@ -1,58 +0,0 @@
|
||||
/**
|
||||
* Role.java
|
||||
* Copyright 2009 by Michael Peter Christen, Frankfurt a. M., Germany
|
||||
* First published 03.12.2009 at http://yacy.net
|
||||
*
|
||||
* $LastChangedDate$
|
||||
* $LastChangedRevision$
|
||||
* $LastChangedBy$
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program in the file lgpl21.txt
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.yacy.cora.ai.greedy;
|
||||
|
||||
/**
|
||||
* a Role object is usually an enumeration object that is produced
|
||||
* in a enumeration that implements the Role interface.
|
||||
* All role object instances must define that one specific other Role
|
||||
* follows the current role. If there are undefined role or multiple role
|
||||
* situations, these may be declared with special role instance constants
|
||||
*/
|
||||
public interface Role {
|
||||
|
||||
/**
|
||||
* define which other role shall follow to the current role
|
||||
* this returns a cloned role
|
||||
* @return the next role
|
||||
*/
|
||||
public Role nextRole();
|
||||
|
||||
/**
|
||||
* necessary equals method for usage of Role in hash tables
|
||||
* @param obj
|
||||
* @return true if the current role and the given role are equal
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object obj);
|
||||
|
||||
/**
|
||||
* necessary hashCode method for usage of Role in hash tables
|
||||
* @return a hash code of the role
|
||||
*/
|
||||
@Override
|
||||
public int hashCode();
|
||||
|
||||
}
|
@ -1,35 +0,0 @@
|
||||
/**
|
||||
* Unirole.java
|
||||
* Copyright 2010 by Michael Peter Christen, Frankfurt a. M., Germany
|
||||
* First published 06.01.2010 at http://yacy.net
|
||||
*
|
||||
* $LastChangedDate$
|
||||
* $LastChangedRevision$
|
||||
* $LastChangedBy$
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program in the file lgpl21.txt
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.yacy.cora.ai.greedy;
|
||||
|
||||
public enum Unirole implements Role {
|
||||
i;
|
||||
|
||||
public Role nextRole() {
|
||||
return this;
|
||||
}
|
||||
|
||||
public static Unirole unirole = Unirole.i;
|
||||
}
|
@ -1,124 +0,0 @@
|
||||
/**
|
||||
* TripleStore
|
||||
* Copyright 2011 by Michael Peter Christen, mc@yacy.net, Frankfurt am Main, Germany
|
||||
* First released 16.12.2011 at http://yacy.net
|
||||
*
|
||||
* $LastChangedDate: 2011-04-14 00:04:23 +0200 (Do, 14 Apr 2011) $
|
||||
* $LastChangedRevision: 7653 $
|
||||
* $LastChangedBy: orbiter $
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program in the file lgpl21.txt
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
package net.yacy.cora.lod;
|
||||
|
||||
import java.util.AbstractMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
import net.yacy.cora.lod.vocabulary.Rdf;
|
||||
import net.yacy.cora.order.ByteOrder;
|
||||
import net.yacy.cora.order.CloneableIterator;
|
||||
import net.yacy.cora.storage.MapStore;
|
||||
|
||||
public class MapTripleStore {
|
||||
|
||||
MapStore store;
|
||||
|
||||
public MapTripleStore(MapStore store) {
|
||||
this.store = store;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
this.store.clear();
|
||||
}
|
||||
|
||||
public boolean contains(byte[] id) {
|
||||
return this.store.containsKey(id);
|
||||
}
|
||||
|
||||
public Node get(byte[] id) {
|
||||
Map<String, byte[]> n = this.store.get(id);
|
||||
if (n == null) return null;
|
||||
return new Node(Rdf.Description, n);
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return this.store.isEmpty();
|
||||
}
|
||||
|
||||
public Node put(byte[] id, Node node) {
|
||||
Map<String, byte[]> n = this.store.put(id, node);
|
||||
if (n == null) return null;
|
||||
return new Node(Rdf.Description, n);
|
||||
}
|
||||
|
||||
public void putAll(MapTripleStore entries) {
|
||||
Iterator<Map.Entry<byte[], Node>> i = entries.iterator();
|
||||
Map.Entry<? extends byte[], ? extends Node> entry;
|
||||
while (i.hasNext()) {
|
||||
entry = i.next();
|
||||
this.put(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
public Node remove(byte[] id) {
|
||||
Map<String, byte[]> n = this.store.remove(id);
|
||||
if (n == null) return null;
|
||||
return new Node(Rdf.Description, n);
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return this.store.size();
|
||||
}
|
||||
|
||||
public Iterator<java.util.Map.Entry<byte[], Node>> iterator() {
|
||||
final Iterator<byte[]> id = this.idIterator();
|
||||
return new Iterator<Map.Entry<byte[], Node>>(){
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return id.hasNext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map.Entry<byte[], Node> next() {
|
||||
byte[] key = id.next();
|
||||
if (key == null) return null;
|
||||
return new AbstractMap.SimpleImmutableEntry<byte[], Node>(key, MapTripleStore.this.get(key));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
id.remove();
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
public ByteOrder getOrdering() {
|
||||
return this.store.getOrdering();
|
||||
}
|
||||
|
||||
public CloneableIterator<byte[]> idIterator() {
|
||||
return this.store.keyIterator();
|
||||
}
|
||||
|
||||
public synchronized void close() {
|
||||
this.store.close();
|
||||
}
|
||||
|
||||
}
|
@ -1,69 +0,0 @@
|
||||
// whois.java
|
||||
// -------------------------------------
|
||||
// (C) by Michael Peter Christen; mc@yacy.net
|
||||
// first published on http://www.anomic.de
|
||||
// Frankfurt, Germany, 2005
|
||||
//
|
||||
// $LastChangedDate$
|
||||
// $LastChangedRevision$
|
||||
// $LastChangedBy$
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation; either version 2 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
package net.yacy.utils;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.Properties;
|
||||
|
||||
public class whois {
|
||||
|
||||
public static Properties Whois(final String dom) {
|
||||
try {
|
||||
final Process p = Runtime.getRuntime().exec("whois " + dom);
|
||||
final BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
|
||||
String line, key, value, oldValue;
|
||||
int pos;
|
||||
final Properties result = new Properties();
|
||||
while ((line = br.readLine()) != null) {
|
||||
pos = line.indexOf(':',0);
|
||||
if (pos > 0) {
|
||||
key = line.substring(0, pos).trim().toLowerCase();
|
||||
value = line.substring(pos + 1).trim();
|
||||
//System.out.println(key + ":" + value);
|
||||
oldValue = result.getProperty(key);
|
||||
result.setProperty(key, (oldValue == null) ? value : (oldValue + "; " + value));
|
||||
}
|
||||
}
|
||||
br.close();
|
||||
return result;
|
||||
} catch (final IOException e) {
|
||||
//Log.logException(e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static String evaluateWhois(final Properties p) {
|
||||
String info1, info2;
|
||||
info1 = p.getProperty("netname");
|
||||
info2 = p.getProperty("descr");
|
||||
if ((info1 != null) && (info2 != null)) return info1 + " / " + info2;
|
||||
info1 = p.getProperty("type");
|
||||
info2 = p.getProperty("name");
|
||||
if ((info1 != null) && (info2 != null) && (info1.toLowerCase().startsWith("person"))) return "Person: " + info2;
|
||||
return "unknown";
|
||||
}
|
||||
}
|
Loading…
Reference in new issue