Пример #1
0
 def add_constraint(self,c):
     # check for inference
     infered_list = []
     for c_ in self.constraint_list:
         if set(c_.var_list).issubset(c.var_list):
             new_var_l = list(set(c.var_list) - set(c_.var_list))
             new_val = c.value - c_.value
             new_c = constraint(new_var_l,new_val)
             infered_list.append(new_c)
         elif set(c.var_list).issubset(c_.var_list):
             new_var_l = list(set(c_.var_list) - set(c.var_list))
             new_val = c_.value - c.value
             new_c = constraint(new_var_l, new_val)
             infered_list.append(new_c)
     self.constraint_list.append(c)
     self.constraint_list = self.constraint_list + infered_list
Пример #2
0
            def createConstraint(indices, u, v, r):
                for i in indices:
                    g.constraints.append(constraint(g.nodes[i], u, v, r, 'y'))

                    g.constraintX.append(g.constraints[len(g.constraints) -
                                                       1].node.u)
                    g.constraintY.append(g.constraints[len(g.constraints) -
                                                       1].node.v)
                    g.constraintColor.append(g.constraints[len(g.constraints) -
                                                           1].color)
Пример #3
0
    def build_new_constraint(self,i,j,val):
        neighs = self.get_neighb(i,j)
        new_neighs = []
        for l,m in neighs:
            if self.known_board[l,m] == 1:
                val = val - self.current_state[l,m]
            else:
                new_neighs.append((l,m))

        for l,m in new_neighs:
            self.isVariable[l,m] = 1

        return constraint(var_list=new_neighs,val=val)
Пример #4
0
def decoder(individual):
    global genotype
    phenotype = {}
    for item in genotype:
        gene = individual[item[-1]:item[-1] + item[2]]
        integer = 0
        for i in range(item[2]):
            integer += gene[i] * 2**(item[2] - i - 1)
        if item[1] == "discrete":
            excess = 2**item[2] - len(item[-2])
            if integer + 1 <= 2 * excess:
                index = integer // 2
            else:
                index = integer - excess
            phenotype[item[0]] = item[-2][index]
        else:
            phenotype[item[0]] = item[3] + float(integer) / float(
                2**item[2] - 1) * (item[4] - item[3])
    if flag_constraint:
        from constraint import constraint
        phenotype = constraint(phenotype)
    return phenotype
Пример #5
0
def ParseLine(line,size):
    #the variables of the sudoku
    variables = []
    constraints = []
    count = 0
    for unit in line:
        if unit == '.':
            #Create a variable with a full domain.
            variables.append(variable.Variable(range(1, size+1)))
        elif( RepresentsInt( unit ) ):
            #Create a variable with a full domain
            variables.append(variable.Variable(range(1, size+1)))
            #create a constraint
            constraint_list = [1 , 2, 3, 4, 5, 6, 7, 8, 9] 
            constraint_list.remove (int(unit) )
            for x in constraint_list:
                new_constraint =  constraint.constraint( variables[ len(variables) - 1 ] )
                new_constraint.unary_constraint = x
                constraints.append (new_constraint)
        count = count + 1

    sudoku_constraints = general_sudoku_constraints( variables )
    
    return [variables, constraints]
Пример #6
0
def ParseLine(line,size):
    #The variables of the sudoku
    variables = []
    #The constraints of the sudoku
    constraints = []
    count = 0
    for unit in line:
        if unit == '.':

            #Create a variable with a full domain.
            variables.append(variable.Variable(range(1, size+1)))
        elif( RepresentsInt( unit ) ):
            #Create a variable with a full domain
            variables.append(variable.Variable(range(1, size+1)))
            #Create all the unary constraints for this given number
            constraint_list = range(1, size+1) 
            constraint_list.remove (int(unit))
            for x in constraint_list:
                new_constraint =  constraint.constraint( len(variables) - 1 )
                new_constraint.unary_constraint = x
                constraints.append (new_constraint)
        count = count + 1

    return (variables, constraints)
Пример #7
0
    def interface(self):
        button = str(self.__getattribute__("button"))
        # check for left click
        if button == "MouseButton.LEFT":

            # Initialize file dialogue interface
            root = tk.Tk()
            root.withdraw()
            
            filepath = filedialog.askopenfilename()
            if filepath == "":
                return
            try:

                # convert file contents to a dataframe
                dfs = pd.read_html(filepath)

                # Clear existing structure
                g.nodes = []
                g.nodeX, g.nodeY, g.nodeColor = [], [], []

                g.elements = []
                loops = len(g.elementsPlot)
                for i in range(0, loops):
                    g.elementsPlot.pop(0).remove()

                g.constraints = []
                g.constraintX, g.constraintY, g.constraintColor = [], [], []

                g.forces = []
                loops = len(g.forcesPlot)
                for i in range(0, loops):
                    g.forcesPlot.pop(0).remove()

                g.moments = []
                loops = len(g.momentsPlot)
                for i in range(0, loops):
                    g.momentsPlot.pop(0).remove()

                    
                # Loop through the dataframe and create the structure
                table = 0
                for i in dfs:
                    if table == 0:
                        for j in i.values:
                            g.nodes.append(node(j[1], j[2], 'b'))
                            g.nodeX.append(g.nodes[len(g.nodes)-1].u)
                            g.nodeY.append(g.nodes[len(g.nodes)-1].v)
                            g.nodeColor.append(g.nodes[len(g.nodes)-1].color)
                    elif table == 1:
                        for j in i.values:
                            g.elements.append(element(g.nodes[int(j[1])], g.nodes[int(j[2])], j[3], j[4], j[5], 'k'))
                    elif table == 2:
                        for j in i.values:
                            g.constraints.append(constraint(g.nodes[int(j[1])], bool(j[2]), bool(j[3]), bool(j[4]), 'y'))
                            g.constraintX.append(g.constraints[len(g.constraints)-1].node.u)
                            g.constraintY.append(g.constraints[len(g.constraints)-1].node.v)
                            g.constraintColor.append(g.constraints[len(g.constraints)-1].color)
                    elif table == 3:
                        for j in i.values:
                            g.forces.append(force(g.nodes[int(j[1])], j[2], j[3], 'darkred'))
                    elif table == 4:
                        for j in i.values:
                            g.moments.append(moment(g.nodes[int(j[1])], j[2], 'darkred'))
                    table += 1
            except ValueError:  # If the file chosen is not .html or has invalid contents
                tk.messagebox.showerror(title="File Error", message="Invalid Structure File")

            # Clear the dataframe
            dfs = 0
Пример #8
0
def general_sudoku_constraints( variables ):
    size = int (math.sqrt(len(variables) )) 
    constraints = []
    #constraint for every number in every column the number cannot be equal
    for a in range (0, size):
        row = variables[ size * a : size * a + size]
        for i in row:
            for j in row:
                if(i != j):
                    new_constraint =  constraint.constraint( i )
                    new_constraint.variable2 = j
                    constraints.append( new_constraint )


    #constraints for every 
    for i in range(0,size):
        #makes 9 rows
        column = []

        for j in range( 0, size ):
            #makes a row 
            column.append( variables[ size * j + i] )
        for k in column:
            for z in column:
                if( k != z ):
                    new_constraint = constraint.constraint( k )
                    new_constraint.variable2 = z
                    constraints.append( new_constraint )

    #constraints for every box
    #first column of boxes
    boxsize = int (math.sqrt( size) )
    for i in range( 0, boxsize ):
        #makes 9 boxes (a box has the same number of variables as the size of the sudoku)
        box = []
        for j in range(0, boxsize ) :
            for k in range(0, boxsize ):
                box.append( variables [ k + j* size + (i * boxsize * size) ] )
        for k in box:
            for z in box:
                if( k != z ):
                    new_constraint = constraint.constraint( k )
                    new_constraint.variable2 = z
                    constraints.append( new_constraint )

    #second column of boxes
    for i in range( 0, boxsize ):
        #makes 9 boxes (a box has the same number of variables as the size of the sudoku)
        box = []
        for j in range(0, boxsize ) :
            for k in range(0, boxsize ):
                box.append( variables [ k + j* size + (i * boxsize * size) + 3 ] )
        for k in box:
            for z in box:
                if( k != z ):
                    new_constraint = constraint.constraint( k )
                    new_constraint.variable2 = z
                    constraints.append( new_constraint )

    #third column of boxes
    for i in range( 0, boxsize ):
        #makes 9 boxes (a box has the same number of variables as the size of the sudoku)
        box = []
        for j in range(0, boxsize ) :
            for k in range(0, boxsize ):
                box.append( variables [ k + j* size + (i * boxsize * size) + 6 ] )
        for k in box:
            for z in box:
                if( k != z ):
                    new_constraint = constraint.constraint( k )
                    new_constraint.variable2 = z
                    constraints.append( new_constraint )

    return constraints
Пример #9
0
I_Ei = np.c_[I[:-1], I[1:]]
x_Eid = x_Id[I_Ei]
J_Emde = np.einsum('mei,Eid->Emed', dN_mei, x_Eid)
J_det_Em = np.linalg.det(J_Emde)
J_inv_Emed = np.linalg.inv(J_Emde)
# Quadratic forms
dN_Eimd = np.einsum('mei,Eide->Eimd', dN_mei, J_inv_Emed)
BB_ECidDjf = np.einsum('m, CD, C, C, Eimd,Ejmf,Em->ECidDjf', w_m, DELTA_cd,
                       A_c, E_c, dN_Eimd, dN_Eimd, J_det_Em)
NN_ECidDjf = np.einsum('m, CD,mi,mj,,,Em->ECiDj', w_m, SWITCH_cd, N_mi, N_mi,
                       p, G, J_det_Em)
BB_Eij = BB_ECidDjf.reshape(-1, n_e_dof, n_e_dof)
NN_Eij = NN_ECidDjf.reshape(-1, n_e_dof, n_e_dof)
K_Eij = BB_Eij + NN_Eij
# Multilayer expansion
C = np.arange(n_c) * n_n_tot
I_C = I[np.newaxis, :] + C[:, np.newaxis]
I_ECi = np.vstack([[I_C[:, :-1], I_C[:, 1:]]]).T.reshape(-1, n_e_dof)
# apply constraints and solve
F_ext = np.zeros((n_dof_tot, ), np.float_)
K_Eij, F_ext = constraint(n_dof_tot / 2 - 1, 0., K_Eij, I_ECi, F_ext)
K_Eij, F_ext = constraint(n_dof_tot - 1, 0.01, K_Eij, I_ECi, F_ext)
d_I = cg(K_Eij, F_ext, I_ECi)
print 'd_I', d_I
# post processing
import matplotlib.pyplot as plt
plt.plot(x_Id[:, 0], d_I.reshape(2, -1).T)
plt.xlabel('x')
plt.ylabel('displacement')
plt.show()
    def __init__(self, L, ham_vec, t, U, max_iter):

        self.L = L
        self.max_iter = max_iter

        self.t_ = t
        self.U_ = U

        self.time_start = time.time()

        g1_vec = np.zeros(2 * self.L, dtype=np.float32)
        z1_vec = np.zeros(2 * self.L, dtype=np.float32)

        g2_ph_vec = np.zeros(4 * L * L * L, dtype=np.float32)
        z2_ph_vec = np.zeros(4 * L * L * L, dtype=np.float32)

        g2_pair1_vec = np.zeros(L * L * L, dtype=np.float32)
        z2_pair1_vec = np.zeros(L * L * L, dtype=np.float32)

        g2_pair2_vec = np.zeros(L * L * L, dtype=np.float32)
        z2_pair2_vec = np.zeros(L * L * L, dtype=np.float32)

        self.primal_var = primal(L, g1_vec, g2_ph_vec, g2_pair1_vec,
                                 g2_pair2_vec)
        self.dual_var = dual(L, z1_vec, z2_ph_vec, z2_pair1_vec, z2_pair2_vec)

        self.const_var = constraint(L)
        self.const_var.generate_constraint_matrix()
        #self.const_var.generate_constraint_matrix_test()

        self.duality_gap = np.zeros(5 * L)

        self.dim_var = len(g1_vec) + len(g2_ph_vec) + len(g2_pair1_vec) + len(
            g2_pair2_vec)
        self.dim_const = self.const_var.dim_const

        self.w = np.zeros((self.dim_var, self.dim_var), dtype=np.float32)
        self.inv_wT = np.zeros((self.dim_var, self.dim_var), dtype=np.float32)

        self.vec_rx = np.zeros(self.dim_var, dtype=np.float32)
        self.vec_ry = np.zeros(self.dim_const, dtype=np.float32)

        self.vec_rz = np.zeros(self.dim_var, dtype=np.float32)
        self.vec_rd = np.zeros(self.dim_var, dtype=np.float32)

        self.wt_lambda_inv_I = np.zeros(self.dim_var, dtype=np.float32)

        self.var_y = np.zeros(self.dim_const, dtype=np.float32)

        self.del_x = np.zeros(self.dim_var, dtype=np.float32)
        self.del_z = np.zeros(self.dim_var, dtype=np.float32)
        self.del_y = np.zeros(self.const_var.dim_const, dtype=np.float32)

        self.vec_ham = ham_vec

        self.debug = False

        if (self.debug):
            self.eig_val_x = np.zeros(2 * self.L + self.L * self.L * 2 +
                                      self.L * self.L + self.L * self.L,
                                      dtype=np.complex64)
            self.eig_val_z = np.zeros(2 * self.L + self.L * self.L * 2 +
                                      self.L * self.L + self.L * self.L,
                                      dtype=np.complex64)

        self.if_combined_direction = True  # if calculate combined direction

        self.convg_criteria = 0.001

        self.prev_error = 1000.0

        self.truncation = 0.001