Forum archive 2000-2006

Thomas R. Shemanske - Answer Evaluators & Hashes

Thomas R. Shemanske - Answer Evaluators & Hashes

by Arnold Pizer -
Number of replies: 0
inactiveTopicAnswer Evaluators & Hashes topic started 1/10/2002; 4:18:38 PM
last post 1/11/2002; 9:18:55 AM
userThomas R. Shemanske - Answer Evaluators & Hashes  blueArrow
1/10/2002; 4:18:38 PM (reads: 1298, responses: 3)
Last year, Zig Fiedorowicz offered some code to allow one to examine a student's answers before deciding how to process them. For example, a solution to a differential equation may be of the form c_1 f_1 + c_2 f_2, and the students are asked to enter functions f_1 and f_2. Since no order was specified to list the functions f_1 and f_2, you have to do a little detective work before testing their answers for correctness.

Below is some parred down code which will function on WeBWork 1.6, but which fails under WeBWorK 1.7.

The reason it fails appears to be due to a change in num_cmp. Under WW1.6, the statement ($ans_eval1) = num_cmp(exp(2 * 1)); would produce the value "CODE(0x8967770)" for $ans_eval1 Under WW 1.7, the same statement produces "AnswerEvaluator=HASH(0x8a7d500)"

This explains why the code below fails. Perhaps someone can suggest a way around this difficulty. I do not know how to extract any information from this answer evaluator. I have stared at the "Answer hash datatype specification", but haven't a clue how to connect it to named answers.

Thanks in advance.

Tom Shemanske

 

 

DOCUMENT();
loadMacros(PG.pl,
PGbasicmacros.pl,
PGchoicemacros.pl,
PGanswermacros.pl,
PGauxiliaryFunctions.pl,
);

TEXT(beginproblem());



BEGIN_TEXT
$BR
Enter the functions ( e^{2t} ) and (e^{3t}) in the blanks below



$BR



(f_1(t) = ) {NAMED_ANS_RULE(first_fcn,25)}
and (f_2(t) = ) {NAMED_ANS_RULE(second_fcn,25)}



$PAR
END_TEXT



## Grab the first answer entered by student
$firstAnswer = $inputs_ref->{first_fcn};



## An alternate means of grabbing the function
#$firstAnswer = ${$main::inputs_ref}{first_fcn} if defined(${$main::inputs_ref}{first_fcn});



## Make sure it is defined
$firstAnswer = '' unless defined($firstAnswer);



## Globally substitute (1) for t in $firstAnswer
$firstAnswer =~ s/t/(1)/g;



## Answer evaluator for the first function at t = 1
($ans_eval1) = num_cmp(exp(2 * 1));



#My mucking around trying to fix things
#$rh_ans_hash = new AnswerHash;



## Check first answer against "correct" first answer
$rh_ans_hash = &$ans_eval1($firstAnswer);



## Check answers accordingly
if ( $rh_ans_hash->{score} == 1 )
{
NAMED_ANS( first_fcn, fun_cmp("exp(2*t)", vars=>['t']) );
NAMED_ANS( second_fcn, fun_cmp("exp(3*t)", vars=>['t']) );
}
else
{
NAMED_ANS( first_fcn, fun_cmp("exp(3*t)", vars=>['t']) );
NAMED_ANS( second_fcn, fun_cmp("exp(2*t)", vars=>['t']) );
}



ENDDOCUMENT();

The error one gets is "Not a CODE reference at (eval 54) line 42."

in reference to ($ans_eval1) = num_cmp(exp(2 * 1));

which makes sense since $ans_eval1 does not evaluate to CODE but to Answer_Evaluator as mentioned above.

<| Post or View Comments |>


userZbigniew Fiedorowicz - Re: Answer Evaluators & Hashes  blueArrow
1/10/2002; 9:02:42 PM (reads: 1509, responses: 2)
Hi Tom,

I think Mike answered this some time ago. Old style answer evaluators returned function pointers, new style answer evaluators return objects which are supposed to be called using the appropriate method for the object. Under 1.6, num_cmp() returned an old style evaluator while function_cmp() returned a new style evaluator. I guess under 1.7 all answer evaluators are new style.

The following code should take care of the situation:

 

sub calleval {
my ($ans_evaluator,$student_answer) = @_;
my $rh_ans_hash = "";
if ( ref($ans_evaluator) eq 'AnswerEvaluator' ) { # new style
$rh_ans_hash = $ans_evaluator->evaluate($student_answer);
} elsif (ref($ans_evaluator) eq 'CODE' ) { #old style
$rh_ans_hash = &$ans_evaluator($student_answer);
} else {
warn "There is a problem using the answer evaluator";
}
return $rh_ans_hash;
}



.........



$rh_ans_hash = &calleval($ans_eval1,$firstAnswer);

Zig

<| Post or View Comments |>


userThomas R. Shemanske - Re: Answer Evaluators & Hashes  blueArrow
1/11/2002; 9:06:25 AM (reads: 1760, responses: 0)
Thanks Zig!

I was pretty sure the issue had come up on the list, but I could not find it.

By the way, is the "evaluate" function you use in $rh_ans_hash = $ans_evaluator->evaluate($student_answer);

a standard Perl construct or a PG construct? Not knowing this was the largest obstacle to sorting this issue out.

Thanks again.

<| Post or View Comments |>