How to store all possible word derivations in a database efficiently? - database

I have this code which takes short words (4 letter words in this case) and breaks them into two (or optionally 3) pieces, and then generates words from those parts.
const words = load()
const derivations = words.map(w => analyze(words, w))
console.log(derivations
.filter(({ list }) => list.length)
.map(({ word, list }) => `${word} => ${list.map(x => `\n ${x.join(':')}`).join('')}`).join('\n')
)
function analyze(words, word, listSize = 2) {
const derivations = []
const permutations = permute([], [], word)
permutations.forEach(list => {
if (list.length !== listSize) return
resolve(words, list[0], list.slice(1)).forEach(derivation => {
if (derivation.length) {
derivations.push(derivation)
}
})
})
return { word, list: derivations }
}
function resolve(words, chunk, remainingList, isFirst = true, result = []) {
words.forEach(word => {
if (!word.match(/^[aeiou]/) && word.length === 4 && (word.startsWith(chunk)) && word !== chunk && word !== [chunk].concat(remainingList).join('')) {
if (remainingList.length) {
resolve(words, remainingList[0], remainingList.slice(1), false).forEach(array => {
if (word !== array[0]) {
result.push([word].concat(array))
}
})
} else {
result.push([ word ])
}
}
})
return result
}
// https://stackoverflow.com/questions/34468474/make-all-possible-combos-in-a-string-of-numbers-with-split-in-javascript
function permute(result, left, right) {
result.push(left.concat(right));
if (right.length > 1) {
for(var i = 1; i < right.length; i++) {
permute(result, left.concat(right.substring(0, i)), right.substring(i));
}
}
return result;
}
function load() {
return `arch
back
bait
bake
ball
band
bank
bark
base
bash
bead
bear
beat
bell
belt
bend
best
bike
bill
bind
bird
bite
blob
blot
blow
blue
blur
boat
bolt
bond
book
boom
boot
boss
bowl
brew
brim
buck
buff
bulb
bulk
bull
bunk
burn
bush
bust
buzz
cake
call
calm
cane
card
care
cart
case
cash
cast
cave
char
chat
chew
chow
cite
clam
claw
clue
coal
coat
code
coin
comb
cone
cook
cool
cord
cork
cost
crab
crew
crib
crow
cube
cuff
cull
cure
curl
dare
dart
dash
date
daub
dawn
daze
deck
deed
dent
dice
diff
dish
disk
dive
dock
dole
doll
doom
dose
down
draw
drum
duck
duct
dude
duel
duke
dunk
dusk
dust
ease
etch
face
fade
fake
fare
farm
fast
fate
fawn
fear
feed
feel
file
fill
film
find
fine
fish
fizz
flat
flaw
flex
flow
flux
foam
fold
fool
fork
form
foul
free
fret
fuel
fume
fuse
fuss
fuzz
hack
haft
halt
hand
hare
harm
hash
haul
have
hawk
haze
head
heat
herd
hide
hike
hill
hint
hiss
hive
hold
hole
home
honk
hood
hoof
hook
hoot
horn
hose
host
howl
huff
hunt
hurt
hush
inch
keel
kern
kick
kill
kiln
kink
kiss
kite
knee
knit
knot
lace
lack
lake
land
lash
laze
lead
leaf
leak
lick
like
line
link
lint
list
load
loaf
loan
lock
look
loom
loot
lord
love
lull
lure
make
mark
mash
mask
mass
mate
maze
mean
meet
melt
mesh
mess
milk
mill
mime
mind
mine
mint
miss
mist
moan
mock
mold
molt
moon
moss
move
muse
mush
must
name
need
nerd
nest
nick
nose
note
null
oink
ooze
race
rack
raft
rake
rank
rant
rate
rave
read
reek
reel
rent
rest
ride
riff
rift
rise
risk
roar
robe
rock
role
roll
room
rose
rule
rush
rust
sack
salt
save
scam
scan
scar
seam
seat
seed
seek
self
sell
shed
shim
shoe
show
side
sift
silt
sink
site
size
skew
skid
skim
skin
slab
slam
sled
slot
snow
soak
sole
soot
sort
star
stem
stew
stir
stub
suck
suit
surf
swan
swim
take
talk
task
team
tear
tell
term
test
text
thaw
thud
tick
tide
tilt
time
toke
toll
tone
tool
toot
toss
tote
tour
tree
trek
trim
trot
tube
tuck
tune
turn
twin
vibe
view
void
vote
waft
wait
wake
walk
wall
want
wash
wave
wear
weed
weld
well
will
wind
wink
wish
wolf
word
work
worm
zone
zoom`.split(/\n+/)
}
Its output as you can see is like this for debugging purposes:
bake =>
back:keel
back:kern
bait:keel
bait:kern
ball:keel
ball:kern
band:keel
band:kern
bank:keel
bank:kern
bark:keel
bark:kern
base:keel
base:kern
bash:keel
bash:kern
band =>
bank:dare
bank:dart
bank:dash
bank:date
bank:daub
bank:dawn
bank:daze
bank:deck
bank:deed
You'll notice that sometimes over 20 words on the left appear (nested, like bank 20+ times). There are only 400 some words, but the combinations number over 8000. If I have a list of 10,000 words, and longer words, and variable number of breakdowns instead of just 2 parts, it grows to enormous proportions.
What if I wanted to store every one of those derivations in a relational database? First, would a relational database be effective in this scenario? Two, what is a standard data structure which could efficiently represent this, in terms of size (sometimes my solution runs out of memory), and disk storage size if it were a DB. I can break these conceptually into models, such as the "Word" model and the "WordDerivation" model, etc.. But if they were each tables, there would be potentially tens of millions of rows just for this. How does a robust company implement such a thing like perhaps Google?
Currently I am trying to work on the data model side of an app side project. I want to show a word, and have it list the derivations for the word. A naive solution would be to search a 100,000 word list for potential matches on every request, but that is basically what I am precomputing here and it takes some time. So I am wondering how you might break it down for a database.
I am used to Rails model, but they seem quite heavy in this scenario, I am not sure. I would need Gigabytes to store this it seems like.

Related

how can I make that label go down a line between every string

I have this array of string
I want it inside the label so that every string has line
"method": [
"Bring a large frying pan of salted water to the boil over medium-high heat. Add the asparagus and cook for 2-3 minutes or until bright green and tender crisp. Use tongs to transfer the asparagus to a plate. Cover to keep warm.",
"Add the vinegar to the water in pan. Reduce heat to medium-low. Crack 1 egg into a small bowl. Use a spoon to stir the water to make a whirlpool. Carefully pour the egg into the centre of the whirlpool. Cook for 4 minutes for a soft yolk or until cooked to your liking. Use a slotted spoon to transfer to a plate. Cover with foil to keep warm. Repeat with remaining eggs.",
"Divide the asparagus among serving plates. Top with an egg. Season with salt and pepper. Top with parmesan to serve."
]
I want it inside the label so that every string has line
You can try something like this:
let arrayStr = [ "Bring a large frying pan of salted water to the boil over medium-high heat. Add the asparagus and cook for 2-3 minutes or until bright green and tender crisp. Use tongs to transfer the asparagus to a plate. Cover to keep warm.", "Add the vinegar to the water in pan. Reduce heat to medium-low. Crack 1 egg into a small bowl. Use a spoon to stir the water to make a whirlpool. Carefully pour the egg into the centre of the whirlpool. Cook for 4 minutes for a soft yolk or until cooked to your liking. Use a slotted spoon to transfer to a plate. Cover with foil to keep warm. Repeat with remaining eggs.", "Divide the asparagus among serving plates. Top with an egg. Season with salt and pepper. Top with parmesan to serve." ]
SwiftUI
Text(arrayStr.joined(separator: "\n\n"))
Storyboard
label.text = arrayStr.joined(separator: "\n\n")

What am I doing wrong with this AI?

I am creating a very naive AI (it maybe shouldn't even be called an AI, as it just tests out a lot of possibilites and picks the best one for him), for a board game I am making. This is to simplify the amount of manual tests I will need to do to balance the game.
The AI is playing alone, doing the following things: in each turn, the AI, playing with one of the heroes, attacks one of the max 9 monsters on the battlefield. His goal is to finish the battle as fast as possible (in the least amount of turns) and with the fewest amount of monster activations.
To achieve this, I've implemented a think ahead algorithm for the AI, where instead of performing the best possible move at the moment, he selects a move, based on the possible outcome of future moves of other heroes. This is the code snippet where he does this, it is written in PHP:
/** Perform think ahead moves
*
* #params int $thinkAheadLeft (the number of think ahead moves left)
* #params int $innerIterator (the iterator for the move)
* #params array $performedMoves (the moves performed so far)
* #param Battlefield $originalBattlefield (the previous state of the Battlefield)
*/
public function performThinkAheadMoves($thinkAheadLeft, $innerIterator, $performedMoves, $originalBattlefield, $tabs) {
if ($thinkAheadLeft == 0) return $this->quantify($originalBattlefield);
$nextThinkAhead = $thinkAheadLeft-1;
$moves = $this->getPossibleHeroMoves($innerIterator, $performedMoves);
$Hero = $this->getHero($innerIterator);
$innerIterator++;
$nextInnerIterator = $innerIterator;
foreach ($moves as $moveid => $move) {
$performedUpFar = $performedMoves;
$performedUpFar[] = $move;
$attack = $Hero->getAttack($move['attackid']);
$monsters = array();
foreach ($move['targets'] as $monsterid) $monsters[] = $originalBattlefield->getMonster($monsterid)->getName();
if (self::$debug) echo $tabs . "Testing sub move of " . $Hero->Name. ": $moveid of " . count($moves) . " (Think Ahead: $thinkAheadLeft | InnerIterator: $innerIterator)\n";
$moves[$moveid]['battlefield']['after']->performMove($move);
if (!$moves[$moveid]['battlefield']['after']->isBattleFinished()) {
if ($innerIterator == count($this->Heroes)) {
$moves[$moveid]['battlefield']['after']->performCleanup();
$nextInnerIterator = 0;
}
$moves[$moveid]['quantify'] = $moves[$moveid]['battlefield']['after']->performThinkAheadMoves($nextThinkAhead, $nextInnerIterator, $performedUpFar, $originalBattlefield, $tabs."\t", $numberOfCombinations);
} else $moves[$moveid]['quantify'] = $moves[$moveid]['battlefield']['after']->quantify($originalBattlefield);
}
usort($moves, function($a, $b) {
if ($a['quantify'] === $b['quantify']) return 0;
else return ($a['quantify'] > $b['quantify']) ? -1 : 1;
});
return $moves[0]['quantify'];
}
What this does is that it recursively checks future moves, until the $thinkAheadleft value is reached, OR until a solution was found (ie, all monsters were defeated). When it reaches it's exit parameter, it calculates the state of the battlefield, compared to the $originalBattlefield (the battlefield state before the first move). The calculation is made in the following way:
/** Quantify the current state of the battlefield
*
* #param Battlefield $originalBattlefield (the original battlefield)
*
* returns int (returns an integer with the battlefield quantification)
*/
public function quantify(Battlefield $originalBattlefield) {
$points = 0;
foreach ($originalBattlefield->Monsters as $originalMonsterId => $OriginalMonster) {
$CurrentMonster = $this->getMonster($originalMonsterId);
$monsterActivated = $CurrentMonster->getActivations() - $OriginalMonster->getActivations();
$points+=$monsterActivated*($this->quantifications['activations'] + $this->quantifications['activationsPenalty']);
if ($CurrentMonster->isDead()) $points+=$this->quantifications['monsterKilled']*$CurrentMonster->Priority;
else {
$enragePenalty = floor($this->quantifications['activations'] * (($CurrentMonster->Enrage['max'] - $CurrentMonster->Enrage['left'])/$CurrentMonster->Enrage['max']));
$points+=($OriginalMonster->Health['left'] - $CurrentMonster->Health['left']) * $this->quantifications['health'];
$points+=(($CurrentMonster->Enrage['max'] - $CurrentMonster->Enrage['left']))*$enragePenalty;
}
}
return $points;
}
When quantifying some things net positive points, some net negative points to the state. What the AI is doing, is, that instead of using the points calculated after his current move to decide which move to take, he uses the points calculated after the think ahead portion, and selecting a move based on the possible moves of the other heroes.
Basically, what the AI is doing, is saying that it isn't the best option at the moment, to attack Monster 1, but IF the other heroes will do this-and-this actions, in the long run, this will be the best outcome.
After selecting a move, the AI performs a single move with the hero, and then repeats the process for the next hero, calculating with +1 moves.
ISSUE: My issue is, that I was presuming, that an AI, that 'thinks ahead' 3-4 moves, should find a better solution than an AI that only performs the best possible move at the moment. But my test cases show differently, in some cases, an AI, that is not using the think ahead option, ie only plays the best possible move at the moment, beats an AI that is thinking ahead 1 single move. Sometimes, the AI that thinks ahead only 3 moves, beats an AI that thinks ahead 4 or 5 moves. Why is this happening? Is my presumption incorrect? If so, why is that? Am I using wrong numbers for weights? I was investigating this, and run a test, to automatically calculate the weights to use, with testing an interval of possible weights, and trying to use the best outcome (ie, the ones, which yield the least number of turns and/or the least number of activations), yet the problem I've described above, still persists with those weights also.
I am limited to a 5 move think ahead with the current version of my script, as with any larger think ahead number, the script gets REALLY slow (with 5 think ahead, it finds a solution in roughly 4 minutes, but with 6 think ahead, it didn't even find the first possible move in 6 hours)
HOW THE FIGHT WORKS: The fight works in the following way: a number of heroes (2-4) controlled by the AI, each having a number of different attacks (1-x), which can be used once or multiple times in a combat, are attacking a number of monsters (1-9). Based on the values of the attack, the monsters lose health, until they die. After each attack, the attacked monster gets enraged if he didn't die, and after each heroes performed a move, all monsters get enraged. When the monsters reach their enrage limit, they activate.
DISCLAIMER: I know that PHP is not the language to use for this kind of operation, but as this is only an in-house project, I've preferred to sacrifice speed, to be able to code this as fast as possible, in my native programming language.
UPDATE: The quantifications that we currently use look something like this:
$Battlefield->setQuantification(array(
'health' => 16,
'monsterKilled' => 86,
'activations' => -46,
'activationsPenalty' => -10
));
If there is randomness in your game, then anything can happen. Pointing that out since it's just not clear from the materials you have posted here.
If there is no randomness and the actors can see the full state of the game, then a longer look-ahead absolutely should perform better. When it does not, it is a clear indication that your evaluation function is providing incorrect estimates of the value of a state.
In looking at your code, the values of your quantifications are not listed and in your simulation it looks like you just have the same player make moves repeatedly without considering the possible actions of the other actors. You need to run a full simulation, step by step in order to produce accurate future states and you need to look at the value estimates of the varying states to see if you agree with them, and make adjustments to your quantifications accordingly.
An alternative way to frame the problem of estimating value is to explicitly predict your chances of winning the round as a percentage on a scale of 0.0 to 1.0 and then choose the move that gives you the highest chance of winning. Calculating the damage done and number of monsters killed so far doesn't tell you much about how much you have left to do in order to win the game.

JSONP Extjs4 issues

I am trying to to load a JSON store with data that I am retrieving from a search server (Solr).
In Firebug, I can see the response (Solr returns data) but I cannot get the store to be loaded with the incoming data.
Notice that I am working locally (solr's address: localhost:8080/blah/blah and my app's address: localhost:8500/blah/blah).
My store code:
Ext.define('Ledger.store.Searchstore', {
extend: 'Ext.data.Store',
requires: ['Ledger.model.Searchmodel'],
model: 'Ledger.model.Searchmodel',
fields: [
{name:'id', mapping:'id'},
{name: 'model', mapping: 'title'},
{name: 'firstname', mapping: 'firstname'},
{name: 'lastname', mapping: 'lastname'},
{name: 'title', mapping: 'title'},
{name: 'biog', mapping: 'biog'}
],
autoLoad: true,
proxy: {
type:'jsonp',
url: 'http://127.0.0.1:8080/solr/select/?q=*:*&fq=model:book&wt=json',
//noCache: false,
//actionMethods:{create: "POST", read: "POST", update: "POST", destroy: "POST"},
//callbackKey: 'myCallback',
reader:{
type: 'json',
root: 'response.docs'
}
}
});
My model code:
Ext.define('Ledger.model.Searchmodel',{
extend:'Ext.data.Model',
fields:[
{name:'id', mapping:'id'},
{name: 'model', mapping: 'title'},
{name: 'firstname', mapping: 'firstname'},
{name: 'lastname', mapping: 'lastname'},
{name: 'title', mapping: 'title'},
{name: 'biog', mapping: 'biog'}
]
});
The incoming JSON from search server (it is valid):
{
"responseHeader":{
"status":0,
"QTime":1,
"params":{
"indent":"on",
"q":"*:*",
"wt":"json",
"fq":"model:book"}},
"response":{"numFound":8,"start":0,"docs":[
{
"id":"1",
"last_modified":"2012-05-12 15:33:07.0",
"model":"book",
"title":"The Art of Fielding: A Novel",
"fake_id":"b_1",
"biog":"At Westish College, a small school on the shore of Lake Michigan, baseball star Henry Skrimshander seems destined for big league stardom. But when a routine throw goes disastrously off course, the fates of five people are upended.\nHenry's fight against self-doubt threatens to ruin his future. College president Guert Affenlight, a longtime bachelor, has fallen unexpectedly and helplessly in love. Owen Dunne, Henry's gay roommate and teammate, becomes caught up in a dangerous affair. Mike Schwartz, the Harpooners' team captain and Henry's best friend, realizes he has guided Henry's career at the expense of his own. And Pella Affenlight, Guert's daughter, returns to Westish after escaping an ill-fated marriage, determined to start a new life.\nAs the season counts down to its climactic final game, these five are forced to confront their deepest hopes, anxieties, and secrets. In the process they forge new bonds, and help one another find their true paths. Written with boundless intelligence and filled with the tenderness of youth, The Art of Fielding is an expansive, warmhearted novel about ambition and its limits, about family and friendship and love, and about commitment--to oneself and to others.\t\t\t\t\t"},
{
"id":"2",
"last_modified":"2012-05-12 15:33:55.0",
"model":"book",
"title":"The Tiger's Wife",
"fake_id":"b_2",
"biog":"Starred Review. The sometimes crushing power of myth, story, and memory is explored in the brilliant debut of Obreht, the youngest of the New Yorker's 20-under-40. Natalia Stefanovi, a doctor living (and, in between suspensions, practicing) in an unnamed country that's a ringer for Obreht's native Croatia, crosses the border in search of answers about the death of her beloved grandfather, who raised her on tales from the village he grew up in, and where, following German bombardment in 1941, a tiger escaped from the zoo in a nearby city and befriended a mysterious deaf-mute woman. The evolving story of the tiger's wife, as the deaf-mute becomes known, forms one of three strands that sustain the novel, the other two being Natalia's efforts to care for orphans and a wayward family who, to lift a curse, are searching for the bones of a long-dead relative; and several of her grandfather's stories about Gavran Gailé, the deathless man, whose appearances coincide with catastrophe and who may hold the key to all the stories that ensnare Natalia. Obreht is an expert at depicting history through aftermath, people through the love they inspire, and place through the stories that endure; the reflected world she creates is both immediately recognizable and a legend in its own right. Obreht is talented far beyond her years, and her unsentimental faith in language, dream, and memory is a pleasure."},
{
"id":"3",
"last_modified":"2012-05-12 15:34:00.0",
"model":"book",
"title":"State of Wonder",
"fake_id":"b_3",
"biog":"In State of Wonder, pharmaceutical researcher Dr. Marina Singh sets off into the Amazon jungle to find the remains and effects of a colleague who recently died under somewhat mysterious circumstances. But first she must locate Dr. Anneck Swenson, a renowned gynecologist who has spent years looking at the reproductive habits of a local tribe where women can conceive well into their middle ages and beyond. Eccentric and notoriously tough, Swenson is paid to find the key to this longstanding childbearing ability by the same company for which Dr. Singh works. Yet that isn’t their only connection: both have an overlapping professional past that Dr. Singh has long tried to forget. In finding her former mentor, Dr. Singh must face her own disappointments and regrets, along with the jungle’s unforgiving humidity and insects, making State of Wonder a multi-layered atmospheric novel that is hard to put down. Indeed, Patchett solidifies her well-deserved place as one of today’s master storytellers. Emotional, vivid, and a work of literature that will surely resonate with readers in the weeks and months to come, State of Wonder truly is a thing of beauty and mystery, much like the Amazon jungle itself."},
{
"id":"4",
"last_modified":"2012-05-12 15:34:05.0",
"model":"book",
"title":"The Marriage Plot",
"fake_id":"b_4",
"biog":"A stunning novel—erudite, compassionate and penetrating in its analysis of love relationships. Eugenides focuses primarily on three characters, who all graduate from Brown in 1982. One of the pieces of this triangle is Madeleine Hanna, who finds herself somewhat embarrassed to have emerged from a “normal” household in New Jersey (though we later find out the normality of her upbringing is only relative). She becomes enamored with Leonard, a brilliant but moody student, in their Semiotics course, one of the texts being, ironically, Roland Barthes’ A Lover’s Discourse, which Madeleine finds disturbingly problematic in helping her figure out her own love relationship. We discover that Leonard had been diagnosed with bipolar disorder during his first year at Brown, and his struggle with mood swings throughout the novel is both titanic and tender. The third major player is Mitchell, a Religious Studies major who is also attracted to Madeleine but whose reticence she finds both disturbing and incomprehensible. On graduation day, Leonard has a breakdown and is hospitalized in a mental-health ward, and Madeleine shows her commitment by skipping the festivities and seeking him out. After graduation, Leonard and Madeleine live together when Leonard gets an internship at a biology lab on Cape Cod, and the spring after graduation they marry, when Leonard is able to get his mood swings under temporary control. Meanwhile Mitchell, who takes his major seriously, travels to India seeking a path—and briefly finds one when he volunteers to work with the dying in Calcutta. But Mitchell’s road to self-discovery eventually returns him to the States—and opens another opportunity for love that complicates Madeleine’s life. "},
{
"id":"5",
"last_modified":"2012-05-12 15:34:07.0",
"model":"book",
"title":"Steve Jobs",
"fake_id":"b_5",
"biog":"Based on more than forty interviews with Jobs conducted over two years—as well as interviews with more than a hundred family members, friends, adversaries, competitors, and colleagues—Walter Isaacson has written a riveting story of the roller-coaster life and searingly intense personality of a creative entrepreneur whose passion for perfection and ferocious drive revolutionized six industries: personal computers, animated movies, music, phones, tablet computing, and digital publishing.\r\nAt a time when America is seeking ways to sustain its innovative edge, and when societies around the world are trying to build digital-age economies, Jobs stands as the ultimate icon of inventiveness and applied imagination. He knew that the best way to create value in the twenty-first century was to connect creativity with technology. He built a company where leaps of the imagination were combined with remarkable feats of engineering.\nAlthough Jobs cooperated with this book, he asked for no control over what was written nor even the right to read it before it was published. He put nothing off-limits. He encouraged the people he knew to speak honestly. And Jobs speaks candidly, sometimes brutally so, about the people he worked with and competed against. His friends, foes, and colleagues provide an unvarnished view of the passions, perfectionism, obsessions, artistry, devilry, and compulsion for control that shaped his approach to business and the innovative products that resulted.\nDriven by demons, Jobs could drive those around him to fury and despair. But his personality and products were interrelated, just as Apple’s hardware and software tended to be, as if part of an integrated system. His tale is instructive and cautionary, filled with lessons about innovation, character, leadership, and values."},
{
"id":"6",
"last_modified":"2012-05-12 15:34:10.0",
"model":"book",
"title":"The Mill River Recluse",
"fake_id":"b_6",
"biog":"Having been loaned a Kindle from a friend, I immediately went in search of books that were inexpensive. I stumbled on The Mill River Recluse and the reviews were what made me really want to read this book. Having never written a review before, when I completed this book I felt I HAD to write a review. I was so wonderfully surprised to the depth of characters, storylines and twists and turns along the way. I stayed up all night just to finish it!! I found myself chuckling at times and caught myself off guard with tears rolling down my face. You became part of Mill River and its community almost from the first page. I really didn't want the story to end.\nIf you have only one chance to read a good Kindle book - this is by far the one! I enjoyed it so much, that when I someday purchase my own Kindle, I will be adding (and most likely reading) this book again....one I DEFINATELY want to add to a 'forever' collection!"},
{
"id":"7",
"last_modified":"2012-05-12 15:34:13.0",
"model":"book",
"title":"Chasing Amanda",
"fake_id":"b_7",
"biog":"In \"Chasing Amanda\" Melissa Foster guides us in helping Molly; wife, mother to a teenage son, search for a missing girl. The young girl has disappeared from their quiet, rural community; a place where things like this simply don't happen. For Molly, it's deja vu. Several years earlier, while living in Philadelphia, she witnessed a similar event. She did nothing at that time, and has been tormented by her inaction ever since.\nMolly's special gift, or curse, is her clairvoyance. She can sometimes 'see' things that others cannot. In Philadelphia, she failed the little girl, whose body was found shortly after. Now, she promised herself that she would not make that mistake again. Molly persists, using her clairvoyance and sheer stubbornness in an effort to find the child. In doing so, Molly exposes her town's shameful secrets, presenting a conclusion to this story, that I never saw coming!\nMelissa Foster's skill with her characters, drew me into the story immediately. The suspense that followed made this book a definite page-turner!"},
{
"id":"13",
"last_modified":"2012-05-12 16:08:48.0",
"model":"book",
"title":"Kodokan Judo",
"fake_id":"b_13",
"biog":"Judo, or the Way of Gentleness, an ideal form of physical exercise and a reliable system, of self-defense, was specially created from traditional Japanese martial arts. This book by the creator of Kodokan judo is uniquely comprehensive and the most authoritative guide to this martial art ever published.\r\nOver a hundred years ago Jigoro Kano mastered swordsmanship and hand-to-hand combat. Failing to discover any underlying principle, he set about designing a new martial art to reflect the concept of maximum efficiency in the use of physical and mental energy. Today, the concepts and techniques of judo taught at the Kodokan are the ones originally devised by their creator and collected together in this book. Covering everything from the fundamental techniques to prearranged formal exercises for both men and women, the book offers detailed explanations of how techniques are combined in two types of practice: randori (free practice) and kata (the practice of forms). In addition to a discussion of traditional methods of resuscitation, the book concludes with a useful appendix of information on the founder and the Kodokan International Judo Center, and a glossary of judo terminology. Fully illustrated throughout, Kodokan Judo will help students and instructors everywhere to discover the principles, techniques, and spirit of this popular martial art."}]
}}
Somewhere I have a button in order to "console.log" the store, but when I call (notice that I have set autoload attribute to true in the store definition) it is empty. When I am loading the above JSON (copy the server's response in a JSON file) via AJAX proxy, the store gets loaded with the data.
What is the problem?
Try this few things.
First one set proxy into model not into store. I had same problem for long time. Then try to reload ur app.
Second set root "docs" directly.
Third set a Ledger.store.Searchstore.load();
At last i used a proxy like this one and i left to my php file how to read remote file:
proxy: {
type: 'ajax',
api: {
read: 'scripts/receive.php',
create: 'scripts/create.php?action=create',
update: 'scripts/create.php?action=update',
destroy:'scripts/destroy.php?action=destroy'
},

Artificial Intelligence for Decision Making [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 2 years ago.
Improve this question
Do you know any example for this topic?.
I have searched google but had no luck with any Decision Making using Artificial Intelligence example ( at least any truly developed)
i have an example for decision making in AI. it is about choosing a dress to purchase. It asks for inputs as follows:
1) Whether you are Male or Female
2)Price range must be less than 1000 or greater than 1000
3)Age <20 or >20
Based on these inputs , my code will analyse which type of dress suits you and in which shop you can purchase it. I have just given few knowledge in my code. Only 3 shop's names i have kept in my knowledge base.
It gives output as a percentage .
Eg) Shop A: 20.5%
Shop B: 40.5&
Shop C: 39.0%
This means that you have high probability of finding your favourite dresses in Shop B.
$ import java.util.*;
import java.lang.*;
import javax.swing.*;
import java.util.Collection.*;
import java.util.Set.*;
public class dress1
{
static int i,j;
static float[] count=new float[3];static int[] count1=new int[3];
static String[] shop=new String[] {"Jean and Tops","Jean and T-shirt","Jean and Shirt/T-shirt","Sherwani","Pyjamas","Formal shirt and Pant","Shorts and shirt","Coat Suit","Sari","Skirt and Tops","Ghagra Choli","Salwar kameez","Churidaar","Pattu Pavadai","Frock"};
static int[] divac= {10,9,8,1,1,2,1,1,7,1,1,1,5,1,1};
static int[] kalac= {1,1,1,2,1,1,1,1,10,1,8,2,2,9,1};
static int[] megac= {7,5,6,3,2,8,2,6,2,4,2,1,2,2,2};
static String[] selected=new String[15];
static boolean value;
public static void main(String arg[])
{
count[0]=0;count[1]=0;count[2]=0;
Set uni = new HashSet();
uni.add("Jean and Tops");
uni.add("Jean and T-shirt");
uni.add("Sari");
uni.add("Skirt and Tops");
uni.add("Ghagra Choli");
uni.add("Salwar kameez");
uni.add("Churidaar");
uni.add("Pattu Pavadai");
uni.add("Sherwani");
uni.add("Frock");
uni.add("Formal shirt and Pant");
uni.add("Jean and Shirt/T-shirt");
uni.add("Shorts and shirt");
uni.add("Coat Suit");
uni.add("Pyjamas");
Set male = new HashSet();
male.add("Sherwani");
male.add("Pyjamas");
male.add("Formal shirt and Pant");
male.add("Jean and Shirt/T-shirt");
male.add("Shorts and shirt");
male.add("Coat Suit");
Set fem = new HashSet();
fem.add("Jean and Tops");
fem.add("Jean and T-shirt");
fem.add("Sari");
fem.add("Skirt and Tops");
fem.add("Ghagra Choli");
fem.add("Salwar kameez");
fem.add("Churidaar");
fem.add("Pattu Pavadai");
fem.add("Frock");
Set l20=new HashSet();
l20.add("Jean and Tops");
l20.add("Jean and T-shirt");
l20.add("Skirt and Tops");
l20.add("Churidaar");
l20.add("Pattu Pavadai");
l20.add("Frock");
Set g20=new HashSet();
g20.add("Jean and T-shirt");
g20.add("Sari");
g20.add("Salwar kameez");
g20.add("Churidaar");
Set ml20=new HashSet();
ml20.add("Pyjamas");
ml20.add("Shorts and shirt");
Set mg20=new HashSet();
mg20.add("Sherwani");
mg20.add("Formal shirt and Pant");
mg20.add("Coat Suit");
mg20.add("Jean and Shirt/T-shirt");
Set mpl1000= new HashSet();
mpl1000.add("Pyjamas");
mpl1000.add("Shorts and shirt");
mpl1000.add("Formal shirt and Pant");
Set mpg1000=new HashSet();
mpg1000.add("Sherwani");
mpg1000.add("Coat Suit");
mpg1000.add("Jean and Shirt/T-shirt");
Set pl1000=new HashSet();
pl1000.add("Frock");
pl1000.add("Skirt and Tops");
pl1000.add("Churidaar");
pl1000.add("Salwar kameez");
Set pg1000=new HashSet();
pg1000.add("Jean and Tops");
pg1000.add("Jean and T-shirt");
pg1000.add("Sari");
pg1000.add("Ghagra Choli");
pg1000.add("Pattu Pavadai");
Scanner input=new Scanner(System.in);
System.out.println("Enter M for Male and F for Female");
String st=input.nextLine();
Set int1 = new HashSet (uni);
if(st.equals("M"))
{
System.out.println("Male");
int1.retainAll(male);
Scanner input1=new Scanner(System.in);
System.out.println("Enter 1 if age <20 or enter 2 if age > 20");
String st1=input1.nextLine();
Set int2= new HashSet (int1);
if(st1.equals("1"))
{
System.out.println("Age Less than 20 Male");
int2.retainAll(ml20);
}
else if(st1.equals("2"))
{
System.out.println("Age Greater than 20 Male");
int2.retainAll(mg20);
}
Scanner input2=new Scanner(System.in);
System.out.println("Enter 1.Price Range < 1000 or 2. Price Range >1000");
String st2=input2.nextLine();
Set int3= new HashSet (int2);
if(st2.equals("1"))
{
System.out.println("Price Less than 1000 ,Male");
int3.retainAll(mpl1000);
}
else if(st2.equals("2"))
{
System.out.println("Price Greater than 1000 ,Male");
int3.retainAll(mpg1000);
}
i=0;
for(Object object : int3)
{
String element = (String) object;
selected[i++]=element;
System.out.println(element);
}
calc();
} /*end of male*/
else if(st.equals("F"))
{
System.out.println("Female");
int1.retainAll(fem);
Scanner input1=new Scanner(System.in);
System.out.println("Enter 1 if age <20 or enter 2 if age > 20");
String st1=input1.nextLine();
Set int2= new HashSet (int1);
if(st1.equals("1"))
{
System.out.println("Age Less than 20 Female");
int2.retainAll(l20);
}
else if(st1.equals("2"))
{
System.out.println("Age Greater than 20 Female");
int2.retainAll(g20);
}
Scanner input2=new Scanner(System.in);
System.out.println("Enter 1.Price Range < 1000 or 2. Price Range >1000");
String st2=input2.nextLine();
Set int3= new HashSet (int2);
if(st2.equals("1"))
{
System.out.println("Price Less than 1000 ,Female");
int3.retainAll(pl1000);
}
else if(st2.equals("2"))
{
System.out.println("Price Greater than 1000 ,Female");
int3.retainAll(pg1000);
}
i=0;
for(Object object : int3)
{
String element = (String) object;
selected[i++]=element;
System.out.println(element);
}
calc();
}/*end of female*/
}/*end of main*/
public static void calc()
{
float finalcount=0;
int k=0;
while(k<i)
{
for(j=0;j<15;j++)
{
value=selected[k].equals(shop[j]);
if(value)
{
break;
}
}/*end of j*/
count[0]=count[0]+divac[j];
count[1]=count[1]+kalac[j];
count[2]=count[2]+megac[j];
k++;
}/*end of while*/
for(int c=0;c<3;c++)
{
finalcount=finalcount+count[c];
}
for(int c=0;c<3;c++)
{
count[c]=((count[c]*100)/finalcount);
}
System.out.println("Availability of dresses available in various shops are:");
System.out.println("Diva is "+count[0]+"%");
System.out.println("Kalanikethan is "+count[1]+"%");
System.out.println("Megamart is "+count[2]+"%");
}
}/*end of class*/
http://msl.cs.uiuc.edu/~lavalle/cs397/
Read this. There are different ways to go about AI decision making. The examples are on the bottom.
Decision-making (DM) is a topic widely studied by different fields of science. (Cognitive sciences, Neurosciences, Computer sciences, Behavioral Economics, Operations research, etc. *1)
However, DM problems are varied and the computational approach to address that problem will vary accordingly. For instance:
If you have to make a frequent decision that affects the previous one you are dealing with a sequential DM problem. In those cases, reinforcement learning *2 or deep reinforcement learning *3 can be used to tackle this problem. Examples of these problems can be seen in video-games where the game AI needs to take different actions (policies) over time to maximise its score. (reward)
If you the problem is not sequential but you deal with multiple criteria to find the most attractive alternative then you are dealing with a multi-criteria decision-making problem, topic widely researched in operations research. There are some typically-used algorithms that are utilised to assist human-decision making like AHP*4, TOPSIS*5, ELECTRE*6, PROMETREE*7. An example of MCDC is selecting a house to buy, where you have to consider location, price among other desirable or undesirable characteristics.
Depending on the level of uncertainty, subjective data and incomplete information of the problem you might require to use fuzzy, intuitionistic or neutrosophic variations of the mentioned algorithms. *8
You might need to optimise DM through different competing goals. In that case, you are dealing with a multi-objective decision-making optimisation problem (MODM). See Decision trees*9, genetic algorithms*10 .
Furthermore, a DM problem can have different 'agents' making decision that can affect ours. So that is known as 'multi-agent' decision-making. In computer science, multi-agent system simulations are commonly used to research these problems. *11
You can also have the case where the agents have to make a collaborative decision that affects all of them. So that is known as 'group' decision-making.
In the industry, computational DM can be seen with the widely used recommender systems such as the ones in Netflix or Amazon.*13 In the B2B sector, AI in DM can be seen in decision-support systems and prescriptive analytics services *14.
I hope you find that information useful. There is indeed, much more about this complex topic, I just tried to summarise.
Some resources you might want to check:
Deep RTS: A playground for reinforcement learning agents in real-time strategy game environments. (Repository: https://github.com/cair/deep-rts) (Pre-print Paper: https://arxiv.org/abs/1808.05032)
OpenAI Gym: A general-purpose playground to test reinforcement learning AI algorithms. (Github: https://github.com/openai/gym, page: https://gym.openai.com/)
DecisionRadar: An online application to apply TOPSIS decision-making algorithm. (Site: https://decision-radar.com/)
AgentSimJS: A 3D multi-agent simulation system built in Javascript. (Repository: https://github.com/maxdeben83/agentsimjs)
REFERENCES:
*1 Atkinson, J. W. (1964). An introduction to motivation.
*1 Berridge, K. C. (2004). Motivation concepts in behavioral neuroscience. Physiology & behavior, 81(2), 179-209.
*1 Hwang, C. L., & Yoon, K. (1981). Methods for multiple attribute decision making. In Multiple attribute decision making (pp. 58-191).Springer, Berlin, Heidelberg.
*1 Tversky, A., & Kahneman, D.(1981). The framing of decisions and the psychology of choice science, 211(4481), 453-458.
*2 Littman, M. L. (1994). Markov games as a framework for multi-agent reinforcement learning. In Machine Learning Proceedings 1994 (pp. 157-163).
*3 Van Hasselt, H., Guez, A., & Silver, D. (2016, February). Deep Reinforcement Learning with Double Q-Learning. In AAAI (Vol. 2, p. 5).
*4 Aczél, J., & Saaty, T. L. (1983). Procedures for synthesizing ratio judgements. Journal of Mathematical Psychology, 27(1),
93–102. doi:10.1016/0022-2496(83)90028-7
*4 Saaty, R. W. (1987). The analytic hierarchy process—what it is and how it is used. Mathematical Modelling, 9(3-5), 167.
doi:10.1016/0270-0255(87)90473-8
*4 Saaty, T. L. (1986). Axiomatic Foundation of the Analytic Hierarchy Process. Management Science, 32(7), 841.
doi:10.1287/mnsc.32.7.841
*4 Hwang, C. L., & Yoon, K. (1981). Methods for multiple attribute decision making. In Multiple attribute decision
making (pp. 58-191). Springer, Berlin, Heidelberg.
*6 Zhou, Y. (1915). Multi-Criteria Decision Making in Software Development: A Systematic Literature Review.
*7 Zhou, Y. (1915). Multi-Criteria Decision Making in Software Development: A Systematic Literature Review.
*8 Pramanik, S., Biswas, P., & Giri, B. C. (2015). Hybrid vector similarity measures and their
applications to multi-attribute decision making under neutrosophic
environment. Neural Computing and Applications, 28(5), 1163
doi:10.1007/s00521-015-2125-3
*8 Mardani, A., Nilashi, M., Zavadskas, E. K., Awang, S. R., Zare, H., & Jamal, N. M. (2018). Decision Making Methods Based on Fuzzy
Aggregation Operators: Three Decades Review from 1986 to 2017.
International Journal of Information Technology & Decision Making,
17(02), 391–466. doi:10.1142/s021962201830001x
*9 Zhao, H. (2007). A multi-objective genetic programming approach to developing Pareto optimal decision trees. Decision Support
Systems, 43(3), 809-826.
*9 Laumanns, M., & Ocenasek, J. (2002, September). Bayesian optimization algorithms for multi-objective optimization. In
International Conference on Parallel Problem Solving from Nature (pp.
298-307). Springer, Berlin, Heidelberg.
*9 Jin, Y. (Ed.). (2006). Multi-objective machine learning (Vol. 16). Springer Science & Business Media.
10 Tamaki, H., Kita, H., & Kobayashi, S. (1996, May).
Multi-objective optimization by genetic algorithms: A review.
In Evolutionary Computation, 1996., Proceedings of IEEE
International Conference on (pp. 517-522). IEEE.
*11 Rodriguez, S., Gaud, N., & Galland, S. (2014, August). SARL: a general-purpose agent-oriented programming language. In
Web Intelligence (WI) and Intelligent Agent Technologies (IAT),
2014 IEEE/WIC/ACM International Joint Conferences on (Vol. 3,
pp. 103-110). IEEE.
*12 Rao, A. S. (1996, January). AgentSpeak (L): BDI agents speak out in a logical computable language. In European Workshop on Modelling Autonomous Agents in a Multi-Agent World (pp. 42-55). Springer, Berlin, Heidelberg.
*13 Ricci, F., Rokach, L., & Shapira, B. (2015). Recommender systems: introduction and challenges. In Recommender systems
handbook (pp. 1-34). Springer, Boston, MA.
*14 https://www.ibm.com/analytics/prescriptive-analytics
There are simply too many examples to count! The whole (large) field of expert systems development, for example, consists of building programs whose explicit purpose is to emulate human decision making. Google "expert systems" to find thousands of examples.
I'll put in a plug for Jess, an expert system shell written in Java which I developed. It's used by thousands of companies world wide to automate decision-making processes.
I did a bit of browsing on the web and I came across this example project.
You might also check out the AI-Depot website here.
Probably the reason there are not so many working examples is because AI decision algorithms, such as neural networks, genetic algorithms, and decision trees get very complex, very quickly. Most developers of such algorithms, at least the algorithms that are stable and actually work, are very protective of their IP. And, for good reason.
In any case, hope this helped.
Let It Be Known

I have a .mp3 file. How can I seperate the human voice from the rest of the sound in C?

Is it even possible in C [I know it is possible in general -GOM player does it]? just let me get started... What do you say?
How exactly do you identify human voice distinguished from other sounds?
Filters in mp3 players usually rely on the fact that the voice source (the performer) in a stereo recording studio is positioned at the center. So they just compute the difference between the channels. If you give them a recording where the performer is not positioned like that they fail - the voice is not extracted.
The reliable way is employing a voice detector. This is a very complex problem that involves hardcore math and thorough tuning of the algorithms for your specific task. if you go this way you start with reading on voice coding (vocoders).
This exact topic was discussed here. It started out as a discussion of audio coding technologies, but on the linked page above someone said
That means no way to extract voice form steoro signal?
But it was pointed out that extracting the voice should be no more difficult than eliminating the voice.
I'll let you read further, but I suspect successful extraction may rely on the relatively narrow spectral distribution of the voice compared to instruments.
Note that it is not possible in principle to perfectly separate different sounds which are mixed together in one track. It's like when you mix cream into your coffee - after it has been mixed in, it isn't possible to perfectly separate the cream and the coffee afterwards.
There might be smart signal processing tricks to get an acceptable result, but in general it's impossible to perfectly separate out the voice from the music.
Seperating the human voice from other sounds is no mean feat. If you have a recording of the other sounds then you can reference cancel the background sound which will leave you with the human voice.
If the background noise is random noise of some sort you will get a win by using some form of spectral filtering. But its not simple and would need a fair bit of playing with to get good results. Adobe Audition has an adaptive spectral filter i believe ...
Assume you have white noise with a fairly even frequency distribution across the entire recorded band (on a 44Khz uncompressed recording you are talking about 0 to 22Khz). Then add a voice on it. Obviously the voice is using the same frequencies as the noise. The human voice ranges from ~300Hz to ~3400Hz. Obviously bandpassing the audio will cut you down to only the voice range of 300 to 3400Hz. Now what? You have a voice AND you have the, now bandpassed, white noise. Somehow you need to be able to remove that noise and leave the voice in tact. There are various filtering schemes but all will damage the voice in the process.
Good luck, its really not gonna be simple!
Look up Independent Component Analysis (ICA)
Where buf has the pcm wav 44100 sample rate input data
int
voiceremoval (char *buf, int bytes, int bps, int nch)
{
short int *samples = (short int *) buf;
int numsamples = 0;
int x = 0;
numsamples = bytes / 2;
x = numsamples;
if (bps == 16)
{
short *a = samples;
if (nch == 2)
while (x--)
{
int l, r;
l = a[1] - a[0];
r = a[0] - a[1];
if (l < -32768)
l = -32768;
if (l > 32767)
l = 32767;
if (r 32767)
r = 32767;
a[0] = -l;
a[1] = r;
a += 2;
}
}
return 0;
}

Resources