Revision 122:687646b15189

View differences:

src/main/java/cz/agents/dimaptools/preprocess/SASDomain.java
70 70
		operators.add(new SASOperator(name,pre,eff));
71 71
	}
72 72

  
73
	public int operators() {
74
		return operators.size();
75
	}
76

  
73 77
}
src/main/java/cz/agents/dimaptools/preprocess/SASFact.java
45 45
	}
46 46

  
47 47

  
48
	@Override
49
	public String toString() {
50
		return var + "-" + val;
51
	}
52
	
53
	
54

  
55

  
48 56
}
src/main/java/cz/agents/dimaptools/preprocess/SASParser.java
9 9
import java.util.HashMap;
10 10
import java.util.Map;
11 11

  
12
import org.apache.log4j.Level;
13 12
import org.apache.log4j.Logger;
14 13

  
15 14

  
src/main/java/cz/agents/dimaptools/preprocess/SASPreprocessor.java
22 22
/**
23 23
 * Translates textual representation into concise integer representation and performs factorization of the operators and
24 24
 * the domains.
25
 * 
26
 * WARNING: variable AND value names must be unique across all agents! TODO: fix that
25 27
 *
26 28
 * configuration:
27 29
 * treatGoalAsPublic(boolean) - all goal achieving actions are treated as public, default: true
......
89 91
		this.sas = sas;
90 92
		treatGoalAsPublic = config.getBoolean("treatGoalAsPublic", true);
91 93

  
92
//		LOGGER.setLevel(Level.INFO);
94
//		LOGGER.setLevel(Level.DEBUG);
93 95

  
94 96
		LinkedList<String> agents = new LinkedList<String>(addl.getAgentList());
95 97

  
......
105 107
			
106 108
			for(String agent : agents){
107 109
				int index = action.label.indexOf("-" + agent);
108
				if(index < minIndex){
110
				if(index > 0 && index < minIndex){
109 111
					minIndex = index;
110 112
					minAgent = agent;
111 113
				}
......
133 135
	//				TODO: use var-val strings or some SASFact class -> may be more efficient
134 136

  
135 137
					//compare each operator with each other
138
					//TODO: should not add again var-vals from already compared operators!
136 139
					for(SASOperator op1 : agentOperators.get(a1)){
137 140
						for(SASOperator op2 : agentOperators.get(a2)){
138 141

  
......
237 240
		//set global size
238 241
		for(String agent : agents){
239 242
			agentDomains.get(agent).setSize(varCount);
240
			LOGGER.info(agentDomains.get(agent));
241
			LOGGER.info(agentDomains.get(agent).getNames());
243
			if(LOGGER.isInfoEnabled()){
244
				LOGGER.info(agentDomains.get(agent));
245
				LOGGER.info(agentDomains.get(agent).getNames());
246
			}
247
			
242 248
		}
243 249

  
244 250

  
......
350 356
		//translate initial state
351 357
		int[] init = new int[d.sizeGlobal()];
352 358
		for(String var : sas.init.keySet()){
353
			LOGGER.debug(var + ":" + varCodes.get(var) + ", " + sas.init.get(var) + ":" + valCodes.get(sas.init.get(var)));
359
			if(LOGGER.isDebugEnabled())LOGGER.info(var + ":" + varCodes.get(var) + ", " + sas.init.get(var) + ":" + valCodes.get(sas.init.get(var)));
354 360
			init[varCodes.get(var)] = valCodes.get(sas.init.get(var));
355 361
		}
356 362
		State initState = new State(d,init);
......
435 441
		return goalSuperState;
436 442
	}
437 443

  
444
	public SASDomain getSASDomain() {
445
		return sas;
446
	}
447
	
438 448
}
src/main/java/cz/agents/dimaptools/preprocess/SASWriter.java
1
package cz.agents.dimaptools.preprocess;
2

  
3
import java.io.File;
4
import java.io.FileNotFoundException;
5
import java.io.PrintStream;
6
import java.util.HashMap;
7
import java.util.Map;
8

  
9
public class SASWriter {
10
	
11
	private final SASDomain sas;
12
	private Map<String,Integer> varCodes = new HashMap<String, Integer>();
13
	private Map<String,Integer> valCodes = new HashMap<String, Integer>();
14
	
15
	public SASWriter(SASDomain sas) {
16
		super();
17
		this.sas = sas;
18
	}
19
	
20
	public void write(PrintStream out){
21
		out.println(generate());
22
	}
23
	
24
	public void write(File file){
25
		try {
26
			PrintStream out = new PrintStream(file);
27
			out.println(generate());
28
			out.close();
29
		} catch (FileNotFoundException e) {
30
			// TODO Auto-generated catch block
31
			e.printStackTrace();
32
		} 
33
	}
34
	
35
	public String generate(){
36
		String //header
37
		out = "begin_version\n3\nend_version\nbegin_metric\n0\nend_metric\n";
38
		
39
		//variables
40
		int varc = 0;
41
		out += sas.getVariables().size() + "\n";
42
		for(String var : sas.getVariables()){
43
			varCodes.put(var, varc++);
44
			out += "begin_variable\n";
45
			out += var + "\n";
46
			out += "-1\n";
47
			out += sas.getDomain(var).size() + "\n";
48
			
49
			int valc = 0;
50
			for(String val : sas.getDomain(var)){
51
				valCodes.put(val, valc++);
52
				out += "Atom " + val + "\n";
53
			}
54
			out += "end_variable\n";
55
		}
56
		
57
		//mutexes
58
		out += "0\n";
59
		
60
		//init
61
		out += "begin_state\n";
62
		for(String var : sas.getVariables()){
63
			out += valCodes.get(sas.init.get(var)) + "\n";
64
		}
65
		out += "end_state\n";
66
		
67
		//goal
68
		out += "begin_goal\n";
69
		out += sas.goal.size() + "\n";
70
		for(String var : sas.goal.keySet()){
71
			out += varCodes.get(var) + " " + valCodes.get(sas.goal.get(var)) + "\n";
72
		}
73
		out += "end_goal\n";
74
		
75
		// variables
76
		out += sas.operators.size() + "\n";
77
		for (SASOperator op : sas.operators) {
78
			out += "begin_operator\n";
79
			out += op.label + "\n";
80
			
81
			//prevail conditions
82
			int pre = 0;
83
			String preS = "";
84
			for(String var : op.pre.keySet()){
85
				if(op.eff.get(var)==null){
86
					++pre;
87
					preS += varCodes.get(var) + " " + valCodes.get(op.pre.get(var)) + "\n";
88
				}
89
			}
90
			out += pre + "\n";
91
			out += preS;
92
			
93
			//effects
94
			out += op.eff.size() + "\n";
95
			for(String var : op.eff.keySet()){
96
				out += "0 " + varCodes.get(var) + " " + valCodes.get(op.pre.get(var)) + " " + valCodes.get(op.eff.get(var)) + "\n";
97
			}
98
			
99
			//cost
100
			out += "0\n";
101
			
102
			out += "end_operator\n";
103
		}
104
		
105
		return out;
106
	}
107
	
108
	
109

  
110
}
src/test/java/cz/agents/dimaptools/preprocess/TestWriter.java
1
package cz.agents.dimaptools.preprocess;
2

  
3
import static org.junit.Assert.fail;
4

  
5
import java.io.File;
6

  
7
import org.apache.log4j.Logger;
8
import org.junit.Test;
9

  
10
import cz.agents.dimaptools.addl.ADDLObject;
11
import cz.agents.dimaptools.addl.ADDLParser;
12
import cz.agents.dimaptools.domain.Problem;
13

  
14
public class TestWriter {
15

  
16
	private static final Logger LOGGER = Logger.getLogger(TestWriter.class);
17

  
18
	private static final String RESOURCES = "./src/test/resources/";
19

  
20
	@Test
21
	public void test() {
22

  
23
//		test("truck-a1");
24
//		test("logistics-a2");
25

  
26
	}
27

  
28
	public void test(String domain){
29
		String sasFileName = RESOURCES + domain + ".pre";
30
		String agentFileName = RESOURCES + domain + ".addl";
31

  
32
		File sasFile = new File(sasFileName);
33
        if (!sasFile.exists()) {
34
            fail("SAS file " + sasFileName + " does not exist!");
35
            System.exit(1);
36
        }
37

  
38
        File agentFile = new File(agentFileName);
39
        if (!agentFile.exists()) {
40
            fail("Agent file " + agentFileName + " does not exist!");
41
            System.exit(1);
42
        }
43
        ADDLObject addl = new ADDLParser().parse(agentFile);
44

  
45
        SASParser parser = new SASParser(sasFile);
46
        SASPreprocessor preprocessor = new SASPreprocessor(parser.getDomain(), addl);
47

  
48
        for(String agent : addl.getAgentList()){
49
        	LOGGER.info("DOMAIN("+agent+"):");
50
            LOGGER.info(preprocessor.getDomainForAgent(agent));
51

  
52
            LOGGER.info("PROBLEM("+agent+"):");
53
            Problem problem = preprocessor.getProblemForAgent(agent);
54
            LOGGER.info("init: " + problem.initState);
55
            LOGGER.info("goal: " + problem.goalSuperState);
56
            LOGGER.info("actions: \n" + problem);
57
        }
58
        
59
        SASWriter writer = new SASWriter(preprocessor.getSASDomain());
60
        writer.write(System.out);
61
	}
62

  
63
}

Also available in: Unified diff