Artificial Intelligence Programs For Programming in Python

Traveling Salesman Problem ( TSP )] Artificial Intelligence Programs For Master Leval Programming in Python from iter tools import permutations def distance(point1, point2): >>> distance([3,4],[0,0]) 5.0 >>> distance([3,6],[10,6]) 7.0 return ((point1[0] - point2[0])**2 + (point1[1] - point2[1])**2) ** 0.5 def total_distance(points): all the points are in the given order. >>> total_distance([[1,2],[4,6]]) 5.0 >>> total_distance([[3,6],[7,6],[12,6]]) 9.0 return sum([distance(point, points[index + 1]) for index, point in enumerate(points[:-1])]) def travelling_salesman(points, start=None): Time complexity is O(N!), so never use on long lists. >>> travelling_salesman([[0,0],[10,0],[6,0]]) ([0, 0], [6, 0], [10, 0]) >>> travelling_salesman([[0,0],[6,0],[2,3],[3,7],[0.5,9],[3,5],[9,1]]) ([0, 0], [6, 0], [9, 1], [2, 3], [3, 5], [3, 7], [0.5, 9]) """ if the start is None: start = points[0] return min([perm for perm ...

mapreduce function in mongodb with example

how to use mapreduce in mongodb.mapreduce function in mongodb with example
MapReduce function in MongoDB with the example


=> BASIC OF MapReduce

syntaxdb.collectionname.mapReduce(
function(){emit(key,value)},
function(key,value){return reducefunction},
query:document,
sort:document,
limit:number,
verbose:boolean,
out:newcollection

})

In Detail Explaination


=> The MapReduce() the first Query's the Collection then maps the result the document to emit key-value pairs which are then reduced based on the keys that have multiple values.

=> Map: It is a javascript function that maps a value with a key and emits key-value pair.

=> Reduce: It is a javascript function that reduces or groups all the documents having the same key.

=> out: It specify's the location of the map reduce (Query if Fire) result.

=> query: It is used to specify the optional selection criteria for selecting the documents.

=> verbose: It is used to specify Boolean Value which signifies whether the details regarding the functioning of map-reduce should be displayed or not. (By default TRUE)

=> limit: It specifies the number of documents which you want to display.

=>Using Of MapReduce

>use emp
>db.createCollection("stud")

>db.stud.insert([
{eno:1,ename:"Smit",course:"MBA",collage:"Marwadi"},
{eno:2,ename:"Amit",course:"BE_IT",collage:"Marwadi"},{eno:3,ename:"Dipak",course:"MCA",collage:"Marwadi"},
{eno:4,ename:"Umang",course:"BE_COMPUTER",collage:"Marwadi"}])

=>Create map and reduce function
> var map = function(){emit(this.ename,this.collage);}
> var reduce = function(key,value){return Array.sum(value)}
> db.stud.mapReduce(map,reduce,{out:"res1"})
OUTPUT IS 
{
        "result" : "res1",
        "timeMillis" : 2109,
        "counts" : {
                "input" : 4,
                "emit" : 4,
                "reduce" : 0,
                "output" : 4
        },
        "ok" : 1
}

=>Displayed Reduced Documents which we can store in the new Collection res1.
> db.res1.find()
{ "_id" : "Amit", "value" : "Marwadi" }
{ "_id" : "Dipak", "value" : "Marwadi" }
{ "_id" : "Smit", "value" : "Marwadi" }
{ "_id" : "Umang", "value" : "Marwadi" }

NOTE: For COUNT Put <1> in Value in emit().(eg.(key,1)) 1 is there Because every time it can be matched same value then it will be increased automatically.

=>Count total Reduced Documents and store in res2.
> var map = function(){emit(this.collage,1);}
> var reduce = function(key,value){return Array.sum(value)
> db.stud.mapReduce(map,reduce,{out:"res2"})
{
        "result" : "res2",
        "timeMillis" : 771,
        "counts" : {
                "input" : 4,
                "emit" : 4,
                "reduce" : 1,
                "output" : 1
        },
        "ok" : 1
}

=>Displayed Reduced Docuements which we can store in the new Collection res2.
> db.res2.find()
{ "_id" : "Marwadi", "value" : 4 }

For Different Result

=>Update the document set Collage name IIT where name Smit, Amit.
> db.stud.update({ename:"Smit"},{$set:{collage:"IIT"}})
> db.stud.update({ename:"Dipak"},{$set:{collage:"IIT"}})

=>Now Map Reduce result store in res3.
> db.stud.mapReduce(map,reduce,{out:"res3"})
{
        "result" : "res3",
        "timeMillis" : 1051,
        "counts" : {
                "input" : 4,
                "emit" : 4,
                "reduce" : 2,
                "output" : 2
        },
        "ok" : 1
}

NOTE: From the Following output it can be matched with different colleges so matching value changed that is "2" in this output. 

> db.res3.find()
{ "_id" : "IIT", "value" : 2 }
{ "_id" : "Marwadi", "value" : 2 }




Comments

Popular posts from this blog

MongoDB Exercises Practice Solution Exercise First

Simple Basic Commands For MongoDB

Artificial Intelligence Programs For Programming in Python