Commit 4d3f25c4 authored by kazushi.kawamura's avatar kazushi.kawamura

upload python code

parent c19af3eb
TARGET = sim
OBJS = $(CPPS:.cpp=.o)
CPPS = main.cpp io.cpp solver.cpp tools.cpp
CXX = g++
CXXFLAGS = -std=c++11 -O3
all: $(TARGET)
$(TARGET): $(OBJS)
$(CXX) -o $@ $(OBJS)
clean:
rm *.o
rm $(TARGET)
#include "io.hpp"
void read_problem(const char *f_name, short int *W, short int *H, short int *blocks, short int *line_num, short int block_info[][5][3], short int line_info[][2][5]) {
ifstream inputfile(f_name);
if(!inputfile) {
cout << f_name << ": Cannot open" << endl;
exit(1);
}
for(int i = 0; i < MAX_BLOCKS+1; i++) {
for(int j = 0; j < 5; j++) {
for(int k = 0; k < 3; k++) {
block_info[i][j][k] = -1;
}
}
}
for(int i = 0; i < MAX_LINES+1; i++) {
for(int j = 0; j < 2; j++) {
for(int k = 0; k < 5; k++) {
line_info[i][j][k] = -1;
}
}
}
*line_num = 0;
int now_block = 0, count;
short int size_x = -1, size_y = -1, now_x, now_y, num;
while(1) {
string line, tmp;
string::size_type pos;
if(!getline(inputfile, line)) break;
if(line.find("\n") != string::npos || line.find("\r") != string::npos) {
line.replace(line.length()-1, 1, ""); // Remove '\n' or '\r'
}
int len = line.length();
if(len == 0) continue;
if(line.find("SIZE") != string::npos) {
replace(line.begin(), line.end(), 'X', ' ');
istringstream iss(line);
iss >> tmp >> *W >> *H;
continue;
}
else if(line.find("BLOCK_NUM") != string::npos) {
istringstream iss(line);
iss >> tmp >> *blocks;
continue;
}
else if(line.find("BLOCK") != string::npos) {
now_block++;
replace(line.begin(), line.end(), 'X', ' ');
istringstream iss(line);
iss >> tmp >> size_x >> size_y;
now_y = 0;
count = 1;
block_info[now_block][0][0] = size_x;
block_info[now_block][0][1] = size_y;
continue;
}
// Replace ' ' -> ""
pos = 0;
while((pos = line.find(' ', pos)) != string::npos) {
line.replace(pos, 1, "");
}
// Replace '+' -> "-1"
pos = 0;
while((pos = line.find('+', pos)) != string::npos) {
line.replace(pos, 1, "-1");
}
// Replace ',' -> " "
pos = 0;
while((pos = line.find(',', pos)) != string::npos) {
line.replace(pos, 1, " ");
}
istringstream iss(line);
for(now_x = 0; now_x < size_x; now_x++) {
iss >> num;
if(num > 0) {
block_info[now_block][count][0] = now_x;
block_info[now_block][count][1] = now_y;
block_info[now_block][count][2] = num;
if(line_info[num][0][0] == -1) { line_info[num][0][0] = now_block; }
else if(line_info[num][1][0] == -1) { line_info[num][1][0] = now_block; }
else { cout << "Error(RE0): Line#" << num << endl; exit(1); }
if(num > *line_num) { *line_num = num; }
count++;
}
else if(num < 0) {
block_info[now_block][count][0] = now_x;
block_info[now_block][count][1] = now_y;
block_info[now_block][count][2] = 0;
count++;
}
}
now_y++;
}
cout << "W: " << *W << ", H: " << *H << endl;
/// Print block information ///
cout << "== Block information ==" << endl;
cout << "#Blocks = " << *blocks << endl;
for(int i = 1; i <= *blocks; i++) {
short int sx = block_info[i][0][0];
short int sy = block_info[i][0][1];
cout << "Block#" << i << ": (" << sx << ", " << sy << ")" << endl;
if(sx == 1 && sy == 1) { // monomino
short int tx = block_info[i][1][0];
short int ty = block_info[i][1][1];
short int tn = block_info[i][1][2];
cout << "- (" << tx << ", " << ty << ") " << tn << endl;
}
else {
for(int it = 1; it < 5; it++) {
short int tx = block_info[i][it][0];
short int ty = block_info[i][it][1];
short int tn = block_info[i][it][2];
cout << "- (" << tx << ", " << ty << ") " << tn << endl;
}
}
}
/// Print block information ///
}
void extract_line_info(short int line_num, short int blocks, short int block_info[][5][3], short int line_info[][2][5]) {
short int lines_x[4], lines_y[4]; // # of nums in a block for X- & Y- axes
for(int i = 1; i <= blocks; i++) {
short int sx = block_info[i][0][0];
short int sy = block_info[i][0][1];
if(sx == 1 && sy == 1) { // monomino
//short int tx = block_info[i][1][0];
//short int ty = block_info[i][1][1];
short int tn = block_info[i][1][2];
if(tn == 0) continue;
short int n_idx = 0;
while(line_info[tn][n_idx][1] != -1) {
n_idx++;
if(n_idx >= 2) { cout << "Error(EL0): Line#" << tn << endl; exit(1); }
}
line_info[tn][n_idx][LE] = 2;
line_info[tn][n_idx][TO] = 2;
line_info[tn][n_idx][RI] = 2;
line_info[tn][n_idx][BO] = 2;
continue;
}
for(short int p = 0; p < 4; p++) lines_x[p] = 0;
for(short int q = 0; q < 4; q++) lines_y[q] = 0;
for(int it = 1; it < 5; it++) {
short int tx = block_info[i][it][0];
short int ty = block_info[i][it][1];
short int tn = block_info[i][it][2];
if(tn > 0) {
lines_x[tx] += 1;
lines_y[ty] += 1;
}
}
for(int it = 1; it < 5; it++) {
short int tx = block_info[i][it][0];
short int ty = block_info[i][it][1];
short int tn = block_info[i][it][2];
if(tn == 0) continue;
short int n_idx = 0;
while(line_info[tn][n_idx][1] != -1) {
n_idx++;
if(n_idx >= 2) { cout << "Error(EL1): Line#" << tn << endl; exit(1); }
}
short int compe_cost, wall_cost;
// Map to left
compe_cost = 2;
compe_cost += lines_x[tx] - 1;
for(short int p = tx - 1; p >= 0; p--) { compe_cost += lines_x[p] * 2; }
wall_cost = 1;
for(int it2 = 1; it2 < 5; it2++) {
short int cx = block_info[i][it2][0];
short int cy = block_info[i][it2][1];
if(cx == tx && cy == ty) continue;
if(cy == ty && cx < tx) {
if(sx == 2 && sy == 3 && ty == 1) {
wall_cost = 4;
}
else {
wall_cost = 2;
}
}
}
line_info[tn][n_idx][LE] = compe_cost * wall_cost;
// Map to top
compe_cost = 2;
compe_cost += lines_y[ty] - 1;
for(short int q = ty - 1; q >= 0; q--) { compe_cost += lines_y[q] * 2; }
wall_cost = 1;
for(int it2 = 1; it2 < 5; it2++) {
short int cx = block_info[i][it2][0];
short int cy = block_info[i][it2][1];
if(cx == tx && cy == ty) continue;
if(cx == tx && cy < ty) {
if(sx == 3 && sy == 2 && tx == 1) {
wall_cost = 4;
}
else {
wall_cost = 2;
}
}
}
line_info[tn][n_idx][TO] = compe_cost * wall_cost;
// Map to right
compe_cost = 2;
compe_cost += lines_x[tx] - 1;
for(short int p = tx + 1; p < sx; p++) { compe_cost += lines_x[p] * 2; }
wall_cost = 1;
for(int it2 = 1; it2 < 5; it2++) {
short int cx = block_info[i][it2][0];
short int cy = block_info[i][it2][1];
if(cx == tx && cy == ty) continue;
if(cy == ty && cx > tx) {
if(sx == 2 && sy == 3 && ty == 1) {
wall_cost = 4;
}
else {
wall_cost = 2;
}
}
}
line_info[tn][n_idx][RI] = compe_cost * wall_cost;
// Map to bottom
compe_cost = 2;
compe_cost += lines_y[ty] - 1;
for(short int q = ty + 1; q < sy; q++) { compe_cost += lines_y[q] * 2; }
wall_cost = 1;
for(int it2 = 1; it2 < 5; it2++) {
short int cx = block_info[i][it2][0];
short int cy = block_info[i][it2][1];
if(cx == tx && cy == ty) continue;
if(cx == tx && cy > ty) {
if(sx == 3 && sy == 2 && tx == 1) {
wall_cost = 4;
}
else {
wall_cost = 2;
}
}
}
line_info[tn][n_idx][BO] = compe_cost * wall_cost;
}
}
/// Print line information ///
cout << "== Line information ==" << endl;
cout << "#Lines = " << line_num << endl;
for(int l = 1; l <= line_num; l++) {
short int block1 = line_info[l][0][0];
short int block2 = line_info[l][1][0];
cout << "Line#" << l << ": ";
cout << "Block#" << block1 << "(";
for(int i = 1; i < 5; i++) {
cout << (float)line_info[l][0][i] / 2;
if(i != 4) { cout << ", "; }
}
cout << "), ";
cout << "Block#" << block2 << "(";
for(int i = 1; i < 5; i++) {
cout << (float)line_info[l][1][i] / 2;
if(i != 4) { cout << ", "; }
}
cout << ")" << endl;
}
/// Print line information ///
}
#ifndef __IO_HPP__
#define __IO_HPP__
#include <iostream>
#include <sstream> // for istringstream
#include <fstream>
#include <algorithm> // for replace
#include "param.hpp"
using namespace std;
void read_problem(const char *f_name, short int *W, short int *H, short int *blocks, short int *line_num, short int block_info[][5][3], short int line_info[][2][5]);
void extract_line_info(short int line_num, short int blocks, short int block_info[][5][3], short int line_info[][2][5]);
#endif /* __IO_HPP__ */
#include <iostream>
#include <getopt.h>
#include "param.hpp"
#include "io.hpp"
#include "solver.hpp"
#include "tools.hpp"
using namespace std;
void usage() {
cout << "Usage: ./sim.exe [--problem input-file] [--output output-file] [--seed seed-value]" << endl;
exit(-1);
}
int main(int argc, char *argv[]) {
char *p_filename = NULL;
char *out_filename = NULL;
uint32_t seed_v = 214;
// Get options
struct option longopts[] = {
{"problem", required_argument, NULL, 'p'},
{"output", required_argument, NULL, 'o'},
{"seed", required_argument, NULL, 's'},
{0, 0, 0, 0}
};
int opt, optidx;
while((opt = getopt_long(argc, argv, "p:o:s:", longopts, &optidx)) != -1) {
switch(opt) {
case 'p':
p_filename = optarg;
break;
case 'o':
out_filename = optarg;
break;
case 's':
sscanf(optarg, "%u", &seed_v);
break;
default:
usage();
}
}
// Init seed value
uint32_t seed[3];
srand(seed_v);
seed[0] = rand();
seed[1] = rand();
seed[2] = rand();
// W: Max width of a board (specified in a problem file)
// H: Max height of a board (specified in a problem file)
// blocks: # of blocks (specified in a problem file)
short int W, H, blocks, line_num;
// W_ext: Width of a current solution
// H_ext: Height of a current solution
short int W_ext, H_ext;
// block data
short int block_info[MAX_BLOCKS+1][5][3];
// line data
short int line_info[MAX_LINES+1][2][5];
// An opt. result will be stored in the array
short int opt_result[MAX_CELLS];
// Check problem
read_problem(p_filename, &W, &H, &blocks, &line_num, block_info, line_info);
extract_line_info(line_num, blocks, block_info, line_info);
// Solver body
int status = solver(seed, W, H, blocks, line_num, block_info, line_info, &W_ext, &H_ext, opt_result);
// Check answer
if(status == 0) {
cout << "Fail re-routing" << endl;
}
else {
cout << "== Answer ==" << endl;
cout << "SIZE " << W_ext << "X" << H_ext << endl;
pair<short int,short int> block_place_basis[MAX_BLOCKS+1];
for(int i = 1; i <= blocks; i++) {
block_place_basis[i] = make_pair(block_info[i][0][0], block_info[i][0][1]);
}
show_result(line_num, blocks, W_ext, H_ext, opt_result, block_place_basis);
}
if(W_ext > W || H_ext > H) {
cout << "Fail satisfying constraint T_T" << endl;
}
else {
cout << "Satisfy constraint ^_^" << endl;
if(out_filename == NULL) return 0;
cout << "Output to " << out_filename << endl;
pair<short int,short int> block_place_basis[MAX_BLOCKS+1];
for(int i = 1; i <= blocks; i++) {
block_place_basis[i] = make_pair(block_info[i][0][0], block_info[i][0][1]);
}
output_to_file(out_filename, line_num, blocks, W_ext, H_ext, opt_result, block_place_basis);
}
return 0;
}
import numpy as np
from argparse import ArgumentParser
import mySolverModule
def commandParsing():
parser = ArgumentParser()
parser.add_argument('-p', '--problem', help='Problem file name', required=True) # Required
parser.add_argument('-s', '--seed', help='seed value') # Option
parser.add_argument('-o', '--output', help='Output file name') # Option
args = vars(parser.parse_args())
return args
def main(args):
seed_v = 214
if args['seed'] is not None: seed_v = int(args['seed'])
seed = mySolverModule.seed_generator(seed_v) # ndarray
info = mySolverModule.check_problem(args['problem']) # extract info (block, line, ...)
ret = mySolverModule.solver(seed, info['W'][0], info['H'][0], info['blocks'][0], info['line_num'][0], info['block_info'], info['line_info'])
if ret['status'][0] == 0: print('Fail re-routing')
else: mySolverModule.show_result(ret['W_ext'][0], ret['H_ext'][0], info['blocks'][0], info['line_num'][0], ret['block_info'], ret['opt_result'])
if ret['W_ext'][0] > info['W'][0] or ret['H_ext'][0] > info['H'][0]:
print('Fail satisfying constraint T_T')
else:
print('Satisfy constraint ^_^')
if args['output'] is not None: mySolverModule.output_to_file(args['output'], ret['W_ext'][0], ret['H_ext'][0], info['blocks'][0], info['line_num'][0], ret['block_info'], ret['opt_result'])
if __name__ == '__main__':
args = commandParsing()
main(args)
#ifndef __PARAM_HPP__
#define __PARAM_HPP__
#define MAX_BLOCKS 128
#define MAX_LINES 256
#define MAX_PATH 128
#define MAX_CELLS 16384 // 128x128
#define MAX_SIZE 128
#define SLOT_MAX_SIZE 64
#define ROUND_LIMIT 4096 //32768 // Max=65534(=2^16-2)
//#define PRINT_BOARD
//#define PRINT_SEARCH // for router debug
#define PQ_BIT 16
#define MAX_PQ 65536
#define PRIO_BIT 16
#define DATA_BIT 16
#define ELEM_BIT 32 // ELEM_BIT = PRIO_BIT + DATA_BIT
#define DATA_MASK 65535 // 0000 FFFF
#define DATA_MAX 65535 // FFFF
#define PRIO_MAX 65535 // FFFF
#define LE 1
#define TO 2
#define RI 3
#define BO 4
#define SA_O 1000
#define SA_I 100000
#define TEMP_S 500
#define TEMP_E 0.1
#define INTER_BLOCK_MARGIN 2
#define LOOP 50
#define TRY_LIMIT 500
#define NO_MOVE 10
#define LFSR_RAND_MAX 4294967295
#endif /* __PARAM_HPP__ */
This diff is collapsed.
SIZE 8X10
1, 1, 1, 2, 2, 2, 2, 0
0, 0, 4, 0, 0, 0, 2, 2
8, 8, 4, 4, 4, 0, 4, 2
7, 8, 0, 0, 4, 4, 4, 2
7, 6, 0, 0, 0, 0, 2, 2
0, 6, 6, 6, 5, 11, 0, 0
10, 10, 10, 6, 5, 11, 0, 0
0, 9, 5, 5, 5, 0, 0, 3
3, 9, 3, 3, 3, 3, 3, 3
3, 3, 3, 0, 0, 0, 0, 0
BLOCK#1 @(0,0)
BLOCK#2 @(0,3)
BLOCK#3 @(0,6)
BLOCK#4 @(2,0)
BLOCK#5 @(5,6)
BLOCK#6 @(4,4)
BLOCK#7 @(1,6)
BLOCK#8 @(4,1)
SIZE 10X10
BLOCK_NUM 8
BLOCK#1 1X4
1
+
8
7
BLOCK#2 3X2
0,8,0
7,6,+
BLOCK#3 2X3
10,0
+,0
3,9
BLOCK#4 2X2
1,2
4,+
BLOCK#5 3X2
11,+,+
0,0,3
BLOCK#6 3X2
0, +,2
5,11,0
BLOCK#7 3X2
0,10,6
9, 5,0
BLOCK#8 3X2
+,+,0
0,+,4
\ No newline at end of file
from distutils.core import setup, Extension
setup(name = 'mySolverModule', version = '1.0.0', ext_modules = [Extension('mySolverModule', sources=['py_wrapper.cpp','io.cpp','solver.cpp','tools.cpp'], extra_compile_args=['-O3', '-w'])])
This diff is collapsed.
#ifndef __SOLVER_HPP__
#define __SOLVER_HPP__
#include "/usr/include/python3.5m/Python.h"
#include <iostream>
#include <math.h>
#include "param.hpp"
#include "tools.hpp"
using namespace std;
int solver(uint32_t seed[3], short int W, short int H, short int blocks, short int line_num, short int block_info[][5][3], short int line_info[][2][5], short int *pwi, short int *phi, short int opt_result[]);
void lfsr_random_init(uint32_t seed, uint32_t seed_x, uint32_t seed_y);
uint32_t lfsr_random();
uint32_t lfsr_x_random();
uint32_t lfsr_y_random();
void global_placement(short int W, short int H, short int line_num, short int blocks, short int block_info[][5][3], short int line_info[][2][5], pair<short int,short int> block_place_global[]);
float calcPlaceCost(short int line_num, short int line_info[][2][5], pair<short int,short int> block_place_global[]);
bool local_placement_with_routing_1(short int line_num, short int blocks, short int *pwi, short int *phi, short int block_info[][5][3], pair<short int,short int> block_place_global[], short int opt_result[]);
bool local_placement_with_routing_2(short int line_num, short int blocks, short int *pwi, short int *phi, short int block_info[][5][3], short int opt_result[]);
pair<short int,short int> add_pair(pair<short int,short int> x, pair<short int,short int> y);
pair<short int,short int> sub_pair(pair<short int,short int> x, pair<short int,short int> y);
unsigned short int new_weight(unsigned short int x);
int router(short int size_x, short int size_y, short int line_num, short int board_str[MAX_CELLS]);
int search(short int size_x, short int size_y, short int *path_size, unsigned short int path[MAX_PATH], unsigned short int start, unsigned short int goal, unsigned short int w[MAX_CELLS]);
void enqueue(unsigned int pq_nodes[MAX_PQ], unsigned short int priority, unsigned short int data, unsigned short int *pq_len, bool *is_empty);
void dequeue(unsigned int pq_nodes[MAX_PQ], unsigned short int *ret_priority, unsigned short int *ret_data, unsigned short int *pq_len, bool *is_empty);
#endif /* __SOLVER_HPP__ */
#include "tools.hpp"
void output_to_file(char *f_name, short int line_num, short int blocks, short int wi, short int hi, short int board[], pair<short int,short int> block_place[]) {
ofstream outputfile(f_name);
outputfile << "SIZE " << wi << "X" << hi << endl;
for(short int q = 0; q < hi; q++) {
for(short int p = 0; p < wi; p++) {
short int n = board[q*MAX_SIZE+p];
if(n == 0) { outputfile << 0; }
else if(n == -1) { outputfile << 0; }
else{ outputfile << n; }
if(p != wi-1) { outputfile << ", "; }
}
outputfile << endl;
}
outputfile << setfill(' ');
outputfile << dec;
for(int i = 1; i <= blocks; i++) {
outputfile << "BLOCK#" << i << " @(" << block_place[i].first << "," << block_place[i].second << ")" << endl;
}
outputfile.close();
}
void show_result(short int line_num, short int blocks, short int wi, short int hi, short int board[], pair<short int,short int> block_place[]) {
// This shows a number from 1 to 255
for(short int q = 0; q < hi; q++) {
for(short int p = 0; p < wi; p++) {
short int n = board[q*MAX_SIZE+p];
if(n == 0) { cout << "--"; }
else if(n == -1) { cout << "XX"; }
else{ cout << setfill('0') << setw(2) << hex << n; }
if(p != wi-1) { cout << " "; }
}
cout << endl;
}
cout << setfill(' ');
cout << dec;
//for(int i = 1; i <= blocks; i++) {
// cout << "BLOCK#" << i << " @(" << block_place[i].first << "," << block_place[i].second << ")" << endl;
//}
}
#ifndef __TOOLS_HPP__
#define __TOOLS_HPP__
using namespace std;
#include <iostream>
#include <iomanip> // for setw
#include <fstream>
#include "param.hpp"
void output_to_file(char *f_name, short int line_num, short int blocks, short int wi, short int hi, short int board[], pair<short int,short int> block_place[]);
void show_result(short int line_num, short int blocks, short int wi, short int hi, short int board[], pair<short int,short int> block_place[]);
#endif /* __TOOLS_HPP__ */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment