- Timestamp:
- 10/11/19 00:25:20 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
issm/trunk-jpl/src/m/classes/qmu/linear_equality_constraint.py
r23716 r24213 3 3 from MatlabArray import * 4 4 5 5 6 class linear_equality_constraint: 6 7 ''' 7 8 constructor for the linear_equality_constraint class. 8 9 9 10 [lec] = linear_equality_constraint.linear_equality_constraint(args) 10 lec 11 lec = linear_equality_constraint() 11 12 12 13 where the required args are: … … 21 22 arguments constructs a new instance from the arguments. 22 23 ''' 23 24 self.matrix =np.array([[float('NaN')]])25 self.target =0.26 27 self.scale =1.24 def __init__(self): 25 self.matrix = np.array([[float('NaN')]]) 26 self.target = 0. 27 self.scale_type = 'none' 28 self.scale = 1. 28 29 29 30 31 30 @staticmethod 31 def linear_equality_constraint(*args): 32 nargin = len(args) 32 33 33 34 35 34 # create a default object 35 if nargin == 0: 36 return linear_equality_constraint() 36 37 37 38 39 if isinstance(args[0],linear_equality_constraint):40 41 42 raise RuntimeError('Object is a '+str(type(args[0]))+' class object, not "linear_equality_constraint"')38 # copy the object 39 elif nargin == 1: 40 if isinstance(args[0], linear_equality_constraint): 41 lec = args[0] 42 else: 43 raise RuntimeError('Object is a ' + str(type(args[0])) + ' class object, not "linear_equality_constraint"') 43 44 44 45 46 if (np.shape(args[0],1) == array_np.size(args[1:min(nargin,4)]) or np.shape(args[0],1) == 1):47 asizec = np.shape(args[1:min(nargin,4)])48 elif (array_np.size(args[1:min(nargin,4)]) == 1):49 asizec = [np.shape(args[0],1),1]50 51 raise RuntimeError('Matrix for object of class '+str(type(lec))+' has inconsistent number of rows.')45 # create the object from the input 46 else: 47 if (np.shape(args[0], 1) == array_np.size(args[1:min(nargin, 4)]) or np.shape(args[0], 1) == 1): 48 asizec = np.shape(args[1:min(nargin, 4)]) 49 elif (array_np.size(args[1:min(nargin, 4)]) == 1): 50 asizec = [np.shape(args[0], 1), 1] 51 else: 52 raise RuntimeError('Matrix for object of class ' + str(type(lec)) + ' has inconsistent number of rows.') 52 53 53 54 lec = [linear_equality_constraint() for i in range(asizec[0]) for j in range(asizec[1])] 54 55 55 56 57 lec[i].matrix = args[0][i,:]58 59 lec[i].matrix= args[0]56 for i in range(np.size(lec)): 57 if (np.shape(args[0])[0] > 1): 58 lec[i].matrix = args[0][i, :] 59 else: 60 lec[i].matrix = args[0] 60 61 61 62 63 64 lec[i].target= args[1][i]65 66 lec[i].target= args[1]62 if (nargin >= 2): 63 for i in range(np.size(lec)): 64 if (np.size(args[1]) > 1): 65 lec[i].target = args[1][i] 66 else: 67 lec[i].target = args[1] 67 68 68 if (nargin >= 3): 69 for i in range(np.size(lec)): 70 if (np.size(args[2]) > 1): 71 lec[i].scale_type = args[2][i] 72 else: 73 lec[i].scale_type = str(args[2]) 74 75 if (nargin >= 4): 76 for i in range(np.size(lec)): 77 if (np.size(args[3]) > 1): 78 lec[i].scale = args[3][i] 79 else: 80 lec[i].scale = args[3] 69 if (nargin >= 3): 70 for i in range(np.size(lec)): 71 if (np.size(args[2]) > 1): 72 lec[i].scale_type = args[2][i] 73 else: 74 lec[i].scale_type = str(args[2]) 81 75 82 if (nargin > 4): 83 print('WARNING: linear_equality_constraint:extra_arg: Extra arguments for object of class '+str(type(lec))+'.') 76 if (nargin >= 4): 77 for i in range(np.size(lec)): 78 if (np.size(args[3]) > 1): 79 lec[i].scale = args[3][i] 80 else: 81 lec[i].scale = args[3] 84 82 85 return lec 86 83 if (nargin > 4): 84 print('WARNING: linear_equality_constraint:extra_arg: Extra arguments for object of class ' + str(type(lec)) + '.') 87 85 88 def __repr__(self): 89 # display the object 90 string = '\n' 91 string += 'class "linear_equality_constraint" object = \n' 92 string += ' matrix: ' +str(self.matrix) + '\n' 93 string += ' target: ' +str(self.target) + '\n' 94 string += ' scale_type: ' +str(self.scale_type) + '\n' 95 string += ' scale: ' +str(self.scale) + '\n' 86 return lec 96 87 97 return string 88 def __repr__(self): 89 # display the object 90 string = '\n' 91 string += 'class "linear_equality_constraint" object = \n' 92 string += ' matrix: ' + str(self.matrix) + '\n' 93 string += ' target: ' + str(self.target) + '\n' 94 string += ' scale_type: ' + str(self.scale_type) + '\n' 95 string += ' scale: ' + str(self.scale) + '\n' 98 96 99 @staticmethod 100 def prop_matrix(lec): 101 if type(lec) not in [list,np.ndarray]: 102 return lec.matrix 97 return string 103 98 104 matrix = np.zeros(np.size(lec)) 105 for i in range(np.size(lec)): 106 matrix[i,0:np.shape(lec[i].matrix)[1]] = lec[i].matrix[0,:] 99 @staticmethod 100 def prop_matrix(lec): 101 if type(lec) not in [list, np.ndarray]: 102 return lec.matrix 107 103 108 return matrix 104 matrix = np.zeros(np.size(lec)) 105 for i in range(np.size(lec)): 106 matrix[i, 0:np.shape(lec[i].matrix)[1]] = lec[i].matrix[0, :] 109 107 110 @staticmethod 111 def prop_lower(lec): 112 lower=[] 113 return lower 108 return matrix 114 109 115 116 def prop_upper(lec):117 upper=[]118 return upper110 @staticmethod 111 def prop_lower(lec): 112 lower = [] 113 return lower 119 114 120 121 def prop_target(lec):122 if type(lec) not in [list,np.ndarray]: 123 return lec.target 115 @staticmethod 116 def prop_upper(lec): 117 upper = [] 118 return upper 124 119 125 target = np.zeros(np.shape(lec)) 126 for i in range(np.size(lec)): 127 target[i] = lec[i].target 128 129 target = allequal(target,0.) 120 @staticmethod 121 def prop_target(lec): 122 if type(lec) not in [list, np.ndarray]: 123 return lec.target 130 124 131 return target 125 target = np.zeros(np.shape(lec)) 126 for i in range(np.size(lec)): 127 target[i] = lec[i].target 132 128 133 @staticmethod 134 def prop_stype(lec): 135 if type(lec) not in [list,np.ndarray]: 136 return lec.scale_type 129 target = allequal(target, 0.) 137 130 138 stype = ['' for i in range(np.size(lec))] 139 for i in range(np.size(lec)): 140 stype[i] = str(lec[i].scale_type) 141 142 stype = allequal(stype,'none') 131 return target 143 132 144 return stype 133 @staticmethod 134 def prop_stype(lec): 135 if type(lec) not in [list, np.ndarray]: 136 return lec.scale_type 145 137 146 @staticmethod 147 def prop_scale(lec): 148 if type(lec) not in [list,np.ndarray]: 149 return lec.scale 138 stype = ['' for i in range(np.size(lec))] 139 for i in range(np.size(lec)): 140 stype[i] = str(lec[i].scale_type) 150 141 151 scale = np.zeros(np.shape(lec)) 152 for i in range(np.size(lec)): 153 scale[i] = lec[i].scale 154 155 scale = allequal(scale,1.) 142 stype = allequal(stype, 'none') 156 143 157 return scale 158 159 @staticmethod 160 def dakota_write(fidi,dvar): 161 # collect only the variables of the appropriate class 162 lec = [struc_type(i,'linear_equality_constraint','lec') for i in dvar] 144 return stype 163 145 164 # write constraints 165 lclist_write(fidi,'linear_equality_constraints','linear_equality',lec) 146 @staticmethod 147 def prop_scale(lec): 148 if type(lec) not in [list, np.ndarray]: 149 return lec.scale 150 151 scale = np.zeros(np.shape(lec)) 152 for i in range(np.size(lec)): 153 scale[i] = lec[i].scale 154 155 scale = allequal(scale, 1.) 156 157 return scale 158 159 @staticmethod 160 def dakota_write(fidi, dvar): 161 # collect only the variables of the appropriate class 162 lec = [struc_type(i, 'linear_equality_constraint', 'lec') for i in dvar] 163 164 # write constraints 165 lclist_write(fidi, 'linear_equality_constraints', 'linear_equality', lec)
Note:
See TracChangeset
for help on using the changeset viewer.