A* Heuristic implementation
I am creating a program to solve this puzzle in the lowest cost
The slidingtitle puzzle consists of three black titles, three white titles, and an empty space in the configuration shown in the Figure.
WWW_BBB
The goal of the puzzle is to get all white tiles to the right of the black tiles while the location of the space does not matter
The puzzle has two legal moves(i.e. actions) with associated costs:
• A title may move into an adjacent empty location. – This has a step cost of
• A title can hop over one or two other tiles into the empty position.
– This has a step cost equal to the number of tiles jumped over.
I am having troubles understand how to create a Heuristic algorithm to be implemented in the algorithm.
I understand the implementation of Dijkstra's algorithm within this problem, but can't figure out how to then make it into the A* algorithm.
1 answer

Assuming you want to use A* on the graph of puzzle states with edges to the states reachable through one of the two rules, then a good heuristic to use would be the number of inversions: https://en.wikipedia.org/wiki/Inversion_(discrete_mathematics)
That's the number of W,B pairs that are out of order (assuming that the relative order of samecolored tiles doesn't change). The W's and B's are in order when the number of versions is 0, and the number of inversions fixed by each type of move is less than or equal to its cost. Therefore the number of inversions as a heuristic will never overestimate the cost of the best sequence.
See also questions close to this topic

Optimizing Sieve of Eratosthenes, Adding Wheel to Bool Array
I know there's a few posts about how to implement wheels, but I'm really struggling to see how to implement one with my current approach to the sieve.
I created my own bit array in C, with the following implementation:
#define setBit1(Array, i) (Array[i/INT_BITS] = (1 << i % INT_BITS)) #define getBit(Array, i) ((Array[i/INT_BITS] & (1 << i % INT_BITS)) ? 1 : 0) #define setBit0(Array, i) (Array[i/INT_BITS] &= ~(1 << i % INT_BITS)) int * createBitArray(unsigned long long size) { // find bytes required, round down to nearest whole byte unsigned long long bytesRequired = size / BITS_PERBYTE; // round up to highest multiple of 4 or 8 bytes (one int) bytesRequired = (sizeof(int) * (bytesRequired / sizeof(int) + ((size % BITS_PERBYTE * sizeof(int) == 0) ? 0 : 1))); // allocate array of "bits", round number of ints required up return (int *)malloc((bytesRequired)); }
I've done a few tests in C using the clock(), and I've found that for large array sizes of more than 1,000,000, the bit array, even with its bit manipulations, is at least 200% faster than an int array. It also uses 1/32 of the memory.
#define indexToNum(n) (2*n + 1) #define numToIndex(n) ((n  1) / 2) typedef unsigned long long LONG; // populates prime array through Sieve of Eratosthenes, taking custom // odd keyed bit array, and the raw array length, as arguments void findPrimes(int * primes, LONG arrLength) { long sqrtArrLength = (long)((sqrt((2 * arrLength) + 1)  1) / 2); long maxMult = 0; long integerFromIndex = 0; for (int i = 1; i <= sqrtArrLength; i++) { if (!getBit(primes, i)) { integerFromIndex = indexToNum(i); maxMult = (indexToNum(arrLength)) / integerFromIndex; for (int j = integerFromIndex; j <= maxMult; j+= 2) { setBit1(primes, numToIndex((integerFromIndex*j))); } } } }
I've been populating the bit array with the index, i, representing a number obtained through (2i + 1). This has the benefit of reducing any time spent iterating over even numbers, and once again reducing the necessary memory of the array by half. 2 is manually added to the primes after. This comes with a consequence of the time spent translating between index and number, but with my tests, for more than 1,000 primes, this approach is faster.
I'm stumped on how I could further optimize; I've reduced array size, I'm only testing to sqrt(n), I'm starting the "sieving" of primes upwards from p * p, I've eliminated all evens, and it's still taking me about 60 seconds for the first 100,000,000 primes in C.
As far as I'm aware, the "wheel" method requires that the actual integers of numbers be stored in the index. I'm really stuck on implementing it with my current bit array.

Why does this green screen algorithm result in lost detail?
I decided to work on a green screen algorithm that can effectively isolate fine hair as a personal challenge and my current algorithm does a pretty good job of isolating the subject, however the exposed hair, while isolated fairly well, loses a lot of sharp detail. No matter how I turn the fundamentals of the algorithm over in my head, I can't seem to figure out why exactly the detail loss would occur.
Here are the results:
And my algorithm:
function processImage(canvas, id) { var imageData = recordCanvasData(canvas); var imageDataModifier = Uint8ClampedArray.from(imageData.data); var imageDataResult; var threshold = 255; var lowerThreshold = 0; var spread = threshold  lowerThreshold; var green = 0; var index, index2, r, g, b, a, a2, fraction; for (let x = 0; x < imageData.width; x++) { for (let y = 0; y < imageData.height; y++) { index = (y * imageData.width + x) * 4; r = imageData.data[index]; g = imageData.data[index + 1]; b = imageData.data[index + 2]; a = 0; green = 0; green = r; green += g * 2; green = b; if (green > lowerThreshold) { if (green > threshold) { imageDataModifier[index + 3] = 0; } else { fraction = (green  threshold) / (lowerThreshold  threshold) a = Math.round(fraction * 255); imageDataModifier[index + 1] = g * fraction; imageDataModifier[index + 3] = a; } } } } renderCanvasData(imageDataResult, id); }
I left off the supporting functions because I'm using the ES6
import
syntax which makes the code break without a server due to CORS issues. However, the algorithm in question is all in the code I've shown, so it should be enough. 
How to integrate Bisection Algorithm in Java?
My teacher assigned some practice assignments for us, and one of them involves implementing the Bisection Algorithm in either Java, C++, or Python. I am more familiar with Java so I chose that platform to do it, but I haven't the faintest idea on how to start.
Here's the problem:
Use the bisection algorithm to ﬁnd an approximate solution z to the equation x^5.3 + (3.5)^x = N, where N is your 7digit phone number, and:
(a) z is correct to 2 signiﬁcant ﬁgures. (b) z is correct to 2 decimal places.
I've done simple calculations before and one advance calculation (Euclidean Algorithm) but I haven't the faintest idea on how to start on this one. Any help is appreciated.

partial search in multidimensional array
Hi i have an array like below. I want to search partial data from this array. for example: i want to search "New Delhi" then i got array where city = Delhi, and search "Raigad" then got array where city = Raigarh
Array( [56] => Array ( [city] => Davangere [product_id] => 14 [tier] => Tier 4 ) [57] => Array ( [city] => Dehradun [product_id] => 14 [tier] => Tier 3 ) [58] => Array ( [city] => Delhi [product_id] => 14 [tier] => Metro ) [59] => Array ( [city] => Delhi [product_id] => 14 [tier] => Metro ) [60] => Array ( [city] => Raigarh [product_id] => 14 [tier] => Metro ) )

Facebook Searchview Functionality
I want to implement search view functionality in my app just like the facebook app.Whenever the user will click on search it will go to the search page, and the same filter button will appear like horizontal scroll view.Can you help me with the ideas?? Thanks in advance!!

Bash script for searching for a string in a XML file and rename the file with the string
I have a lot of report files in a folder called XXXXX.xml and i need to search for a string in each one of this file to rename the file with a particular string, for instance:
I have this file called 28022018.xml
<?xml version="1.0" encoding="UTF8"?> <SampleResults XMLCreationDateTime="20180223T10:28:45" XMLVersion="7"> <SampleResult AreReproTestOutliersIgnored="No" ReproTestResult="NotUsed" ReproTestType="None" Instrument="PXC01" MethodName="Fe91" RecalculationDateTime="20180222T12:26:16" BackupStatus="Original" Origin="Measured" CorrType="None" Type="Unknown" OperatorName="" Name="181325"> <SampleIDs> <SampleID Type="Text" KeepLastValue="False" MustExist="False" IsReadOnly="False" IsSampleName="True"> <IDName>Sampe Name</IDName> <IDValue>181325</IDValue> </SampleID> <SampleID Type="GradeName" KeepLastValue="True" MustExist="False" IsReadOnly="True" IsSampleName="False"> <IDName>Grade ID</IDName> <IDValue>1.8161 58CrV4</IDValue> </SampleID> <SampleID Type="Text" KeepLastValue="False" MustExist="False" IsReadOnly="False" IsSampleName="False"> <IDName>New</IDName> <IDValue>Cliente</IDValue> </SampleID> </SampleIDs> </SampleResult> </SampleResults>
I need to create a script that save the string in the properties tag NAME written on 3 line (name ="181325"), and use it to rename the file from 28022018.xml to 181325.xml.
Can someone help me?

Matching similar users with PHP machine learning
I have a social network website written in PHP (i.e. the only language I know).
I have a database with registered user; every user have the following fields:
university1(only one); university2(only one); interest(up to 3 possible);
I want to match users that are similar (i.e. create a collaborative filter that suggest users that are similar) with machine learning.
I have looked up everywhere but I have not found anything that could work for this problem, except something in Python/Java which I don't know.
I know that there is PHPML (and that's what I would like to use) but I don't understand how to use it for this matching problem (KNN? DBSCAN? KMeans?...)

Predicting optimal call time using Azure Machine Learning
As part of a school project, I would like to build a model that could predict the best time for calling some one, i.e the most likely time he/she will pick up.
What I have  2mil+ of call logs, over a 10hr period monfri, split across10 countries with their respective outcome and timestamp.
Thoughts so far  I've never done this before so its a learning experience for me. I was looking into using Azure ML studio. Since I have a huge number of timestamps, I assume I should probably group by day of the week then 30min intervals. Furthermore, I have about 10 call outcomes which I thought of grouping simply into 2  success and fail. I was also thinking of testing various learning models, then compare using 75% for learning and 25% test. End result should be a model that I can run every day with updated data, and it should return a range (possibly multiple ranges?) that is most likely to result in a successful call based on a given day of the week and country combination.
Is this the right approach, can someone point me in the right direction to get started please and perhaps give me additional ideas?
Thanks!

Confusion with Q learning Episode Definition
After reading some tutorials I am still unsure about the definition of any episode. Is episode defined as one walk through from the start state to an exit/goal state?

check if heuristics is compatible
i know that compatible heuristics is the one that under these condition : h(n) <= c(n,a,n') + h(n').
Andthe admissible heuristics is under condition: 0 <= h(n) <= the real cost. However, I don't know how to check if this heuristics is compatible :
An agent lives in an NxN gridworld. The agent's current position is given by the tuple (Xa; Ya) representing the row and column it is currently in. The goal location the agent wants to reach is represented by the tuple (Xg; Yg). The agent can only move up, down, left, or right 1 square at a time.h(a) = (Xa  Xg) + (Ya  Yg)
Can I have some hints to do this? thank you so much. 
Optimal solution with heuristic changed
Say if I have a heuristic h(n) which is consistent (monotone). Then is A* search f(n) = g(n) +3 ∗ h(n) gives the optimal solution?
My understanding is that it will not since 3*h(n) might be bigger than h*(n)