start of new file
[IRC.git] / Robust / src / Benchmarks / TileSearch / Tag / SubProblem.java
1 public class SubProblem {
2     flag findingNewFits;
3     flag scored;
4     flag main;
5     flag leaf;
6
7     public SubProblem(){
8         partial = false;
9     }
10
11     public Tile[]   tilesToFit;
12     public Tile[]   tilesFitted;
13     public TileGrid workingGrid;
14
15     // these indices are into the respective
16     // tile arrays
17     public int indexToFit;
18     public int indexFitted;
19
20     // this score represents the evaluation
21     // of every arrangement of this sub-problem's
22     // bestArrangements list
23     public int highScore;
24
25     public boolean partial;
26
27     public void incrementIndices() {
28         ++indexFitted;
29         if( indexFitted == tilesFitted.length ) {
30             indexFitted = 0;
31             ++indexToFit;
32         }
33     }
34
35     public void initializeSubProblem( SubProblem nsp, int checkingFits ) {
36         nsp.tilesToFit = new Tile[tilesToFit.length - 1];
37         nsp.indexToFit = 0;
38
39         int j = 0;
40         for( int i = 0; i < tilesToFit.length; ++i ) {
41             // copy everything but the tile that
42             // is being moved to the fitted list
43             if( i != indexToFit ) {
44                 nsp.tilesToFit[j] = tilesToFit[i].copy();
45                 ++j;
46             }
47         }
48
49         nsp.tilesFitted = new Tile[tilesFitted.length + 1];
50         nsp.tilesFitted[nsp.tilesFitted.length - 1] = tilesToFit[indexToFit].copy();
51         nsp.indexFitted = 0;
52         for( int i = 0; i < tilesFitted.length; ++i ) {
53             nsp.tilesFitted[i] = tilesFitted[i].copy();
54             //  if((checkingFits == 1) || 
55             //                  (checkingFits == 3)) {
56             nsp.tilesFitted[i].x = tilesFitted[i].x;
57             nsp.tilesFitted[i].y = tilesFitted[i].y;
58             //  }
59         }
60
61         // set fitted tiles position according to fit type
62         if( checkingFits == 1 ) {
63             nsp.tilesFitted[nsp.tilesFitted.length - 1].x = 
64                 tilesFitted[indexFitted].x;
65             nsp.tilesFitted[nsp.tilesFitted.length - 1].y = 
66                 tilesFitted[indexFitted].y - 1;
67         } else if( checkingFits == 2 ) {
68             nsp.tilesFitted[nsp.tilesFitted.length - 1].x = 
69                 tilesFitted[indexFitted].x;
70             nsp.tilesFitted[nsp.tilesFitted.length - 1].y = 
71                 tilesFitted[indexFitted].y + 1;
72         } else if( checkingFits == 3 ) {
73             nsp.tilesFitted[nsp.tilesFitted.length - 1].x = 
74                 tilesFitted[indexFitted].x + 1;
75             nsp.tilesFitted[nsp.tilesFitted.length - 1].y = 
76                 tilesFitted[indexFitted].y;
77         } else { // ( checkingFits == 4 )
78             nsp.tilesFitted[nsp.tilesFitted.length - 1].x = 
79                 tilesFitted[indexFitted].x - 1;
80             nsp.tilesFitted[nsp.tilesFitted.length - 1].y = 
81                 tilesFitted[indexFitted].y;
82         }
83
84         // copy grid and place newly fitted tile in sub-problem's
85         // version of the grid
86         nsp.workingGrid = workingGrid.copy();
87         nsp.workingGrid.grid[nsp.tilesFitted[nsp.tilesFitted.length - 1].x]
88                              [nsp.tilesFitted[nsp.tilesFitted.length - 1].y] =
89                                  nsp.tilesFitted.length - 1;
90
91         nsp.highScore      = highScore;
92
93         /*
94         System.printString( "-----------new sub-problem------------\n" );
95         //System.printString( "raw grid\n" );
96         //nsp.workingGrid.printGridRaw();
97         System.printString( "tiles fitted:\n" );
98         nsp.printTileArray( nsp.tilesFitted );
99         System.printString( "tiles to fit:\n" );
100         nsp.printTileArray( nsp.tilesToFit );
101         //System.printString( "the nice grid:\n" );
102         //nsp.workingGrid.printGrid( nsp.tilesFitted );
103         System.printString( "nsp.indexToFit: " );
104         System.printInt( nsp.indexToFit );
105         System.printString( "\n" );
106         System.printString( "nsp.indexFitted: " );
107         System.printInt( nsp.indexFitted );
108         System.printString( "\n" );
109         System.printString( "-----------end sub-problem------------\n" );
110          */
111     }
112
113     public void scoreWorkingGrid() {
114         highScore = 0;
115         for( int i = 0; i < tilesFitted.length; ++i ) {
116             Tile tileToScore = tilesFitted[i];
117             // add those face values that are not adjacent to other face
118             // N
119             if(this.workingGrid.grid[tileToScore.x][tileToScore.y-1] == -1) {
120                 highScore += tileToScore.n;
121             }
122             // S
123             if(this.workingGrid.grid[tileToScore.x][tileToScore.y+1] == -1) {
124                 highScore += tileToScore.s;
125             }
126             // E
127             if(this.workingGrid.grid[tileToScore.x+1][tileToScore.y] == -1) {
128                 highScore += tileToScore.e;
129             }
130             // W
131             if(this.workingGrid.grid[tileToScore.x-1][tileToScore.y] == -1) {
132                 highScore += tileToScore.w;
133             }
134         }
135     }
136
137     public printTileArray( Tile tiles[] ) {
138         for( int i = 0; i < tiles.length; ++i ) {
139             tiles[i].printRow0();
140             System.printString( "  " );
141         }
142         System.printString( "\n" );
143
144         for( int i = 0; i < tiles.length; ++i ) {
145             tiles[i].printRow1();
146             System.printString( "  " );
147         }
148         System.printString( "\n" );
149
150         for( int i = 0; i < tiles.length; ++i ) {
151             tiles[i].printRow2();
152             System.printString( "  " );
153         }
154         System.printString( "\n" );
155
156         for( int i = 0; i < tiles.length; ++i ) {
157             tiles[i].printRow3();
158             System.printString( "  " );
159         }
160         System.printString( "\n" );
161
162         for( int i = 0; i < tiles.length; ++i ) {
163             tiles[i].printRow4();
164             System.printString( ", " );
165         }
166         System.printString( "\n" );
167     }    
168 }