Exemplo n.º 1
0
	MapIndex* GetSelection() const
	{
		MapIndex*const* map = m_maps.Find(m_currentlySelectedId);
		if(map)
			return *map;
		return nullptr;
	}
Exemplo n.º 2
0
	DifficultyIndex* GetSelectedDifficulty() const
	{
		MapIndex*const* map = m_maps.Find(m_currentlySelectedId);
		if(map)
		{
			return map[0]->difficulties[m_currentlySelectedDiff];
		}
		return nullptr;
	}
Exemplo n.º 3
0
	void SelectDifficulty(int32 newDiff)
	{
		m_currentSelection->SetSelectedDifficulty(newDiff);
		m_currentlySelectedDiff = newDiff;

		MapIndex** map = m_maps.Find(m_currentlySelectedId);
		if(map)
		{
			OnDifficultySelected.Call(map[0]->difficulties[m_currentlySelectedDiff]);
		}
	}
//*******************************************************//
// Read in the expression and process it and write it 
// to the file pointed by f_output.
// 1. Parameters should be replaced by its value. If the 
//    value is less than 0, the value should be wrapped 
//    by '( )'.
// 2. States such as x.delta should be replaced by its index
//    such as x_0.
// 3. Math symbols (+-*/()<>=) should be write to the file
//    directly and add space before and after it.
// 4. Variables that are not defined as parameters are write
//    directly into the file.
// 5. expression should not include the ';' at the end
// Return nothing.
//*******************************************************//
void writeToFile(FILE *f_output, char *expression, 
				 Map param_map, char inputs[10][20], char states[10][20], char measures[10][20],
				 int inputs_num, int states_num, int measures_num,
				 char *x_old, char *x_new, char *u, char *ubar, char *weight, char *y)
{
	int len = strlen(expression);
	int temp_len;
	int isFound;
	int index;
	char *ptr = expression;
	char *temp_ptr = (char*)malloc(len+1);
	char *temp_ptr2 = (char*)malloc(len+1);
	char *token;
	double value;
	char *major, *minor;
	while(ptr != expression + len){
		// Current token is a whitespace, skip it
		if (*ptr == ' ' || *ptr == '\t' || *ptr == '.') ptr ++; 

		// I. Current postion is '<' or '>', check if there is an '=' following
		else if(*ptr == '>' || *ptr == '<'){
			fprintf(f_output, "%c", *ptr);
			ptr ++;
			if(*ptr == '='){
				fprintf(f_output, "= ");
				ptr ++;
			}
			else
				fprintf(f_output, " ");
		}

		// II. Current postion is a math symbol
		else if(isMathSymbol(ptr)){
			fprintf(f_output, "%c ", *ptr);
			ptr ++;
		}

		// III. Current position is a digit, write the number to the file directly
		else if(isDigit(ptr)){
			strcpy(temp_ptr, ptr);
			token = strtok(temp_ptr, " ,+-*/()<>=\r\n\t"); // Get the string which represents a number
			// Dealing with situations like 1.0e-10
			if (token[strlen(token)-1] == 'e'){
				temp_len = strlen(token);
				ptr += temp_len; // *ptr should be '+' or '-'
				if(*ptr == '+' || *ptr == '-'){
					token[temp_len] = *ptr;
					token[temp_len +1] = '\0';
					ptr ++;
				}
				strcpy(temp_ptr2, ptr);
				strcat(token, strtok(temp_ptr2, " ,+-*/()=\r\n\t"));
				ptr += strlen(token) - temp_len - 1;
			}
			else
				ptr += strlen(token);
			fprintf(f_output, "%s ", token);
		}

		// IV. Current token is a variable or sqrt or exp or pow
		//     1. Parameter
		//     2. New variable
		//     3. Control signal or state variables
		//     4. Random number
		//	   5. sqrt
		else if(isLetter(ptr)){
			strcpy(temp_ptr, ptr);
			token = strtok(temp_ptr, " ,+-*/()\r\n\t"); // Get the string which represents a variable's name
			ptr += strlen(token);
			value = param_map.Find(token, &isFound);
			// The variable is a defined parameter
			if(isFound == 1){
				if(value < 0.0)
					fprintf(f_output, "(%lf) ", value);
				else 
					fprintf(f_output, "%lf ", value);
			}
			// The variable is randn(1)
			else if(strcmp(token, "randn") == 0){
				fprintf(f_output, "RANDN ");
				ptr += 3; // skip (1)
			}
			// The variable is sqrt or exp
			else if(strcmp(token, "sqrt") == 0 || strcmp(token, "exp") == 0 || strcmp(token, "pow") == 0)
				fprintf(f_output, "%s", token);
			// The variable is weight
			else if(strcmp(token, weight) == 0)
				fprintf(f_output, "WEIGHT ");
			// token is a new variable or control signal or states
			else {
				strcpy(temp_ptr2, token);
				major = strtok(temp_ptr2, ".\r\n\t");
				// token is a new variable
				if(strlen(token) == strlen(major))
					fprintf(f_output, "%s ", token);
				// token is control signal or state or measurement
				else {
					minor = token + strlen(major) + 1;
					// The old state
					if(strcmp(major, x_old) == 0){
						fprintf(f_output, "xold_");
						index = findIndex(minor, states, states_num);
						if(index == -1) cout<<"Undefined old state: "<<minor<<endl;
						else fprintf(f_output, "%d ", index);
					}
					// The new state
					else if(strcmp(major, x_new) == 0){
						fprintf(f_output, "xnew_");
						index = findIndex(minor, states, states_num);
						if(index == -1) cout<<"Undefined new state: "<<minor<<endl;
						else fprintf(f_output, "%d ", index);
					}
					// The control signal
					else if (strcmp(major, u) == 0){
						fprintf(f_output, "u_");
						index = findIndex(minor, inputs, inputs_num);
						if(index == -1) cout<<"Undefined control signal: "<<minor<<endl;
						else fprintf(f_output, "%d ", index);
					}
					// The lookahead control signal
					else if (strcmp(major, ubar) == 0){
						fprintf(f_output, "ubar_");
						index = findIndex(minor, inputs, inputs_num);
						if(index == -1) cout<<"Undefined control signal: "<<minor<<endl;
						else fprintf(f_output, "%d ", index);
					}
					// The prediction
					else if (strcmp(major, "prediction") == 0){
						fprintf(f_output, "prediction_");
						index = findIndex(minor, measures, measures_num);
						if(index == -1) cout<<"Undefined measurements: "<<minor<<endl;
						else fprintf(f_output, "%d ", index);
					}
					// The observation
					else if (strcmp(major, y) == 0){
						fprintf(f_output, "y_");
						index = findIndex(minor, measures, measures_num);
						if(index == -1) cout<<"Undefined measurements: "<<minor<<endl;
						else fprintf(f_output, "%d ", index);
					}
				}
			}
		}

	}

	free(temp_ptr);
	free(temp_ptr2);
}
//*******************************************************//
// Evaluate the value of expression.
// 1. If a double occurs, push it into the value stack
// 2. If a parameter occurs, find its value in param_map
//    and push its value in the value stack.
// 3. If an operator occurs, push it into the op stack
//    or do some calculations according to the predecence
//    of the operator and the op on top of the stack
// 4. If a '(' or ')' occurs, push it into the op stack
//    or do some calculations.
// Return the value of the expression
//*******************************************************//
double evaluate(Map param_map, char* expression)
{
	Stack<double> values_stack;
	Stack<char> ops_stack;
	char *ptr = expression;
	char *token;
	double value, val1, val2;
	int len = strlen(expression); // length of the expression
	int temp_len;
	int isFound;
	char *temp_ptr = (char*)malloc(len+1);
	char *temp_ptr2 = (char*)malloc(len+1);

	// If the expression starts with a '-', add 0.0 in front of the expression
	if(*ptr == '-'){
		values_stack.Push(0.0);
		ops_stack.Push('-');
		ptr ++;
	}
	while(ptr != expression + len){
		// Current token is a whitespace, skip it
		if(*ptr == ' ') ptr ++;

		// I. Current token is a number, push it to stack for numbers
		else if(isDigit(ptr)){
			strcpy(temp_ptr, ptr);
			token = strtok(temp_ptr, " +-*/()\r\n\t"); // Get the string which represents a number
			// Dealing with situations like 1.0e-10
			if (token[strlen(token)-1] == 'e'){
				temp_len = strlen(token);
				ptr += temp_len; // *ptr should be '+' or '-'
				if(*ptr == '+' || *ptr == '-'){
					token[temp_len] = *ptr;
					token[temp_len +1] = '\0';
					ptr ++;
				}
				strcpy(temp_ptr2, ptr);
				strcat(token, strtok(temp_ptr2, " +-*/()\r\n\t"));
				ptr += strlen(token) - temp_len - 1;
			}
			else
				ptr += strlen(token);
			values_stack.Push(strtod(token,NULL));
		}

		// II. Current token is a variable, check if it is defined. 
		//     If so, push it to stack for numbers
		else if(isLetter(ptr)){
			strcpy(temp_ptr, ptr);
			token = strtok(temp_ptr, " +-*/()\r\n\t"); // Get the string which represents a variable's name
			ptr += strlen(token);
			value = param_map.Find(token, &isFound);
			if(isFound == 0){ // The parameter is not defined yet
				cout<<"Undefined parameter : "<<token<<endl;
				exit(1);
			}
			values_stack.Push(value);
		}

		// III. Current token is an opening brace, push it to 'ops'
		else if(*ptr == '('){
			ops_stack.Push(*ptr);
			ptr ++;
		}

		// IV. Closing brace encountered, solve entire brace
		else if(*ptr == ')'){
			while(ops_stack.Peek(0) != '(')
				values_stack.Push(applyOp(ops_stack.Pop(), values_stack.Pop(), values_stack.Pop()));
			ops_stack.Pop();
			ptr ++;
		}
		// V. Current token is an operator.
        	else if (isOperator(ptr))
        	{
            		// While top of 'ops' has same or greater precedence to current
           		// token, which is an operator. Apply operator on top of 'ops'
            		// to top two elements in values stack
            		while (!ops_stack.Empty() && hasPrecedence(*ptr, ops_stack.Peek(0))){
				val1 = values_stack.Pop();
				val2 = values_stack.Pop();
                		values_stack.Push(applyOp(ops_stack.Pop(), val1, val2));
			}
            		// Push current token to 'ops'.
            		ops_stack.Push(*ptr);
			ptr ++;
        	}
	}
	// Entire expression has been parsed at this point, apply remaining
    // ops to remaining values
	while (!ops_stack.Empty()){
		val1 = values_stack.Pop();
		val2 = values_stack.Pop();
		values_stack.Push(applyOp(ops_stack.Pop(), val1, val2));
	}
	free(temp_ptr);
	free(temp_ptr2);

	return values_stack.Pop();
}