Kimoto Gravity Well Issue? - algorithm

Can anybody explain what this code actually do and how it is re targeted the difficulty. I want my difficulty to be re targeted every 60 seconds, searching on internet find that this function will re targeted the difficulty.
I am working on Logos LGS coin.
unsigned int static KimotoGravityWell(const CBlockIndex* pindexLast, const CBlockHeader *pblock, uint64 TargetBlocksSpacingSeconds, uint64 PastBlocksMin, uint64 PastBlocksMax) {
/* current difficulty formula - kimoto gravity well */
const CBlockIndex *BlockLastSolved = pindexLast;
const CBlockIndex *BlockReading = pindexLast;
const CBlockHeader *BlockCreating = pblock;
BlockCreating = BlockCreating;
uint64 PastBlocksMass = 0;
int64 PastRateActualSeconds = 0;
int64 PastRateTargetSeconds = 0;
double PastRateAdjustmentRatio = double(1);
CBigNum PastDifficultyAverage;
CBigNum PastDifficultyAveragePrev;
double EventHorizonDeviation;
double EventHorizonDeviationFast;
double EventHorizonDeviationSlow;
if (BlockLastSolved == NULL || BlockLastSolved->nHeight == 0 || (uint64)BlockLastSolved->nHeight < PastBlocksMin) { return bnProofOfWorkLimit.GetCompact(); }
for (unsigned int i = 1; BlockReading && BlockReading->nHeight > 0; i++) {
if (PastBlocksMax > 0 && i > PastBlocksMax) { break; }
PastBlocksMass++;
if (i == 1) { PastDifficultyAverage.SetCompact(BlockReading->nBits); }
else { PastDifficultyAverage = ((CBigNum().SetCompact(BlockReading->nBits) - PastDifficultyAveragePrev) / i) + PastDifficultyAveragePrev; }
PastDifficultyAveragePrev = PastDifficultyAverage;
PastRateActualSeconds = BlockLastSolved->GetBlockTime() - BlockReading->GetBlockTime();
PastRateTargetSeconds = TargetBlocksSpacingSeconds * PastBlocksMass;
PastRateAdjustmentRatio = double(1);
if (PastRateActualSeconds < 0) { PastRateActualSeconds = 0; }
if (PastRateActualSeconds != 0 && PastRateTargetSeconds != 0) {
PastRateAdjustmentRatio = double(PastRateTargetSeconds) / double(PastRateActualSeconds);
}
EventHorizonDeviation = 1 + (0.7084 * pow((double(PastBlocksMass)/double(144)), -1.228));
EventHorizonDeviationFast = EventHorizonDeviation;
EventHorizonDeviationSlow = 1 / EventHorizonDeviation;
if (PastBlocksMass >= PastBlocksMin) {
if ((PastRateAdjustmentRatio <= EventHorizonDeviationSlow) || (PastRateAdjustmentRatio >= EventHorizonDeviationFast)) { assert(BlockReading); break; }
}
if (BlockReading->pprev == NULL) { assert(BlockReading); break; }
BlockReading = BlockReading->pprev;
}
CBigNum bnNew(PastDifficultyAverage);
if (PastRateActualSeconds != 0 && PastRateTargetSeconds != 0) {
bnNew *= PastRateActualSeconds;
bnNew /= PastRateTargetSeconds;
}
if (bnNew > bnProofOfWorkLimit) { bnNew = bnProofOfWorkLimit; }
return bnNew.GetCompact();
}

Related

Updating sand simulation in grid doesn't work

I want to make a falling sand simulation using cellular automata, but when I update it, nothing happens, and when I want to do a line of diffrent material using lineDrawing() this material appear in random cells. This is update code:
void update()
{
for (int i = verticalNumberOfCells - 1; i > 0; i--)
{
for (int j = 0; j < horizontalNumberOfCells; j++)
{
world[j][i].update(false);
}
}
for (int y = verticalNumberOfCells - 1; y > 0; y--)
{
for (int x = 0; x < horizontalNumberOfCells; x++)
{
if (world[x][y].hasMoved) continue;
if (world[x][y].state == 0 && world[x][y].state == 1) continue;
if (canMove(world[x][y].state, x, y + 1))
{
move(x, y, x, y + 1);
}
}
}
}
The auxiliary functions that I use to check if the contents of a cell can change and to change the contents of a cell look like this:
boolean canMove(int state, int positionX, int positionY)
{
if (positionX < 0 || positionX >= horizontalNumberOfCells || positionY < 0 || positionY >= verticalNumberOfCells) return false;
int otherSubstance = world[positionX][positionY].state;
if (state == 5) return (otherSubstance == 4);
if (otherSubstance == 0) return true;
if (state == 2 && otherSubstance == 3 && random(1f) < 0.5f) return true;
return false;
}
void move(int fromX, int fromY, int toX, int toY)
{
Cells otherSubstance = world[toX][toY];
world[toX][toY] = world[fromX][fromY];
world[fromX][fromY] = otherSubstance;
world[fromX][fromY].hasMoved = true;
world[toX][toY].hasMoved = true;
world[fromX][fromY].velocityX = 0;
world[fromX][fromY].velocityY = 0;
if (toX > fromX)
{
world[toX][toY].velocityX = 1;
} else if (toX < fromX)
{
world[toX][toY].velocityX = -1;
} else
{
world[toX][toY].velocityX = 0;
}
if (toY > fromY)
{
world[toX][toY].velocityY = 1;
} else if (toY < fromY)
{
world[toX][toY].velocityY = -1;
} else
{
world[toX][toY].velocityY = 0;
}
}
I was able to fix this problem. The thing was, copying a cell in the move function didn't work. Here is the wrong version of the code:
Cells otherSubstance = world[toX][toY];
world[toX][toY] = world[fromX][fromY];
world[fromX][fromY] = otherSubstance;
and here is right version of the code:
int oldState = world[toX][toY].state;
world[toX][toY].state = world[fromX][fromY].state;
world[fromX][fromY].state = oldState;

How to check and validate Iranian National Code (Melli Code) in Flutter?

How to validate Iranian 10 digits national code in Flutter (Dart)?
It has a specific pattern.
Following method is written in Dart based on this android code and is tested to validate Iranian National Code (Melli-Code) in Flutter:
bool validateNationalCode(String nc) {
if (nc.trim() == '') {
return false;
} else if (nc.length != 10) {
return false;
} else {
int sum = 0;
for (int i = 0; i < 9; i++) {
sum += int.parse(nc[i]) * (10 - i);
}
int lastDigit;
int divideRemaining = sum % 11;
if (divideRemaining < 2) {
lastDigit = divideRemaining;
} else {
lastDigit = 11 - (divideRemaining);
}
if (int.parse(nc[9]) == lastDigit) {
return true;
} else {
return false;
}
}
}

Is the cuda kernel limited by memory usage per thread/block

I have a kernel code that executes properly
runnable code
__global__ static void CalcSTLDistance_Kernel(Integer ComputeParticleNumber)
{
//const Integer TID = CudaGetTargetID();
const Integer ID =CudaGetTargetID();
/*if(ID >= ComputeParticleNumber)
{
return ;
}*/
CDistance NearestDistance;
Integer NearestID = -1;
NearestDistance.Magnitude = 1e8;
NearestDistance.Direction.x = 0;
NearestDistance.Direction.y = 0;
NearestDistance.Direction.z = 0;//make_Scalar3(0,0,0);
//if(c_daOutputParticleID[ID] < -1)
//{
// c_daSTLDistance[ID] = NearestDistance;
// c_daSTLID[ID] = NearestID;
// return;
//}
//Scalar3 TargetPosition = c_daParticlePosition[ID];
Integer TriangleID;
Integer CIDX, CIDY, CIDZ;
Integer CID = GetCellID(&CONSTANT_BOUNDINGBOX,&c_daParticlePosition[ID],CIDX, CIDY, CIDZ);
if(CID >=0 && CID < c_CellNum)
{
//Integer Range = 1;
for(Integer k = -1; k <= 1; ++k)
{
for(Integer j = -1; j <= 1; ++j)
{
for(Integer i = -1; i <= 1; ++i)
{
Integer MCID = GetCellID(&CONSTANT_BOUNDINGBOX,CIDX +i, CIDY + j,CIDZ + k);
if(MCID < 0 || MCID >= c_CellNum)
{
continue;
}
unsigned int TriangleNum = c_daCell[MCID].m_TriangleNum;
for(unsigned int l = 0; l < TriangleNum; ++l)
{
TriangleID = c_daCell[MCID].m_TriangleID[l];
/*if(c_daTrianglesParameters[c_daTriangles[TriangleID].ModelIDNumber].isDrag)
{
continue;
}*/
if( TriangleID >= 0 && TriangleID < c_TriangleNum && TriangleID != NearestID)// No need to calculate again for the same triangle
{
CDistance Distance ;
Distance.Magnitude = CalcDistance(&c_daTriangles[TriangleID], &c_daParticlePosition[ID], &Distance.Direction);
if(Distance.Magnitude < NearestDistance.Magnitude)
{
NearestDistance = Distance;
NearestID = TriangleID;
}
}
}
}
}
}
}
c_daSTLDistance[ID] = NearestDistance;
c_daSTLID[ID] = NearestID;
}
and when I add any basic variables or perform any checking operation, it gives unknown error and while checking wih cuda-memcheck, it suggests memory read error.
here in the changed code, i tried to check the previously calculated part and tried to skip the redundant calculation. for this I tried to perform basic check operation in array but it throws memory error.
error raising code
__global__ static void CalcSTLDistance_Kernel(Integer ComputeParticleNumber)
{
//const Integer TID = CudaGetTargetID();
const Integer ID =CudaGetTargetID();
/*if(ID >= ComputeParticleNumber)
{
return ;
}*/
CDistance NearestDistance;
Integer NearestID = -1;
NearestDistance.Magnitude = 1e8;
NearestDistance.Direction.x = 0;
NearestDistance.Direction.y = 0;
NearestDistance.Direction.z = 0;//make_Scalar3(0,0,0);
//if(c_daOutputParticleID[ID] < -1)
//{
// c_daSTLDistance[ID] = NearestDistance;
// c_daSTLID[ID] = NearestID;
// return;
//}
//Scalar3 TargetPosition = c_daParticlePosition[ID];
Integer TriangleID;
Integer CIDX, CIDY, CIDZ;
Integer CID = GetCellID(&CONSTANT_BOUNDINGBOX,&c_daParticlePosition[ID],CIDX, CIDY, CIDZ);
int len=0;
int td[100];
for(int m=0;m<100;m++)
{
td[m]=-1;
}
if(CID >=0 && CID < c_CellNum)
{
//Integer Range = 1;
for(Integer k = -1; k <= 1; ++k)
{
for(Integer j = -1; j <= 1; ++j)
{
for(Integer i = -1; i <= 1; ++i)
{
Integer MCID = GetCellID(&CONSTANT_BOUNDINGBOX,CIDX +i, CIDY + j,CIDZ + k);
if(MCID < 0 || MCID >= c_CellNum)
{
continue;
}
unsigned int TriangleNum = c_daCell[MCID].m_TriangleNum;
bool flag = false;
//len=len+TriangleNum ;
for(unsigned int l = 0; l < TriangleNum; ++l)
{
TriangleID = c_daCell[MCID].m_TriangleID[l];
//tem[l] = c_daCell[MCID].m_TriangleID[l];
for(int m=0;m<100;m++)
{
if(TriangleID ==td[m])
{
flag= true;
}
if(flag == true)
break;
}
if(flag == true)
continue;
else
{
td[len] = TriangleID;
len= len+1;
if( TriangleID >= 0 && TriangleID < c_TriangleNum && TriangleID != NearestID)// No need to calculate again for the same triangle
{
CDistance Distance ;
Distance.Magnitude = CalcDistance(&c_daTriangles[TriangleID], &c_daParticlePosition[ID], &Distance.Direction);
if(Distance.Magnitude < NearestDistance.Magnitude)
{
NearestDistance = Distance;
NearestID = TriangleID;
}
}
}
}
}
}
}
}
c_daSTLDistance[ID] = NearestDistance;
c_daSTLID[ID] = NearestID;
}
this problem arises whenever I tried to add any piece of code,thus I suspects that this block of kernel is not allowing me to add any further code due to memory over use.
is there any memory violation rule per block or thread??
how to find the total memory usuage per kernel ?? is there any way??

simulating centipede finite state machine

I'm trying to write a centipede game clone, and I have written a node class which is basically a circle.
I'm trying to write an FSM to simulate node movements and bouncing with bricks or the screen, but the problem, that the FSM conflicts with other conditions. How would I resolve it ? For example one of the nodes can be at x <0 but they have y > 800.
public function gameLoop(event:Event):void
{
for (var i:int = 0; i < m_nodes.length; i++)
{
if (m_nodes[i].x > 750)
{
m_nodes[i].current_dir = Node.Direction_RIGHT;
}
else if (m_nodes[i].x < 10)
{
m_nodes[i].current_dir = Node.Direction_UP;
}
if ( m_nodes[i].y < 0 )
{
m_nodes[i].current_dir = Node.Direction_RIGHT;
}
else if ( m_nodes[i].y > 590 )
{
m_nodes[i].current_dir =Node.Direction_LEFT;
}
if (m_nodes[i].hitTestObject(m_bricks[i]))
{
//m_nodes[i].current_dir = Node.Direction_RIGHT;
}
if (m_nodes[i].current_dir == Node.Direction_LEFT)
{
m_nodes[i].vx = -5;
m_nodes[i].vy = 0;
}
if (m_nodes[i].current_dir == Node.Direction_RIGHT)
{
m_nodes[i].vx = 0;
m_nodes[i].vy = 5;
}
if (m_nodes[i].current_dir == Node.Direction_UP)
{
m_nodes[i].vx = 0;
m_nodes[i].vy = -5;
}
m_nodes[i].x += m_nodes[i].vx;
m_nodes[i].y += m_nodes[i].vy;
}
}
Update: new updated code, with fixing the logic, but collision detects fails, the state machines have conflict states.
for (var i:int = 0; i < m_nodes.length; i++)
{
if (m_nodes[i].x > 750)
{
m_nodes[i].current_dir = Node.Direction_DOWN;
}
else if (m_nodes[i].x < 40 && m_nodes[i].y < 600)
{
if (!m_nodes[i].current_dir == Node.Direction_RIGHT)
{
m_nodes[i].current_dir = Node.Direction_UP;
trace("dir up");
}
}
if (m_nodes[i].hitTestObject(m_bricks[i]) )
{
trace("hit");
m_nodes[i].vx = 0;
m_nodes[i].vy = 5;
}
if (m_nodes[i].y < 40 && m_nodes[i].x < 40)
{
m_nodes[i].current_dir = Node.Direction_RIGHT;
}
else if (m_nodes[i].y > 560 && m_nodes[i].x > 40)
{
m_nodes[i].current_dir = Node.Direction_LEFT;
}
if (m_nodes[i].current_dir == Node.Direction_LEFT)
{
m_nodes[i].vx = -5;
m_nodes[i].vy = 0;
}
else if (m_nodes[i].current_dir == Node.Direction_DOWN)
{
m_nodes[i].vx = 0;
m_nodes[i].vy = 5;
}
else if (m_nodes[i].current_dir == Node.Direction_RIGHT)
{
m_nodes[i].vx = 5;
m_nodes[i].vy = 0;
}
if (m_nodes[i].current_dir == Node.Direction_UP)
{
m_nodes[i].vx = 0;
m_nodes[i].vy = -5;
}
m_nodes[i].x += m_nodes[i].vx;
m_nodes[i].y += m_nodes[i].vy;
}

Is there email validator code for Java ME or BlackBerry?

Is there some standard email validator code sample for Java ME or BlackBerry?
public static boolean validateEmailID(String email) {
email = email.trim();
String reverse = new StringBuffer(email).reverse().toString();
if (email == null || email.length() == 0 || email.indexOf("#") == -1) {
return false;
}
int emailLength = email.length();
int atPosition = email.indexOf("#");
int atDot = reverse.indexOf(".");
String beforeAt = email.substring(0, atPosition);
String afterAt = email.substring(atPosition + 1, emailLength);
if (beforeAt.length() == 0 || afterAt.length() == 0) {
return false;
}
for (int i = 0; email.length() - 1 > i; i++) {
char i1 = email.charAt(i);
char i2 = email.charAt(i + 1);
if (i1 == '.' && i2 == '.') {
return false;
}
}
if (email.charAt(atPosition - 1) == '.' || email.charAt(0) == '.' || email.charAt(atPosition + 1) == '.' || afterAt.indexOf("#") != -1 || atDot < 2) {
return false;
}
return true;
}
Use this code for checking the given email id is valid or not,
private static boolean validateEmailID(String email) {
if (email == null || email.length() == 0 || email.indexOf("#") == -1 || email.indexOf(" ") != -1) {
return false;
}
int emailLenght = email.length();
int atPosition = email.indexOf("#");
String beforeAt = email.substring(0, atPosition);
String afterAt = email.substring(atPosition + 1, emailLenght);
if (beforeAt.length() == 0 || afterAt.length() == 0) {
return false;
}
if (email.charAt(atPosition - 1) == '.') {
return false;
}
if (email.charAt(atPosition + 1) == '.') {
return false;
}
if (afterAt.indexOf(".") == -1) {
return false;
}
char dotCh = 0;
for (int i = 0; i < afterAt.length(); i++) {
char ch = afterAt.charAt(i);
if ((ch == 0x2e) && (ch == dotCh)) {
return false;
}
dotCh = ch;
}
if (afterAt.indexOf("#") != -1) {
return false;
}
int ind = 0;
do {
int newInd = afterAt.indexOf(".", ind + 1);
if (newInd == ind || newInd == -1) {
String prefix = afterAt.substring(ind + 1);
if (prefix.length() > 1 && prefix.length() < 6) {
break;
} else {
return false;
}
} else {
ind = newInd;
}
} while (true);
dotCh = 0;
for (int i = 0; i < beforeAt.length(); i++) {
char ch = beforeAt.charAt(i);
if (!((ch >= 0x30 && ch <= 0x39) || (ch >= 0x41 && ch <= 0x5a) || (ch >= 0x61 && ch <= 0x7a)
|| (ch == 0x2e) || (ch == 0x2d) || (ch == 0x5f))) {
return false;
}
if ((ch == 0x2e) && (ch == dotCh)) {
return false;
}
dotCh = ch;
}
return true;
}
You can simply google around for email validation regex pattern. Thats the easiest and efficient way to check email string format. see the link below.
http://www.zparacha.com/ultimate-java-regular-expression-to-validate-email-address/
http://leshazlewood.com/2006/02/04/java-email-address-validation-the-right-way-regular-expression/
You will find many other examples for regex. For Regex, check the following link.
http://www.regular-expressions.info/java.html

Resources