Exemplo n.º 1
0
def optimize(in_file, out_file):
  proggy = json.loads(in_file.read())
  proggy = opt.optimize(proggy)
  json.dump(proggy, out_file)
Exemplo n.º 2
0
                #                factor1_sig = np.sqrt(factor_df[(factor1, factor1)].fillna(0).ix[pd.to_datetime(dayname)])
                #               factor2_sig = np.sqrt(factor_df[(factor2, factor2)].fillna(0).ix[pd.to_datetime(dayname)])
                #                print "Factor Correlation {}, {}: {}".format(factor1, factor2, factor_cov/(factor1_sig*factor2_sig))
            except:
                #                print "No cov found for {} {}".format(factor1, factor2)
                factor_cov = 0

            opt.g_fcov[find1, find2] = factor_cov * horizon
            opt.g_fcov[find2, find1] = factor_cov * horizon

            find2 += 1
        find1 += 1

    try:
        (target, dutil, eslip, dmu, dsrisk, dfrisk, costs,
         dutil2) = opt.optimize()
    except:
        date_group.to_csv("problem.csv")
        raise

    optresults_df = pd.DataFrame(index=date_group.index,
                                 columns=[
                                     'target', 'dutil', 'eslip', 'dmu',
                                     'dsrisk', 'dfrisk', 'costs', 'dutil2',
                                     'traded'
                                 ])
    optresults_df['target'] = target
    optresults_df['dutil'] = dutil
    optresults_df['eslip'] = eslip
    optresults_df['dmu'] = dmu
    optresults_df['dsrisk'] = dsrisk
Exemplo n.º 3
0
  def on_run(self):
    start = time.time()

    # Construct full distance map
    positions = {self.namesToIDs[task.name] : task.startPosition() for task in self.tasks}
    fullDistanceMap = opt.FullDistanceMap()
    for taskInd, task in enumerate(self.tasks):
      distanceMap = opt.DistanceMap()
      for otherInd, other in enumerate(self.tasks):
        distanceMap[otherInd] = numpy.linalg.norm(positions[otherInd] - positions[taskInd])
      fullDistanceMap[taskInd] = distanceMap

    # Construct optimizable tasks
    possibleTasks = set()
    optimizableTaskList = opt.OptimizableTaskList()
    modes = {self.namesToIDs[task.name] : self.instances[task.name].possibleModes() for task in self.tasks}
    for taskInd, task in enumerate(self.tasks):
      if not self.taskExecutable[task.name]:
        continue
      modeList = opt.ModeList()
      for modeInd, mode in enumerate(modes[taskInd]):
        modeList.append(opt.Mode(modeInd, mode.expectedPoints, mode.expectedTime))
      if len(modes[taskInd]) > 0:
        possibleTasks.add(self.idsToNames[taskInd])
        optimizableTaskList.append(opt.OptimizableTask(taskInd, modeList))

    # Construct topological restrictions
    topologicalRestrictionList = opt.TopologicalRestrictionList()
    for restriction in self.restrictions:
      if restriction.beforeTask in possibleTasks: # TODO FIXME better way, this doesn't really check completion status?
        topologicalRestrictionList.append(opt.TopologicalRestriction(self.namesToIDs[restriction.beforeTask], self.namesToIDs[restriction.afterTask]))

    remainingTime = (60. * 15) - (time.time() - self.initialization)
    self.logv('Remaining time: {}s'.format(remainingTime))

    res = opt.optimize(fullDistanceMap, remainingTime, optimizableTaskList, topologicalRestrictionList)

    end = time.time()
    self.logv('Generated, validated, and scored possible execution plans in {} seconds.'.format(end - start))

    if len(res) > 0:
      plan = []
      for val in res:
        plan.append(TaskPlan(self.idsToNames[val.id()], modes[val.id()][val.mode().id()]))
      if self.lastPlan is None or not equivalent(plan, self.lastPlan):
        self.logi('New optimal execution plan: {}.'.format(prettify(plan)))
      self.lastPlan = plan
      taskPlan = plan[0]
      if taskPlan.taskName != self.lastTask or taskPlan.mode.name != self.lastMode:
          self.instances[taskPlan.taskName] = self.tasks[self.namesToIDs[taskPlan.taskName]].cls()
          self.logi('Reinstantiated task: {} due to reason: switched.'.format(taskPlan.taskName))
          self.logi('Switched to: task {} with mode {}.'.format(colored(taskPlan.taskName, 'green', attrs = ['bold']), colored(taskPlan.mode.name, 'cyan', attrs = ['bold'])))
      self.lastTask = taskPlan.taskName
      self.lastMode = taskPlan.mode.name

      assertModules(self.instances[taskPlan.taskName].desiredModules(), self.logi)
      try: 
        execute(self.instances[taskPlan.taskName], taskPlan)
      except Exception as e:
        self.namesToExceptionCounts[taskPlan.taskName] += 1
        if self.namesToExceptionCounts[taskPlan.taskName] < self.maxExceptionCount:
          self.logw('Task {} threw exception: {}! Exception {} of {} before that task is killed!'.format(taskPlan.taskName, \
            e, self.namesToExceptionCounts[taskPlan.taskName], self.maxExceptionCount))
        else:
          self.loge('Task {} threw exception: {}! Task has reached exception threshold, will no longer be attempted!'.format( \
            taskPlan.taskName, e))
          self.taskExecutable[taskPlan.taskName] = False

    else:
      self.finish()
Exemplo n.º 4
0
                factor_cov = factor_df[(factor1, factor2)].fillna(0).ix[pd.to_datetime(dayname)]
                #                factor1_sig = np.sqrt(factor_df[(factor1, factor1)].fillna(0).ix[pd.to_datetime(dayname)])
                #               factor2_sig = np.sqrt(factor_df[(factor2, factor2)].fillna(0).ix[pd.to_datetime(dayname)])
                #                print "Factor Correlation {}, {}: {}".format(factor1, factor2, factor_cov/(factor1_sig*factor2_sig))
            except:
                #                print "No cov found for {} {}".format(factor1, factor2)
                factor_cov = 0

            opt.g_fcov[ find1, find2 ] = factor_cov * horizon
            opt.g_fcov[ find2, find1 ] = factor_cov * horizon

            find2 += 1
        find1 += 1
        
    try:
        (target, dutil, eslip, dmu, dsrisk, dfrisk, costs, dutil2) = opt.optimize()
    except:
        date_group.to_csv("problem.csv")
        raise

    optresults_df = pd.DataFrame(index=date_group.index, columns=['target', 'dutil', 'eslip', 'dmu', 'dsrisk', 'dfrisk', 'costs', 'dutil2', 'traded'])
    optresults_df['target'] = target
    optresults_df['dutil'] = dutil
    optresults_df['eslip'] = eslip
    optresults_df['dmu'] = dmu
    optresults_df['dsrisk'] = dsrisk
    optresults_df['dfrisk'] = dfrisk
    optresults_df['costs'] = costs
    optresults_df['dutil2'] = dutil2
    
    # pnl_df.ix[ date_group.index, 'target'] = optresults_df['target']
Exemplo n.º 5
0
                    factor1, factor2)].fillna(0).ix[pd.to_datetime(dayname)]
                #                factor1_sig = np.sqrt(factor_df[(factor1, factor1)].fillna(0).ix[pd.to_datetime(dayname)])
                #               factor2_sig = np.sqrt(factor_df[(factor2, factor2)].fillna(0).ix[pd.to_datetime(dayname)])
                #                print("Factor Correlation {}, {}: {}".format(factor1, factor2, factor_cov/(factor1_sig*factor2_sig)))
            except:
                #                print("No cov found for {} {}".format(factor1, factor2))
                factor_cov = 0

            opt.g_fcov[find1, find2] = factor_cov * horizon
            opt.g_fcov[find2, find1] = factor_cov * horizon

            find2 += 1
        find1 += 1
    try:
        (target, dutil, eslip, dmu, dsrisk, dfrisk, costs, dutil2, vol,
         price) = opt.optimize()
    except:
        date_group.to_csv("problem.csv")
        raise

    optresults_df = pd.DataFrame(index=date_group.index,
                                 columns=[
                                     'target', 'dutil', 'eslip', 'dmu',
                                     'dsrisk', 'dfrisk', 'costs', 'dutil2',
                                     'traded'
                                 ])
    optresults_df['target'] = target
    optresults_df['dutil'] = dutil
    optresults_df['eslip'] = eslip
    optresults_df['dmu'] = dmu
    optresults_df['dsrisk'] = dsrisk
Exemplo n.º 6
0
            try:
                factor_cov = factor_df[(factor1, factor2)].fillna(0).ix[pd.to_datetime(dayname)]
                #                factor1_sig = np.sqrt(factor_df[(factor1, factor1)].fillna(0).ix[pd.to_datetime(dayname)])
                #               factor2_sig = np.sqrt(factor_df[(factor2, factor2)].fillna(0).ix[pd.to_datetime(dayname)])
                #                print("Factor Correlation {}, {}: {}".format(factor1, factor2, factor_cov/(factor1_sig*factor2_sig)))
            except:
                #                print("No cov found for {} {}".format(factor1, factor2))
                factor_cov = 0

            opt.g_fcov[find1, find2] = factor_cov * horizon
            opt.g_fcov[find2, find1] = factor_cov * horizon

            find2 += 1
        find1 += 1
    try:
        (target, dutil, eslip, dmu, dsrisk, dfrisk, costs, dutil2, vol, price) = opt.optimize()
    except:
        date_group.to_csv("problem.csv")
        raise

    optresults_df = pd.DataFrame(index=date_group.index,
                                 columns=['target', 'dutil', 'eslip', 'dmu', 'dsrisk', 'dfrisk', 'costs', 'dutil2',
                                          'traded'])
    optresults_df['target'] = target
    optresults_df['dutil'] = dutil
    optresults_df['eslip'] = eslip
    optresults_df['dmu'] = dmu
    optresults_df['dsrisk'] = dsrisk
    optresults_df['dfrisk'] = dfrisk
    optresults_df['costs'] = costs
    optresults_df['dutil2'] = dutil2