コード例 #1
0
    def Interpolate(self, **kwds):  #XXX: refactor so use self.interpolator ?
        """interpolate data (x,z) to generate response function z=f(*x)

        Input:
          maxpts: int, maximum number of points to use from (x,z)
          noise: float, amplitude of gaussian noise to remove duplicate x
          method: string for kind of interpolator
          extrap: if True, extrapolate a bounding box (can reduce # of nans)
          arrays: if True, return a numpy array; otherwise don't return arrays

        Output:
          interpolated response function, where z=f(*x.T)

        NOTE:
          if scipy is not installed, will use np.interp for 1D (non-rbf),
          or mystic's rbf otherwise. default method is 'nearest' for
          1D and 'linear' otherwise. method can be one of ('rbf','linear',
          'nearest','cubic','inverse','gaussian','quintic','thin_plate').
        """
        from interpolator import Interpolator
        args = self.args.copy()
        args.update(kwds)
        maxpts, noise = self.maxpts, self.noise
        ii = Interpolator(self.x, self.z, maxpts=maxpts, noise=noise, **args)
        self.surrogate = ii.Interpolate(**args)
        # build the surrogate
        self.surrogate.__doc__ = self.objective.__doc__
        return self.surrogate
コード例 #2
0
 def _get_interpolator(self):
     single_CB = CompositeCrackBridge(E_m=self.E_m,
                                      reinforcement_lst=[self.reinforcement])
     CB_model = CompositeCrackBridgePostprocessor(model=single_CB)
     return Interpolator(CB_model = CB_model,
                          load_sigma_c_max = self.load_sigma_c_max,
                          load_n_sigma_c = self.load_n_sigma_c,
                          n_w = 80, n_x = 61, n_BC = 6
                          )
コード例 #3
0
 def get_x_y(data_list):
     interpolator = Interpolator()
     interpolator.set_u(ACTIONS)
     x = []
     y = []
     for data_row in data_list:
         new_q = data_row["reward"]
         if not data_row["done"]:
             new_q += DISCOUNT * np.max(data_row["next_qualities"])
         interpolator.set_q(data_row["qualities"])
         interpolator.update_function(data_row["action"], new_q)
         x.append(data_row["state"])
         y.append(interpolator.get_q())
     return x, y
コード例 #4
0
    def compute(self, rdf):

        cut_beg    = 13.0
        cut_end    = 15.0

        # Removes values with a density of zero, they will get added back later.
        rdf = rdf[nonzero(rdf[:,1])[0], :]
        # Finds the first index where the density is greater than 0.25*rho.
        i0 = nonzero(rdf[:,1] > 0.25)[0][0]

        # Get distance (r) and energy (e).
        r =  rdf[:,0]
        e = -kB*self.temperature*log(rdf[:,1])


        dr_final = (cut_end-self.min_distance)/self.npts
        # Figure out what range we need

        # Compute derivative from splines.
        rr     = arange(cut_end, r[i0], -dr_final)[::-1]

        interp = Interpolator(r,e)
        ff = [-interp.derivative(ri) for ri in rr]
    
        v0 = [5.0, 0.01]
        lj96_err = lambda v: ff - lj96_force(v,rr)
        v = optimize.leastsq(lj96_err, v0)[0]


        # Add more values to short distance to make sure that 
        # LAMMPS run won't fail when pair distance < table min.
        rpad = arange(self.min_distance, rr[0]-0.5*dr_final, dr_final)
        fpad = lj96_force(v, rpad) + ff[0] - lj96_force(v, rr[0])
        rr   = concatenate((rpad, rr))
        ff   = concatenate((fpad, ff))

        # Now we cut off forces smoothly at any point past max_attraction.
        ff[rr>cut_beg] *= 0.5*(1.0+cos(pi*(rr[rr>cut_beg]-cut_beg)/(cut_end-cut_beg)))
        ff[rr>cut_end] = 0.0

        # Compute energy by integrating forces.
        # Integrating backwards reduces noise.
        ee = -simpson_integrate(rr[::-1], ff[::-1])[::-1]
        ee -= ee[-1]

        self.distance.append(rr)
        self.force.append(ff)
        self.energy.append(ee)
コード例 #5
0
    def fit(self, **kwds):
        """generate an interpolated model from data

    Input:
        data: a mystic legacydata.dataset (or callable model, y = model(x))
        rnd: bool, if False, treat the model as deterministic [default: True]
        cached: bool, if True, use a mystic.cache [default: False]

    NOTE:
        any additional keyword arguments will be passed to the interpolator

    NOTE:
        if data is a model, interpolator will use model's cached archive
        """
        self.__kwds__.update(kwds)
        cached = self.__kwds__.pop('cached', False)
        archive = data = self.__kwds__.pop('data', None)
        self.rnd = self.__kwds__.pop('rnd', self.rnd) and (
            bool(self.__kwds__.get('noise', True))
        )  # or callable(data) or isinstance(data, type('')))
        if callable(data):  #XXX: is a model, allow this?
            data = sample(data, bounds=None, pts=0)  #XXX: axis? multivalue?
        elif isinstance(data, type('')):  #XXX: is a name, allow this?
            import mystic.cache as mc
            import dataset as ds
            data = ds.from_archive(mc.archive.read(data))
        x = getattr(data, 'coords', getattr(data, 'x', None))
        z = getattr(data, 'values', getattr(data, 'y', None))
        from interpolator import Interpolator
        terp = Interpolator(x, z, **self.__kwds__)
        self.__func__ = terp.Interpolate()  #XXX: ValueError: zero-size
        self.__model__ = _init_axis(terp.model)
        self.__model__.__name__ = self.__name__
        self.__kwds__['data'] = archive
        if cached:  #FIXME: clear the archive??? generate new uid name?
            self.__kwds__['cached'] = True
            self._OUQModel__init_cache()
            if hasattr(self.__model__, '__cache__'):
                c = self.__model__.__cache__()
                c.clear()
        else:
            self.__kwds__['cached'] = False
        return
コード例 #6
0
    def __init__(self, window_name="output"):
        self.WIDTH = 250

        self.DISPLAY_STEERING_MIN = -1
        self.DISPLAY_THROTTLE_MIN = -1
        self.DISPLAY_STEERING_MAX = 1
        self.DISPLAY_THROTTLE_MAX = 1

        self.DISPLAY_STEERING_RANGE = self.DISPLAY_STEERING_MAX - self.DISPLAY_STEERING_MIN
        self.DISPLAY_THROTTLE_RANGE = self.DISPLAY_THROTTLE_MAX - self.DISPLAY_THROTTLE_MIN

        self.DISPLAY_STEP = 0.1
        self.DISPLAY_STEERING_SEGMENT_WIDTH = int(
            self.WIDTH * self.DISPLAY_STEP / self.DISPLAY_STEERING_RANGE)
        self.DISPLAY_THROTTLE_SEGMENT_WIDTH = int(
            self.WIDTH * self.DISPLAY_STEP / self.DISPLAY_THROTTLE_RANGE)

        self.HUE_RANGE = 60

        self.img = None
        self.window_name = window_name
        self._clear()
        self.interpolator = Interpolator()
コード例 #7
0
    def __init__(self, simulate=False):
        # setup motor controller. The PWM controller can control up to 16
        # different devices. We have to add devices, one for each thruster that
        # we can control. The first parameter is the human-friendly name of the
        # device. That is used for logging to the console and/or a database. The
        # next parameter indicates which PWM connector this device is connected
        # to. This is refered to as the PWM channel. The last two values
        # indicate at what time intervals (ticks) the PWM should turn on and
        # off, respectively. We simply start each device at 0 time and control
        # the duration of the pulses by adjusting the off time. Note that we may
        # be able to shuffle on/off times to even out the current draw from the
        # thrusters, but so far, that hasn't been an issue. It's even possible
        # that the PWM controller may do that for us already.
        if simulate is False:
            from pwm_controller import PWMController

            self.motor_controller = PWMController()
            self.motor_controller.add_device("HL", HL, 0, NEUTRAL)
            self.motor_controller.add_device("VL", VL, 0, NEUTRAL)
            self.motor_controller.add_device("VC", VC, 0, NEUTRAL)
            self.motor_controller.add_device("VR", VR, 0, NEUTRAL)
            self.motor_controller.add_device("HR", HR, 0, NEUTRAL)
            self.motor_controller.add_device("LIGHT", LIGHT, 0, FULL_REVERSE)
        else:
            self.motor_controller = None

        # setup the joysticks. We use a 2D vector to represent the x and y
        # values of the joysticks.
        self.j1 = Vector2D()
        self.j2 = Vector2D()

        # create interpolators
        self.horizontal_left = Interpolator()
        self.vertical_left = Interpolator()
        self.vertical_center = Interpolator()
        self.vertical_right = Interpolator()
        self.horizontal_right = Interpolator()

        # setup interpolators from a file or manually
        if os.path.isfile(SETTINGS_FILE):
            with open(SETTINGS_FILE, 'r') as f:
                self.set_settings(json.load(f), False)
        else:
            # Set the sensitivity to be applied to each thruster. 0 indicates a
            # linear response which is the default when no sensitivity is applied. 1
            # indicates full sensitivity. Values between 0 and 1 can be used to
            # increase and to decrease the overall sensitivity. Increasing sensivity
            # dampens lower values and amplifies larger values giving more precision
            # at lower power levels.
            self.sensitivity = 0.7

            # We use a cubic to apply sensitivity. If you find that full sensitivity
            # (dampening) does not give you fine enough control, you can increase\
            # the degree of the polynomial used for dampening. Note that this must
            # be a positive odd number. Any other values will cause unexpected
            # results.
            self.power = 3

            # setup the various interpolators for each thruster. Each item we add
            # to the interpolator consists of two values: an angle in degrees and a
            # thrust value. An interpolator works by returning a value for any given
            # input value. More specifically in this case, we will give each
            # interpolator an angle and it will return a thrust value for that
            # angle. Since we have only given the interpolator values for very
            # specific angles, it will have to determine values for angles we have
            # not provided. It does this using linear interpolation.
            self.horizontal_left.addIndexValue(0.0, -1.0)
            self.horizontal_left.addIndexValue(90.0, 1.0)
            self.horizontal_left.addIndexValue(180.0, 1.0)
            self.horizontal_left.addIndexValue(270.0, -1.0)
            self.horizontal_left.addIndexValue(360.0, -1.0)

            self.vertical_left.addIndexValue(0.0, 1.0)
            self.vertical_left.addIndexValue(90.0, -1.0)
            self.vertical_left.addIndexValue(180.0, -1.0)
            self.vertical_left.addIndexValue(270.0, 1.0)
            self.vertical_left.addIndexValue(360.0, 1.0)

            self.vertical_center.addIndexValue(0.0, 0.0)
            self.vertical_center.addIndexValue(90.0, 1.0)
            self.vertical_center.addIndexValue(180.0, 0.0)
            self.vertical_center.addIndexValue(270.0, -1.0)
            self.vertical_center.addIndexValue(360.0, 0.0)

            self.vertical_right.addIndexValue(0.0, -1.0)
            self.vertical_right.addIndexValue(90.0, -1.0)
            self.vertical_right.addIndexValue(180.0, 1.0)
            self.vertical_right.addIndexValue(270.0, 1.0)
            self.vertical_right.addIndexValue(360.0, -1.0)

            self.horizontal_right.addIndexValue(0.0, 1.0)
            self.horizontal_right.addIndexValue(90.0, 1.0)
            self.horizontal_right.addIndexValue(180.0, -1.0)
            self.horizontal_right.addIndexValue(270.0, -1.0)
            self.horizontal_right.addIndexValue(360.0, 1.0)

        # setup ascent/descent controllers
        self.ascent = -1.0
        self.descent = -1.0

        # setup light
        self.light = 0.0
コード例 #8
0
                             nargs=5,
                             required=False,
                             metavar=('DATABASE', 'USER', 'PASSWORD', 'HOST',
                                      'PORT'),
                             help='Specifies connection to database')
    file_parser.add_argument('--db',
                             nargs=5,
                             required=False,
                             metavar=('DATABASE', 'USER', 'PASSWORD', 'HOST',
                                      'PORT'),
                             help='Specifies connection to database')

    args = parser.parse_args()

    output, closing = output.get_outputs(args)
    interpolator = Interpolator(output)

    try:
        if args.command == 'mqtt':

            def on_message(client, user_data, msg):
                if msg.topic == STANDARD_TOPIC:
                    message = msg.payload.rstrip()
                    interpolator.add_item(message.split(','))

            def on_connect(client, userdata, flags, rc):
                client.subscribe(STANDARD_TOPIC)

            client = mqtt.Client()

            client.on_connect = on_connect
コード例 #9
0
                increment = True
            f.close()
            os.rename(outpath + ".tmp", outpath)
            #interpolator.printTime("Completed dump");
            #self.invalid.append(path);
            if not increment:
                i = i + 1
        self.cleanup()


######################################################
# start Gatekeeper and listen for requests...
######################################################
lockfile = "lockfile"
filename = '../weather_data/all.nc'
ip = Interpolator(filename)
gk = Gatekeeper("../coms", lockfile)
mindelay = 0.1
# seconds
start = time.time()
last = start
ip.printTime("Starting")

## Running it indefinitely
try:
    while (True):
        gk.process(ip)
        current = time.time()
        delay = last + mindelay - current
        if (delay > 0):
            time.sleep(delay)
コード例 #10
0
ファイル: run.py プロジェクト: mpriessner/VFIN
        exit(1)
    # Start frame
    batch_count = (cag['frame_count'] - start_frame) // cag['batch_size']
    if (cag['frame_count'] - 1) % cag['batch_size']:
        batch_count += 1

    # Setup
    sys.path.append(f"{os.path.abspath(cag['algorithm'])}")
    from interpolator import Interpolator

    # Interpolate
    interpolator = Interpolator(
        cag['model_path'],
        cag['sf'],
        int(cag['height']),
        int(cag['width']),
        batch_size=cag['batch_size'],
        net_name=cag['net_name'],
        channel=3  # DAIN
    )
    save = data_writer(cag['output_type'])
    ori_frames = []
    batch = [None] * (cag['batch_size'])
    batch.append(video.read()[1])
    interpolator.batch[0] = interpolator.ndarray2tensor([batch[-1]])[0]
    timer = 0
    start_time = time.time()
    try:
        for i in range(batch_count):
            batch[0] = batch[-1]
            batch[1:] = [video.read() for _ in range(cag['batch_size'])]
コード例 #11
0
    def train(self, terminal_state):
        # Start training only if certain number of samples is already saved
        if len(self.replay_memory) < MIN_REPLAY_MEMORY_SIZE:
            return

        # Calculate Prioritized Experience Replay weights
        current_states = np.array([transition[0] for transition in self.replay_memory])
        future_states = np.array([transition[3] for transition in self.replay_memory])
        current_qs = self.model.predict(current_states)
        future_qs = self.target_model.predict(future_states)
        p = np.array([abs((reward + DISCOUNT * np.amax(future_qs[index]) if not done else reward)
                          - current_qs[index][ACTIONS.index(action)])
                      for index, (_, action, reward, _, done) in enumerate(self.replay_memory)])
        p = np.interp(p, (p.min(), p.max()), (0, +1))
        p /= np.sum(p)

        # Get a minibatch of random samples from memory replay table
        minibatch = np.array(self.replay_memory)[np.random.choice(len(self.replay_memory),
                                                                  size=MINIBATCH_SIZE,
                                                                  replace=False,
                                                                  p=p)]  # random.sample(self.replay_memory, MINIBATCH_SIZE)

        # Get current states from minibatch, then query NN model for Q values
        current_states = np.array([transition[0] for transition in minibatch])  # / 255
        current_qs_list = self.model.predict(current_states)

        # Get future states from minibatch, then query NN model for Q values
        # When using target network, query it, otherwise main network should be queried
        new_current_states = np.array([transition[3] for transition in minibatch])  # / 255
        future_target_qs_list = self.target_model.predict(new_current_states)
        future_model_qs_list = self.model.predict(new_current_states)

        x = []
        y = []
        interpolator = Interpolator()

        # Now we need to enumerate our batches
        for index, (current_state, action, reward, new_current_state, done) in enumerate(minibatch):

            # If not a terminal state, get new q from future states, otherwise set it to 0
            # almost like with Q Learning, but we use just part of equation here
            future_model_qs_at_index = future_model_qs_list[index]
            future_target_qs_at_index = future_target_qs_list[index]
            # future_qs = np.reshape(future_model_qs_at_index, OUTPUT_2D_SHAPE)
            if not done:
                max_future_q = future_target_qs_at_index[np.argmax(future_model_qs_at_index)]
                new_q = reward + DISCOUNT * max_future_q
            else:
                new_q = reward

            # Update Q value for given state
            current_qs_list_at_index = current_qs_list[index]
            current_qs = np.reshape(current_qs_list_at_index, OUTPUT_2D_SHAPE)
            current_actions = ACTIONS
            current_qualities = current_qs

            interpolator.set_u(current_actions)
            interpolator.set_q(current_qualities)
            interpolator.update_function(action, new_q)
            # current_qs = np.zeros(OUTPUT_2D_SHAPE)
            # current_qs[:, :2] = interpolator.get_u()
            current_qs = interpolator.get_q()  # [current_actions.index(action)] = [new_q]  #

            # print(current_state)
            # print(current_qs_list)
            # print(action)
            # current_qs[action] = new_q

            # And append to our training data
            x.append(current_state)
            reshaped_current_qs = np.reshape(current_qs, OUTPUT_1D_SHAPE)
            y.append(reshaped_current_qs)

        # print("x:", x)
        # print("y:", y)
        # Fit on all samples as one batch, log only on terminal state
        self.model.fit(np.array(x), np.array(y), batch_size=MINIBATCH_SIZE, verbose=0, shuffle=False,
                       callbacks=[self.tensorboard] if terminal_state else None)
        # Update target network counter every episode
        if terminal_state:
            self.target_update_counter += 1

        # If counter reaches set value, update target network with weights of main network
        if self.target_update_counter > UPDATE_TARGET_EVERY:
            self.target_model.set_weights(self.model.get_weights())
            # a = self.model.get_weights()
            # print(a)
            self.target_update_counter = 0
            self.save_replay_memory()
コード例 #12
0
 frame_count = int(cap.get(7))
 frame_count_len = len(str(frame_count))
 # Setup
 if 'Interpolator' not in locals():
     if args['empty_cache']:
         os.environ['CUDA_EMPTY_CACHE'] = '1'
     if args['model_path'] == 'default':  # 模型路径
         model_path = f"{args['algorithm']}/{model_path[args['algorithm']]}"
     # Model checking
     if not os.path.exists(model_path):
         print(f"Model {model_path} doesn't exist, exiting")
         exit(1)
     sys.path.append(f"{os.path.abspath(args['algorithm'])}")
     from interpolator import Interpolator
 # Interpolate
 interpolator = Interpolator(model_path, sf, int(cap.get(4)), int(cap.get(3)), batch_size=1,
                             save_which=1, net_name='DAIN_slowmotion', channel=3)
 timer = 0
 loss_fn = mse()
 start_time = time.time()
 batch = [cap.read()[1]]
 batch_count = frame_count//sf-1
 for i in range(batch_count):
     gt = []
     for f in [cap.read() for _ in range(sf-1)]:
         if f[0]:
             gt.append(f[1])
         else:
             break
     f = cap.read()
     if f[0]:
         del batch[:-1]