Exemplo n.º 1
0
    void RunExperiments(ifstream &input, ofstream &output) {
        // Read input file
        input.seekg(0, ios::end);
        size_t input_length = input.tellg();
        input.seekg(0, ios::beg);
        char * input_content = new char[input_length+1];
        input.read(input_content, input_length);
        input_content[input_length] = 0;

        map<string, fnptr> &registeredExperiments = RegisterExperiment("", NULL);
        cout << "Registered experiments: " << endl;
        for(map<string, fnptr>::iterator it = registeredExperiments.begin(); it != registeredExperiments.end(); ++it)
            cout << "\t- " << it->first << endl;

        // Create JSON object
        JSONValue *root = JSON::Parse(input_content);
        delete [] input_content;

        // Iterate over each object
        if (!root->IsObject()) {
            cerr << "Error, the input file does not contain objects." << endl;
            return;
        }

        cout << endl;
        cout << "* Running Experiments" << endl;
        output << "{" << endl;
        // This is a vector of <sdt::wstring, JSONValue>
        JSONObject objects = root->AsObject();
      
        Timer overall_time;        
        for(JSONObject::iterator it = objects.begin(); it != objects.end(); ++it) {
            string tmp((it->first).begin(), (it->first).end());
            output << "experiment_name: \"" << tmp << "\"" << endl;
            if (registeredExperiments.count(tmp) == 0) {
                cout << "\tExperiment " << tmp << " is not registered." << endl;
                output << "result: \"error\"" << endl;
            } else {
                cout << "\tRunning experiment " << tmp << endl;
                Timer experiment_time;
                wstring result = registeredExperiments[tmp](it->second->Stringify());

                experiment_time.stop();
                
                string tmp_result(result.begin(), result.end());
                output << "result: " << tmp_result << endl;
                output << "experiment_time:" << experiment_time.elapsedTime() << endl;
            }
        }
        overall_time.stop();
        output << "overall_time:" << overall_time.elapsedTime() << endl;
        output << GetSystemInformation();
        output << "}" << endl;
    }
 /**
  * @param nums1 an integer array of length m with digits 0-9
  * @param nums2 an integer array of length n with digits 0-9
  * @param k an integer and k <= m + n
  * @return an integer array
  */
 vector<int> maxNumber(vector<int>& nums1, vector<int>& nums2, int k) {
     // Write your code here
     vector<int> result(k), tmp_result(k);
     vector<vector<int> > max_num1(k + 1), max_num2(k + 1);
     genDP(nums1, max_num1, k);  
     genDP(nums2, max_num2, k);
     for(int i = 0; i <= k; i++) {
         if(max_num1[i].size() + max_num2[k - i].size() < k)
             continue;
         merge(tmp_result, max_num1, max_num2, i, k);
         if(result.empty() || smaller(result, tmp_result))
             result = tmp_result;
     }
     return result;        
 }
Exemplo n.º 3
0
void
datawidth::entry()
{
  sc_unsigned   tmp_a (in1_width);
  sc_unsigned   tmp_b (in2_width);
  sc_unsigned   tmp_result (result_width);

  while (true) {
    
    // HANDSHAKING
    do { wait(); } while (ready != 1);

    // COMPUTATION
    tmp_a = in1.read();
    tmp_b = in2.read();
    tmp_result = tmp_a + tmp_b;

    // WRITE OUTPUT
    result.write(tmp_result);		// result = in1 + in2
    wait();
  }
}
Exemplo n.º 4
0
Indicator HKU_API AmaSpecial(const Block& block, KQuery query,
        Indicator ama) {
    Indicator result;
    StockManager& sm = StockManager::instance();

    //计算每日股票总数
    DatetimeList dateList = sm.getTradingCalendar(query, "SH");

    size_t dayTotal = dateList.size();
    if (dayTotal == 0) {
        result = PRICELIST(PriceList());
        result.name("POS");
        return result;
    }

    vector<size_t> numberPerDay(dayTotal);
    for (size_t i = 0; i < dayTotal; ++i) {
        numberPerDay[i] = 0;
        for (auto stk_iter = block.begin(); stk_iter != block.end(); ++stk_iter) {
            if (stk_iter->startDatetime() <= dateList[i]
                  && dateList[i] <= stk_iter->lastDatetime()) {
                numberPerDay[i]++;
            }
        }
    }

    vector<size_t> position(dayTotal);
    size_t discard = ama.discard();
    for (auto stk_iter = block.begin(); stk_iter != block.end(); ++stk_iter) {
        KData kdata = stk_iter->getKData(query);
        if (kdata.empty())
            continue;
        SignalPtr sg(SG_Single(ama));
        sg->setTO(kdata);
        bool isHold = false;
        size_t n_dis = 0;
        for (size_t i = 0; i < dayTotal; ++i) {
            if (isHold) {
                if (sg->shouldSell(dateList[i])) {
                    isHold = false;
                } else {
                    position[i]++;
                }

            } else {
                if (sg->shouldBuy(dateList[i])) {
                    position[i]++;
                    isHold = true;
                }
            }

            if (dateList[i] >= kdata[0].datetime) {
                if (n_dis < discard) {
                    n_dis++;
                    numberPerDay[i]--;
                }
            }

        }
    }

    PriceList tmp_result(dayTotal, Null<price_t>());
    for (auto i = discard; i < dayTotal; ++i) {
        tmp_result[i] = numberPerDay[i]
                  ? (double)position[i]/(double)numberPerDay[i] : 1.0;
    }

    result = PRICELIST(tmp_result);
    result.name("POS");
    return PRICELIST(result);
}