Beispiel #1
0
def commit_allocations(request):
    if request.is_ajax():
        if request.method == 'POST':
            user = request.user
            question_ids = request.POST.getlist('question_ids[]', False)
            allocs = request.POST.getlist('allocs[]', False)
            batch_id = request.POST.get('batch_id', False)
            price = request.POST.get('price', False)
            domain_id = None
            response = dict()
            if not all([allocs, question_ids, batch_id, price]):
                response['status'] = 'failure'
                response['msg'] = 'Missing inputs.'
                return dict_to_json_response(response)
            if len(question_ids) != len(allocs):
                response['status'] = 'failure'
                response['msg'] = 'Numbers of questions and allocations do not match.'
                return dict_to_json_response(response)
            batch = Batch.objects.get(pk=int(batch_id))
            try:
                user.pay_out(batch, float(price))
            except InsufficientFundsException as e:
                response['status'] = 'failure'
                response['msg'] = str(e)  # TODO: change this.
                return dict_to_json_response(response)
            for x in range(len(question_ids)):
                q = BaseQuestion.objects.get(pk=int(question_ids[x]))
                if domain_id is None:
                    domain_id = q.domain_id
                for uid in allocs[x].split(','):
                    assn = Assignment()
                    assn.answerer = User.objects.get(pk=int(uid))
                    assn.question = q
                    assn.agreed_price = get_overview_record(assn.answerer, q.domain)['price']
                    assn.complete = False
                    assn.save()
                    # remove after initial demo
                    profile = assn.answerer.get_profile()
                    profile.has_been_assigned = True
                    profile.save()
            batch.is_allocated = True
            batch.save()
            # TODO: get rid of this.
            assert domain_id is not None
            if settings.LOG_MARKET_STATS:
                log_market_stats(domain_id)
            if settings.DYNPRICING:
                # update prices in level records
                dp.update_prices()
                # update prices in allocation stems
                allc.update_prices(domain_id)
            response['status'] = 'success'
            response['msg'] = 'Committed successfully. Redirecting...'
            return dict_to_json_response(response)
Beispiel #2
0
def do_batch(reprice=False):
    batch_mu       = CONFIG['batch_size_mean']
    batch_sigma    = CONFIG['batch_size_stddev']
    min_price_prob = CONFIG['min_price_prob']
    conf_mu        = CONFIG['confidence_req_mean']
    conf_sigma     = CONFIG['confidence_req_stddev']
    price_mu       = CONFIG['max_price_mean']
    price_sigma    = CONFIG['max_price_stddev']
#    if random.random() < min_price_prob:
#        method = 'min_price'
#        crit = sigmoid(random.gauss(conf_mu, conf_sigma))
#    else:
#        method = 'max_conf'
#        crit = random.gauss(price_mu, price_sigma)
    batch_size = int(max([1, math.ceil(random.gauss(batch_mu, batch_sigma))]))
    # get answerers
    assgns = db.create_assignments(DOMAIN.id, .9, batch_size, 'min_price')
    if not assgns:
        print "failing over to max_conf"
        assgns = db.create_assignments(DOMAIN.id, 10000, batch_size, 'max_conf')
        if not assgns:
            return 
    # create questions
    questions = {}
    for x in range(batch_size):
        q = NRTrainingQuestion(asker=ASKER, 
                               domain=DOMAIN,
                               question_type=TYPE)
        q.save()
        true = NRTrainingChoice(question=q)
        true.save()
        false = NRTrainingChoice(question=q)
        false.save()
        assgn = assgns[x]
        ALLOCATIONS.append(assgn.get_dict())
        for u in assgn.members:
            keymatch = lambda z: z.id == u
            user_rec = filter(keymatch, USERS.keys())[0]
            user_acc = USERS[user_rec]
            a = Assignment(answerer=user_rec, question=q, completed=True)
            a.save()
            chosen = false
            if random.random() <= user_acc:
                chosen = true
            answer = NRTrainingAnswer(answerer=user_rec,
                                      question=q,
                                      confidence=1.,
                                      authority=1.)
            answer.save()
            review = NRTrainingReview(reviewer=ASKER,
                                      answer=answer,
                                      is_correct=(chosen == true),
                                      confidence=1.,
                                      authority=1.)
            review.save()
    if reprice:
        dp.update_prices()
        allocs.update_prices(DOMAIN.id)
    db.log_market_stats(DOMAIN.id)
    take_system_snapshot()    
    return True