# Evolutionary System Design with Answer Set Programming
## 4 Encoding the System Synthesis Problem with ASP Modulo Difference Constraints
### General Problem Encoding
6 next((X,Y,Z),(X,Y',Z'),(X,Y+1,Z)):-coord(X,Y,Z),coord(X,Y',Z'),
7 coord(X,Y+1,Z),Y<Y'. 8 next((X,Y,Z),(X,Y',Z'),(X,Y-1,Z)):-coord(X,Y,Z),coord(X,Y',Z'),
9 coord(X,Y-1,Z),Y>Y'. 10 next((X,Y,Z),(X,Y,Z'),(X,Y,Z+1)):-coord(X,Y,Z),coord(X,Y,Z'),
11 coord(X,Y,Z+1),Z<Z'. 12 next((X,Y,Z),(X,Y,Z'),(X,Y,Z-1)):-coord(X,Y,Z),coord(X,Y,Z'),
13 coord(X,Y,Z-1),Z>Z. 14 route((T,T'),R,R'):-bind(T,R),bind(T',R'),send(T,T'),
15 location(R,C),location(R',C),R!=R'. 16 route((T,T'),R,R'):-route((T,T'),_,R),bind(T',R'),
17 location(R,C),location(R',C'),
18 next(C,C',C'),location(R',C'),
19 notbind(T',R'). 20 route((T,T'),R',R):-bind(T',R),send(T,T'),
21 location(R,C),location(R',C),R!=R'. ```
Listing 9: Routing encoding via dimension-ordered routing. To determine the quality of an implementation, we first determine which resources are allocated. This can be seen in Listing 10. Here, we derive atoms over predicate allocated/1, where allocated(\(r\)) signifies that resource \(r\) is used in an implementation. A resource is considered to be in use if a task is bound to it (Line 1) or if it is included in the routing of a communication (Lines 3-4). ```
1 allocated(R):-bind(T,R). 3 allocated(R):-route(_,R,_). 4 allocated(R):-route(_,_,R). ```
Listing 10: Deriving allocation of an implementation. The preference encoding in Listing 11 makes use of this information. Preference definitions rely on atoms over predicates preference/2 and preference/5. Additionally, rules are provided that capture atoms that the preferences relate to via atoms over predicate holds/2. These predicates are known to the Pareto optimization system and are used to communicate the desired preferences. All preference definitions follow the same scheme. First, an atom preference(\(n,t\)) declares preference \(n\) of type \(t\); in our case, we have preference(cost,sum), preference(energy,sum), and preference(latency,max). Here, sum and max are predefined preference types that are known to our Pareto preference propagator. The former determines the sum of elements that are added if certain conditions hold, and the latter determines the maximum of integer variables, optionally with a constant offset. For those preference types, smaller values are preferred, and we use Pareto optimization over all preferences. In contrast to _aspirin_, preference types cannot be added via ASP but are Python classes that implement certain methods. This is due to the fact that the values of the start times have to be considered to calculate the latency. These values are only known to the difference constraints propagator and are never reified in the ASP encoding. Then, the preference elements are defined, i.e., what quality holds under what conditions. To determine the cost of an implementation, the rule in Line 2 states that whenever allocated(\(r\)) holds, we add \(c(r)\) to the value of preference cost for \(r\in R\). The energy consumption is split into several preference statements. Line 7 adds \(se(r)\) whenever allocated(\(r\)) holds for \(r\in R\). Line 11 sums up the energy consumption of every link used in a communication by adding \(re\) for every route(\((t,t^{\prime})\),r,r') that holds for \((t,t^{\prime})\in\mathbb{D}\) and \(\{r,r^{\prime}\}\subseteq R\). Finally, Line 15 states that whenever bind(\(t\),\(r\)) holds, we add \(se(r,t)\) for \(r\in R\) and \(t\in\mathbb{T}\). For latency, Line 21 states that whenever bind(\(t\),\(r\)) holds, we add the value \(s(t)+e(r,t)\) to the set over which the maximum is calculated for \(t\in\mathbb{T}\) and \(r\in R\). Essentially, we build the maximum over all starting times plus execution times, which is the latency of the implementation. The third and final part of each preference description refiies atoms that are conditions for the preference elements into holds/1 atoms. Note that this is the only non-domain part of the preference encoding. In detail, holds/1 atoms describe the part of the answer set that determines the quality. For more information on the fact format and methodology see [2; 13].