Beispiel #1
0
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%d",&p[i]);
	print_cut(p,n);
	for(int i=0;i<=n;i++)
	  memo[i] = MIN;
	print_bottom_cut(p,n);
	printf("%d\n",memo[n]);
	return 0;
}
Beispiel #2
0
/***********************************************************************\  
 ** If it has been decided that the incumbent solution should change, 
 ** several values must be updated, including the incumbent X, the 
 ** incumbent cut, the estimate at the incumbent and the standard 
 ** deviation of the estimate, as well as the last_update counter.
 ** This function performs these updates.  It assumes that the candidate 
 ** solution becomes the incumbent solution, with a height corresponding 
 ** to the _est_ parameter.
 \***********************************************************************/
void new_incumbent(prob_type *p, cell_type *c, soln_type *s, double est)
{
	vector X;
	double val, stdev;
	int obs, idx;
	int count;
	i_type i;

#ifdef TRACE
	printf("Inside new_incumbent\n");
#endif 

	/* Copy over information about the new incumbent */
	copy_arr(s->incumb_x, s->candid_x, p->num->mast_cols)
	s->incumb_cut = c->cuts->cnt - 1;
	s->incumb_est = est;
	s->last_update = c->k;
	s->incumb_k = c->k;

	/* 
	 ** The cut consists of an average of many pi X omega products.  We
	 ** use the mean and standard deviation of this collection of products
	 ** in the check for optimality, so they are updated here.  We loop 
	 ** through the istar indices of new incumbent cut to find the stdev.
	 */
	count = 0;
	stdev = 0.0;
	X = s->incumb_x;

	if (c->cuts->cnt > 0)
	{
		for (obs = 0; obs < c->cuts->val[s->incumb_cut]->omega_cnt; obs++)
			if (valid_omega_idx(s->omega, obs))
			{
				/* Find the pi from the cut's argmax for this observation */
				i.sigma = c->cuts->val[s->incumb_cut]->istar[obs];
				i.delta = c->sigma->lamb[i.sigma];

				/*
				 ** Couldn't this be done only in the optimality check, so we 
				 ** don't do it every time we change our minds about the incumbent ?
				 ** Which happens more often -- an optimality check or a change of
				 ** the incumbent x?
				 ** What about when the the incumbent cut is updated, but X is not?
				 */

				/* Calculate the height for this observation and dual vector */
				val = c->sigma->val[i.sigma].R + s->delta->val[i.delta][obs].R;
				for (idx = 0; idx < p->num->nz_cols; idx++)
					val -= c->sigma->val[i.sigma].T[idx]
							* X[c->sigma->col[idx]];
				for (idx = 0; idx < p->num->rv_cols; idx++)
					val -= s->delta->val[i.delta][obs].T[idx]
							* X[s->delta->col[idx]];

				stdev += s->omega->weight[obs] * SQR(s->incumb_est - val);
				count += s->omega->weight[obs];
			}
		s->incumb_stdev = sqrt(stdev / (double) count);
	}

	s->incumbent_change = TRUE;
#ifdef DEBUG
	printf("\nMade a new incumbent, since it qualified");
	print_cut(c->cuts, p->num, s->incumb_cut);
#endif

}