I made a little bit of it myself, so make a note
It was used when running a genetic algorithm. I feel better. .. .. As a bonus, I made a simple decimal-binary conversion and Gray code conversion. It's easy to use bitstring, but with the meaning of practice.
First of all, C code (part). The indentation is broken, but I wrote the same process many times, but the naming convention is random, but as a starting point. It's a little long, but it's the same.
By the way, I passed the List from the Python side and received it on the C side. Program is difficult ...
Reference (mainly API related) http://stackoverflow.com/questions/5079570/writing-a-python-c-extension-how-to-correctly-load-a-pylistobject
http://rokujyouhitoma.hatenablog.com/entry/20100704/1278216557
http://www.hexacosa.net/documents/python-extending/
Reference (bit operation, etc.) http://saeki-ce.xsrv.jp/index.html
Other wiki etc.
cbinary.c
#include <Python.h>
#define LOWER_BOUND 0
#define UPPER_BOUND 1000
#define MAX_BIT_LENGTH 10
int binaryToValue(int *);
static PyObject *
valueToGray(PyObject *self, PyObject *args)
{
//return gray code
int i, n, m, len;
unsigned int num;
int b[MAX_BIT_LENGTH];
PyListObject *binary;
binary = (PyListObject *) PyList_New(MAX_BIT_LENGTH);
if (!PyArg_ParseTuple(args, "i", &num)){
return NULL;
}
n = (num >> 1) ^ num; //Convert to gray code
//Convert to binary
for (i=0; n>0; i++){
m=n%2; //Divided by 2
n=n/2; //Divide by 2
b[i] = m;
}
len = i; //Length when converted to binary number of integer
//Add 0 to Niketsu so that it becomes 10bit
for (i=len; i<MAX_BIT_LENGTH; i++ ){
b[i] = 0;
}
//Copy the bit string that was in reverse order in binary order
n = 0;
for (i=MAX_BIT_LENGTH-1; i>=0; i--){
//Put an int type value in gray one bit at a time
PyList_SET_ITEM(binary, n++, Py_BuildValue("i", b[i]));
}
return Py_BuildValue("O", binary);
}
static PyObject *
make_individual(PyObject *self, PyObject *args)
{
int b[MAX_BIT_LENGTH];
int m,n,i,len;
unsigned int min, max;
unsigned int random_int;
unsigned int gray_int;
PyListObject *gray; //Representing a python list object
gray = (PyListObject *) PyList_New(MAX_BIT_LENGTH); //Size is MAX~Make a list of
if (!PyArg_ParseTuple(args, "ii", &min, &max)){ //When there are multiple arguments, write them side by side like ii
return NULL;
}
if (min < LOWER_BOUND){
exit(0);
}
else if (max > UPPER_BOUND){
exit(0);
}
//Randomly generate an integer
random_int = min + (int)(rand()*(max-min+1.0)/(1.0+RAND_MAX));
gray_int = (random_int >> 1) ^ random_int; //Convert to gray code
n = gray_int;
//Convert to binary
for (i=0; n>0; i++){
m=n%2; //Divided by 2
n=n/2; //Divide by 2
b[i] = m;
}
len = i; //Length when converted to binary number of integer
//Add 0 to Niketsu so that it becomes 10bit
for (i=len; i<MAX_BIT_LENGTH; i++ ){
b[i] = 0;
}
//Copy the bit string that was in reverse order in binary order
n = 0;
for (i=MAX_BIT_LENGTH-1; i>=0; i--){
//Put an int type value in gray one bit at a time
PyList_SET_ITEM(gray, n++, Py_BuildValue("i", b[i]));
}
return Py_BuildValue("O", gray);
}
static PyObject *
grayToBinary(PyObject *self, PyObject *args)
{
unsigned int num;
unsigned int mask;
int m,n,i,len;
int b[MAX_BIT_LENGTH], inputed_binary[MAX_BIT_LENGTH];
PyListObject *binary; //Representing a python list object
PyObject *get_list;
binary = (PyListObject *) PyList_New(MAX_BIT_LENGTH);
if (!PyArg_ParseTuple(args, "O", &get_list )){
return NULL;
}
if PyList_Check(get_list) {
for (i=0; i<PyList_Size(get_list); i++){
//Extract the contents of the list object while converting it so that it can be seen in C?(No confidence)
inputed_binary[i] = PyInt_AsSsize_t(PyList_GetItem(get_list, (Py_ssize_t)i)); //ok
}
}
num = binaryToValue(inputed_binary);
for (mask = num >> 1; mask != 0; mask = mask >> 1){
//Undo from gray code
num = num ^ mask;
}
n = num;
//Convert to binary
for (i=0; n>0; i++){
m=n%2; //Divided by 2
n=n/2; //Divide by 2
b[i] = m;
}
len = i; //Length when converted to binary number of integer
//Add 0 to Niketsu so that it becomes 10bit
for (i=len; i<MAX_BIT_LENGTH; i++ ){
b[i] = 0;
}
//Copy the bit string that was in reverse order in binary order
n = 0;
for (i=MAX_BIT_LENGTH-1; i>=0; i--){
//Put an int type value in binary one bit at a time
PyList_SET_ITEM(binary, n++, Py_BuildValue("i", b[i]));
}
return Py_BuildValue("O", binary);
}
int binaryToValue(int *b){
//Convert a binary number to an integer
int i,n;
i=0; n=0;
while(i < MAX_BIT_LENGTH){
if (b[i] == 1) n+=1;
i+=1;
if (i == MAX_BIT_LENGTH) break;
n=n*2;
//printf("%d\n", n);
}
return n;
}
static PyObject *
binaryToPtype(PyObject *self, PyObject *args)
{
int i,n;
int inputed_binary[MAX_BIT_LENGTH];
PyListObject *binary; //Representing a python list object
PyObject *get_list;
if (!PyArg_ParseTuple(args, "O", &get_list )){ //Pass the python object as is
return NULL;
}
if PyList_Check(get_list) {
for (i=0; i<PyList_Size(get_list); i++){
inputed_binary[i] = PyInt_AsSsize_t(PyList_GetItem(get_list, (Py_ssize_t)i)); //ok
}
}
i=0; n=0;
while(i < MAX_BIT_LENGTH){
if (inputed_binary[i] == 1) n+=1;
i+=1;
if (i == MAX_BIT_LENGTH) break;
n=n*2;
//printf("%d\n", n);
}
return Py_BuildValue("i", n);
}
static PyObject *
hello(PyObject *self)
{
printf("Hello World!!\n");
Py_RETURN_NONE;
}
static char ext_doc[] = "C extention module example\n";
static PyMethodDef methods[] = {
{"hello", (PyCFunction)hello, METH_NOARGS, "print hello world.\n"},
{"value_to_gray", valueToGray, METH_VARARGS, "return gray obj.\n"},
{"make_individual", make_individual, METH_VARARGS, "return gray code.\n"},
{"gray_to_binary", grayToBinary, METH_VARARGS, "return binary code.\n"},
{"binary_to_ptype", binaryToPtype, METH_VARARGS, "return ptype value.\n"},
{NULL, NULL, 0, NULL}
};
void initcbinarymethods(void)
{
Py_InitModule3("cbinarymethods", methods, ext_doc);
}
As usual? Write setup.py.
setup.py
from distutils.core import setup, Extension
module1 = Extension('cbinarymethods',
sources = ['cbinary.c'])
setup (name = 'cbinarymethods',
version = '1.0',
description = 'This is a demo package',
ext_modules = [module1])
Again, build on the command line as usual.
$ python setup.py build_ext --inplace #--Add in place to make it current.It seems that so can be done
If you can build successfully, you have cbinarymethods.so.
Let's import it with python and use it
>>> import cbinarymethods as cbm
>>> i = cbm.make_individual(200, 250)
>>> i
[0, 0, 1, 0, 0, 0, 1, 0, 1, 1]
>>> a = cbm.gray_to_binary([0,0,1,0,1,0,0,1,0,1])
>>> a
[0, 0, 1, 1, 0, 0, 0, 1, 1, 0]
Recommended Posts