示例#1
0
def update(self):
    if self.maximum not in self.path_dic_ucs:
        temp2 = []
        temp3 = []
        temp4 = []
        temp2.append(self.elements[0])
        temp2.append(self.elements[1])
        temp2.append(self.elements[3])
        temp2.append(self.elements[2])
        num2 = int(temp2[0] + temp2[1] + temp2[2] + temp2[3])
        if num2 not in self.path_dic_ucs:
            p2 = pk.Pancake(temp2[0] + temp2[1] + temp2[2] + temp2[3] + "u")
            p2.maximum = num2
            p2.parent = self
            self.left = p2
            p2.g = 2
            p2.total_g = self.total_g + p2.g
            p2.path_dic_ucs = self.path_dic_ucs
            add_path_ucs(p2, p2.maximum)
            p2.update_h()

        temp3.append(self.elements[0])
        temp3.append(self.elements[3])
        temp3.append(self.elements[2])
        temp3.append(self.elements[1])
        num3 = int(temp3[0] + temp3[1] + temp3[2] + temp3[3])
        if num3 not in self.path_dic_ucs:
            p3 = pk.Pancake(temp3[0] + temp3[1] + temp3[2] + temp3[3] + "u")
            p3.maximum = num3
            p3.parent = self
            self.mid = p3
            p3.g = 3
            p3.total_g = self.total_g + p3.g
            p3.path_dic_ucs = self.path_dic_ucs
            add_path_ucs(p3, p3.maximum)
            p3.update_h()

        temp4.append(self.elements[3])
        temp4.append(self.elements[2])
        temp4.append(self.elements[1])
        temp4.append(self.elements[0])
        num4 = int(temp4[0] + temp4[1] + temp4[2] + temp4[3])
        if num4 not in self.path_dic_ucs:
            p4 = pk.Pancake(temp4[0] + temp4[1] + temp4[2] + temp4[3] + "u")
            p4.maximum = num4
            p4.parent = self
            self.mid = p4
            p4.g = 4
            p4.total_g = self.total_g + p4.g
            p4.path_dic_ucs = self.path_dic_ucs
            add_path_ucs(p4, p4.maximum)
            p4.update_h()
示例#2
0
def random_pancake():
    sdp = p.Pancake()
    for z in range(0,2):
        sdp.set_val(0,0,z,R())
        for theta in range(0,6):
            sdp.set_val(1,theta,z,R())
    return sdp 
示例#3
0
def create_pc(value):
    elements = []
    for c in str(value):
        elements.append(c)
    elements.append("u")
    temp = pk.Pancake(elements)
    temp.update_h()
    temp.g = 0
    temp.total_g = 0
    return temp
示例#4
0
from socket import *
import pancake as p

#holder for the client Small data pancake
client_sdp = p.Pancake()
serverPort = 12000
serverSocket = socket(AF_INET, SOCK_STREAM)
serverSocket.bind(('', serverPort))
serverSocket.listen(1)
print 'The server is ready to receive'
connectionSocket, addr = serverSocket.accept()
#while 1:
pancake_string = connectionSocket.recv(1024)
print "RECIVED FROM CLINET"
print pancake_string
client_sdp.from_string(pancake_string)
client_sdp.set_val(0, 0, 0, 5000)
client_sdp.set_val(0, 0, 1, 5000)
modified_pancake = client_sdp.to_string()
connectionSocket.send(modified_pancake)
connectionSocket.close()
示例#5
0
from socket import *
import pancake as p
import randomcake as rc
#server name
serverName = ''
#get server Ip from user
serverName = raw_input("Enter ip:")
serverPort = 12000
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((serverName,serverPort))
#make a random pancake
sdp = rc.random_pancake()
#print cake before sending it over wire
print "Before Seding"
sdp.print_cake()
modified_pancake_string = ''
#container to hold the modified pancake
modified_pancake = p.Pancake()
pancake_string = sdp.to_string()
print "client pancake string"
print pancake_string
#while 1:
clientSocket.send(pancake_string)
modified_pancake_string = clientSocket.recv(1024)
modified_pancake.from_string(modified_pancake_string)
modified_pancake.print_cake()
clientSocket.close()


示例#6
0
# Get user input
user_in = raw_input("\nPlease enter four digits and one letter(d/u/g/a)\n")
length = len(user_in)
# Create a pancake list
# Each pancake cannot have the same value
pancake = []
if length != 5:
    exit(0)
else:
    for x in range(0, length):
        if x != length:
            pancake.append(user_in[x])

# Test Cases
# print(pancake[x], type(pancake[x]))
p1 = pk.Pancake(pancake)

# d is for DFS
if p1.choice == "d":
    dfs.add_path(p1)
    p1.update_h()
    dfs.print_dfs(p1)
    while p1.h != 0:
        dfs.compare_update(p1)
        p1.update_h()
        dfs.print_dfs(p1)
# This part is for UCS
elif p1.choice == "u":
    ucs.ucs(p1)
# This part is for Greedy
elif p1.choice == "g":
示例#7
0
def astar(p, visited, c):
    cost = c
    temp = pk.Pancake("1234a")
    while p.maximum != 4321:
        val = 0
        visit = visited
        if p.maximum not in visit:
            visit.append(p.maximum)
        p.set_next()
        values = []
        p1 = p.cakelist[0]
        p2 = p.cakelist[1]
        p3 = p.cakelist[2]
        p1.update_h()
        p2.update_h()
        p3.update_h()
        p1.astar = p1.g + p1.h
        p2.astar = p2.g + p2.h
        p3.astar = p3.g + p3.h
        dict_star = {
            p1.maximum: p1.astar,
            p2.maximum: p2.astar,
            p3.maximum: p3.astar
        }
        sort_star = sorted(dict_star.items())
        if sort_star[0][1] == sort_star[1][1] and sort_star[0][1] == sort_star[
                2][1]:
            for i in sort_star:
                if i[0] not in visit:
                    values.append(i[0])
                    val = max(values)

        elif sort_star[0][0] == sort_star[1][0]:
            if sort_star[0][0] not in visit and sort_star[1][0] not in visit:
                val = max(sort_star[0][0], sort_star[1][0])
            elif sort_star[0][0] in visit and sort_star[1][0] not in visit:
                val = sort_star[1][0]
            elif sort_star[0][0] not in visit and sort_star[1][0] in visit:
                val = sort_star[0][0]
            else:
                if sort_star[3][0] not in visit:
                    val = sort_star[3][0]

        else:
            if sort_star[0][0] not in visit:
                val = sort_star[0][0]
            else:
                if sort_star[1][1] == sort_star[2][1]:
                    if sort_star[1][0] not in visit and sort_star[2][
                            0] not in visit:
                        val = max(sort_star[1][0], sort_star[2][0])
                    elif sort_star[1][0] in visit and sort_star[2][
                            0] not in visit:
                        val = sort_star[2][0]
                    elif sort_star[1][0] not in visit and sort_star[2][
                            0] in visit:
                        val = sort_star[1][0]
                else:
                    val = sort_star[1][0]
        if val == p1.maximum:
            temp = p1
        elif val == p2.maximum:
            temp = p2
        elif val == p3.maximum:
            temp = p3

        if val in visit:
            if temp == p1:
                if p2.astar > p3.astar:
                    temp = p3
                if temp.maximum in visit:
                    temp = p2
                else:
                    temp = p3

            elif temp == p2:
                if p1.astar > p3.astar:
                    temp = p3
                if temp.maximum in visit:
                    temp = p1
                else:
                    temp = p3

            elif temp == p3:
                if p1.astar > p2.astar:
                    temp = p2
                else:
                    temp = p1
                if temp.maximum in visit:
                    temp = p1
                else:
                    temp = p2

        cost = cost + temp.g
        print_astar(p, cost)
        astar(temp, visit, cost)
    print_astar(p, cost)
    exit(0)
示例#8
0
def greedy(p, visited, c):
	cost = c
	p_temp = pk.Pancake("1234a")
	while p.h != 0:
		p.set_next()
		p1 = p.cakelist[0]
		p2 = p.cakelist[1]
		p3 = p.cakelist[2]
		p1.update_h()
		p2.update_h()
		p3.update_h()
		visit = visited
		if p.maximum not in visit:
			visit.append(p.maximum)
		# There is no equality
		if p1.h < p2.h:
			if p1.h < p3.h:
				p_temp = p1
			else:
				p_temp = p3
		else:
			if p2.h < p3.h:
				p_temp = p2
			else:
				p_temp = p3

		if p1.h == p2.h & p2.h == p3.h:
			if p1.maximum > p2.maximum:
				if p1.maximum > p3.maximum:
					p_temp = p1
				else:
					p_temp = p3
			else:
				if p2.maximum > p3.maximum:
					p_temp = p2
				else:
					p_temp = p3

		elif p1.h == p2.h & p3.h > p1.h:
			if p1.maximum > p2.maximum:
				p_temp = p1
			else:
				p_temp = p2

		elif p1.h == p2.h & p3.h < p1.h:
			p_temp = p3

		elif p1.h == p3.h & p2.h > p1.h:
			if p1.maximum > p3.maximum:
				p_temp = p1
			else:
				p_temp = p3

		elif p1.h == p3.h & p2.h < p1.h:
			p_temp = p2

		elif p2.h == p3.h & p1.h > p2.h:
			if p2.maximum > p3.maximum:
				p_temp = p2
			else:
				p_temp = p3
		elif p2.h == p3.h & p1.h < p2.h:
			p_temp = p1

		if p_temp.maximum in visit:
			if p_temp == p1:
				if p2.h > p3.h:
					p_temp = p3
				else:
					p_temp=p2
				if p_temp.maximum in visit:
					if p_temp==p3:
						p_temp = p2
					else:
						p_temp = p3

			elif p_temp == p2:
				if p1.h > p3.h:
					p_temp = p3
				else:
					p_temp=p1
				if p_temp.maximum in visit:
					if p_temp==p3:
						p_temp = p1
					else:
						p_temp = p3

			elif p_temp == p3:
				if p1.h > p2.h:
					p_temp = p2
				else:
					p_temp=p1
				if p_temp.maximum in visit:
					if p_temp==p2:
						p_temp = p1
					else:
						p_temp = p2
		print_gd(p, cost)
		cost = cost + p_temp.g
		greedy(p_temp, visit, cost)
	print_gd(p, cost)
	exit(0)
示例#9
0
import pancake as p
import randomcake as rc

print "python Pancke testing:::::::\n"
sdp = rc.random_pancake()
sdp.print_cake()
p_string = sdp.to_string()
print "string print"
print p_string
test_p = p.Pancake()
print "test pancake"
test_p.print_cake()
test_p.from_string(p_string)
test_p.print_cake()
print "test adding"
test_p.set_val(1, 4, 1, 5000)
test_p.set_val(1, 12, 1, 5000)
test_p.set_val(1, 24, 0, 5000)
test_p.set_val(0, 0, 1, 5000)
test_p.set_val(0, 0, 0, 5000)
test_p.print_cake()
print "yo :"
print test_p.get_val(1, 25, 0)

new_grow = p.Pancake
print "meow"
test_p.print_cake()
print "cake"

print "-----------------------------------------"