Difference between revisions of "AdaptiveParameters"

From WeBWorK_wiki
Jump to navigation Jump to search
m
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
  +
{{historical}}
  +
  +
<p style="font-size: 120%;font-weight:bold">This problem has been replaced with [https://openwebwork.github.io/pg-docs/sample-problems/problem-techniques/AdaptiveParameters.html a newer version of this problem]</p>
  +
 
<h2>Adaptive Parameters: PG Code Snippet</h2>
 
<h2>Adaptive Parameters: PG Code Snippet</h2>
   
Line 45: Line 49:
 
<tr valign="top">
 
<tr valign="top">
 
<td style="background-color:#eeddff;border:black 1px dashed;">
 
<td style="background-color:#eeddff;border:black 1px dashed;">
  +
<pre>
  +
ANS( $aSoln->cmp( checker => sub {
  +
my ( $correct, $student, $self ) = @_;
  +
# return 0 if ( $student == Formula(0) ); # see comments
  +
if ($self->{_filter_name} ne 'produce_equivalence_message') {
  +
my $context = Context()->copy;
  +
$context->flags->set(no_parameters=>0);
  +
$context->variables->add('C0'=>'Parameter');
  +
$student = Formula($context,$student);
  +
$correct = Formula($context,"C0 * e^x - 1");
  +
}
  +
return $correct == $student;
  +
}));
  +
</pre>
  +
<td style="background-color:#eeccff;padding:7px;">
  +
<p>
  +
Then in the answer, we define a [[CustomAnswerCheckers|custom answer checker]] that creates a copy of the Context in which an adaptive parameter is allowed, redefines the student and correct answers in that context, and checks that the answers match with the adaptive parameter.
  +
</p>
  +
<p>
  +
Again, note that if we just want to allow a solution to differ by an <em>additive constant</em> from the answer in the problem, we can use the existing MathObjects infrastructure, as discussed on the [[FormulasToConstants|formulas up to constants]] techniques page.
  +
</p>
  +
<p>
  +
Some history to help users who have run into trouble with older versions of this page. This page has been updated based on [https://webwork.maa.org/moodle/mod/forum/discuss.php?d=497 a forum post] to fix some issues with modified versions of the
  +
original recipe for a similar needs. The '''old''' code on this page was:
 
<pre>
 
<pre>
 
ANS( $aSoln->cmp( checker => sub {
 
ANS( $aSoln->cmp( checker => sub {
Line 57: Line 85:
 
}));
 
}));
 
</pre>
 
</pre>
<td style="background-color:#eeccff;padding:7px;">
 
  +
</p>
 
<p>
 
<p>
Then in the answer, we define a [[CustomAnswerCheckers|custom answer checker]] that creates a copy of the Context in which an adaptive parameter is allowed, redefines the student and correct answers in that context, and checks that the answers match with the adaptive parameter.
 
  +
However, in the setting where the correct answer is just a multiple of a standard answer without an additive constant we '''would expect''' code like the following to work:
  +
<pre>
  +
# HAS BUGS
  +
$aSoln = Compute("e^x");
  +
ANS( $aSoln->cmp( checker => sub {
  +
my ( $correct, $student, $self ) = @_;
  +
my $context = Context()->copy;
  +
$context->flags->set(no_parameters=>0);
  +
$context->variables->add('C0'=>'Parameter');
  +
$student = Formula($context,$student);
  +
$correct = Formula($context,"C0 ($correct)");
  +
return $correct == $student;
  +
}));
  +
</pre>
  +
but that code has 2 issues:
  +
<ul>
  +
<li>If "0" was entered as the answer then the following submission would report "Please inform your instructor that an error occurred while checking your answer at [PG]/lib/Value/AnswerChecker.pm line 252"</li>
  +
<li>Entering a scalar multiple of the prior answer (whether correct or now) would trigger the message "This answer is equivalent to the one you just submitted."</li>
  +
</ul>
  +
The solution to these issues (provided by Davide Cervone in the forum thread) is to use code like:
  +
<pre>
  +
$aSoln = Compute("e^x");
  +
ANS( $aSoln->cmp( checker => sub {
  +
my ( $correct, $student, $self ) = @_;
  +
return 0 if ( $student == Formula(0) );
  +
if ($self->{_filter_name} ne 'produce_equivalence_message') {
  +
my $context = Context()->copy;
  +
$context->flags->set(no_parameters=>0);
  +
$context->variables->add('C0'=>'Parameter');
  +
$student = Formula($context,$student);
  +
$correct = Formula($context,"C0 ($correct)");
  +
}
  +
return $correct == $student;
  +
}));
  +
</pre>
  +
which bypasses the adaptive parameter code for the 'produce_equivalence_message' filter, and also includes a test to avoid allowing 0 times the standard answer to be accepted. (See [https://webwork.maa.org/moodle/mod/forum/discuss.php?d=324 this forum post] for a mention of the test against 0.)
 
</p>
 
</p>
 
<p>
 
<p>
Again, note that if we just want to allow a solution to differ by an <em>additive constant</em> from the answer in the problem, we can use the existing MathObjects infrastructure, as discussed on the [[FormulasToConstants|formulas up to constants]] techniques page.
 
  +
Similar changes were made to the code on the left.
 
</p>
 
</p>
  +
  +
<p><strong>Warning</strong>:
  +
Using AnswerHints to check for <code>DNE</code> or <code>NONE</code> would cause problems with the adaptive paramater code,
  +
as these answers cannot be multiplied by the adaptive parameter, so would trigger the error message<br>
  +
<code>Please inform your instructor that an error occurred while checking your answer at [PG]/lib/Value/AnswerChecker.pm line 252</code>.
  +
<br>
  +
To fix this, you should handle cases where <code>correct</code> is <code>DNE</code> or <code>NONE</code> <strong>before</strong>
  +
it reaches the block where the parameter is added. For example, you can add something like
  +
<pre>
  +
if ( $correct == Compute("DNE") || $correct == Compute("NONE")) {
  +
# Should not get into the adaptive parameter code.
  +
return( $correct == $student );
  +
}
  +
</pre>
  +
near the top of the function.</p>
 
</td>
 
</td>
 
</tr>
 
</tr>
Line 70: Line 148:
 
[[IndexOfProblemTechniques|Problem Techniques Index]]
 
[[IndexOfProblemTechniques|Problem Techniques Index]]
 
</p>
 
</p>
  +
  +
[[Category:Problem_Techniques]]

Latest revision as of 16:41, 20 June 2023

This article has been retained as a historical document. It is not up-to-date and the formatting may be lacking. Use the information herein with caution.

This problem has been replaced with a newer version of this problem

Adaptive Parameters: PG Code Snippet

This code snippet shows the essential PG code to include adaptive parameters in a problem solution. Note that these are insertions, not a complete PG file. This code will have to be incorporated into the problem file on which you are working. This page is largely drawn from this discussion thread, which looks at one application.

Note that if we want to have a formula that is only unique up to a specified additive constant, we have the upToConstant option in the answer checker, as discussed in the formulas up to constants page.

Problem Techniques Index

PG problem file Explanation
$aSoln = Compute("e^x - 1");

In this case we work with the adaptive parameters only in the custom answer checker that we use for the problem, so no special set-up needs to be done in the problem set-up section of the PG file. Here we've defined $aSoln so that we have a MathObjects function to work with later.

BEGIN_TEXT
Find one solution to the differential equation
\[ \frac{dy}{dx} = y + 1. \]
\( y = \) \{ ans_rule(35) \}
END_TEXT

And the text section of the file is similarly "normal." The general solution to this differential equation is y = c ex - 1, but a student could also write it as something like y = ec ex - 1. We use the adaptive parameter to find the value of the constant.

ANS( $aSoln->cmp( checker => sub {
    my ( $correct, $student, $self ) = @_;
    # return 0 if ( $student == Formula(0) ); # see comments
    if ($self->{_filter_name} ne 'produce_equivalence_message') {
      my $context = Context()->copy;
      $context->flags->set(no_parameters=>0);
      $context->variables->add('C0'=>'Parameter');
      $student = Formula($context,$student);
      $correct = Formula($context,"C0 * e^x - 1");
    }
    return $correct == $student;
}));

Then in the answer, we define a custom answer checker that creates a copy of the Context in which an adaptive parameter is allowed, redefines the student and correct answers in that context, and checks that the answers match with the adaptive parameter.

Again, note that if we just want to allow a solution to differ by an additive constant from the answer in the problem, we can use the existing MathObjects infrastructure, as discussed on the formulas up to constants techniques page.

Some history to help users who have run into trouble with older versions of this page. This page has been updated based on a forum post to fix some issues with modified versions of the original recipe for a similar needs. The old code on this page was:

ANS( $aSoln->cmp( checker => sub {
    my ( $correct, $student, $self ) = @_;
    my $context = Context()->copy;
    $context->flags->set(no_parameters=>0);
    $context->variables->add('C0'=>'Parameter');
    my $c0 = Formula($context,'C0');
    $student = Formula($context,$student);
    $correct = Formula($context,"$c0 e^x - 1");
    return $correct == $student;
}));

However, in the setting where the correct answer is just a multiple of a standard answer without an additive constant we would expect code like the following to work:

# HAS BUGS
$aSoln = Compute("e^x");
ANS( $aSoln->cmp( checker => sub {
    my ( $correct, $student, $self ) = @_;
    my $context = Context()->copy;
    $context->flags->set(no_parameters=>0);
    $context->variables->add('C0'=>'Parameter');
    $student = Formula($context,$student);
    $correct = Formula($context,"C0 ($correct)");
    return $correct == $student;
}));

but that code has 2 issues:

  • If "0" was entered as the answer then the following submission would report "Please inform your instructor that an error occurred while checking your answer at [PG]/lib/Value/AnswerChecker.pm line 252"
  • Entering a scalar multiple of the prior answer (whether correct or now) would trigger the message "This answer is equivalent to the one you just submitted."

The solution to these issues (provided by Davide Cervone in the forum thread) is to use code like:

$aSoln = Compute("e^x");
ANS( $aSoln->cmp( checker => sub {
    my ( $correct, $student, $self ) = @_;
    return 0 if ( $student == Formula(0) );
    if ($self->{_filter_name} ne 'produce_equivalence_message') {
      my $context = Context()->copy;
      $context->flags->set(no_parameters=>0);
      $context->variables->add('C0'=>'Parameter');
      $student = Formula($context,$student);
      $correct = Formula($context,"C0 ($correct)");
    }
    return $correct == $student;
}));

which bypasses the adaptive parameter code for the 'produce_equivalence_message' filter, and also includes a test to avoid allowing 0 times the standard answer to be accepted. (See this forum post for a mention of the test against 0.)

Similar changes were made to the code on the left.

Warning: Using AnswerHints to check for DNE or NONE would cause problems with the adaptive paramater code, as these answers cannot be multiplied by the adaptive parameter, so would trigger the error message
Please inform your instructor that an error occurred while checking your answer at [PG]/lib/Value/AnswerChecker.pm line 252.
To fix this, you should handle cases where correct is DNE or NONE before it reaches the block where the parameter is added. For example, you can add something like

    if ( $correct == Compute("DNE") || $correct == Compute("NONE")) {
      # Should not get into the adaptive parameter code.
      return( $correct == $student );
    }
near the top of the function.

Problem Techniques Index