WeBWorK Problems

Variable Assignments and Answers Read From a File

Variable Assignments and Answers Read From a File

by Sherman Wong -
Number of replies: 8
A professor from another department wants to know if WeBWorK can be used for the following situation:

He wants to assign to each of roughly 250 students a set of parameters, each set containing different values. These sets of values are contained in a file. Along with the each set, there is a set of answers for that particular set. Can a WeBWorK problem template be easily written so that when a student is assigned this problem, the assignments of the variables are read from the file and also the assignments of the answers are read from the same file, e.g.? Is there a straightforward method of ensuring that each student is assigned a different set of valuables, i.e., selection without replacement?

Thank you.

Sherman
In reply to Sherman Wong

Re: Variable Assignments and Answers Read From a File

by Gavin LaRose -
Hi Sherman,

I'm not quite sure what your colleague is trying to do here. What do you mean by the "set of parameters" that he is trying to assign? My first thought was that this is something like a lab situation where the students get parameters for the lab, and the answers that they should obtain are then a function of those parameters, but I don't have a lot of confidence that I'm interpreting your post correctly.

Thanks,
Gavin
In reply to Gavin LaRose

Re: Variable Assignments and Answers Read From a File

by Sherman Wong -
Hi Gavin:

I apologize for being vague. Suppose that a problem template uses 4 variables, $a, $b, $c, and $c. What the professor wants to do is to assign to each of these variables a value that is read from a file. Similarly, if the problem has a three-part answer, then the answers are assigned from data on the same file also.

Thank you,

Sherman
In reply to Sherman Wong

Re: Variable Assignments and Answers Read From a File

by Gavin LaRose -
Hi Sherman,

It sounds to me as if this would be most easily done with a local "macros file" in the course templates/macros directory which could then be loaded in the loadMacros() call at the top of the problem file.

This assumes that (a) all of the possible input variable values are then put in the same file, and (b) the values of those are indexed in some way to associate them with the students in the course. I'm thinking of just having the macros file define the variables for each student by looking at the student's ID number or login name. This could be done by having the local macros file define a hash on students' login names, e.g.,
 
%vars = ( user1 => [ 1, 2, 3, 4 ],
user2 => [ 5, 6, 7, 8 ],
...
);


And then in the problem file, have
 
($a, $b, $c, $d) = @{$vars{$studentLogin}};


It would also be possible to put the variables in a file named such that a different file is loaded for each student---e.g., data_user1.pl, data_user2.pl, etc.---in which case each file could just define the variables desired.

There should be some easy variations to this, but I'll stop with this as a start and see if either it seems like it's getting at what you want, or someone else wants to add a comment that's more to the point.

Gavin
In reply to Gavin LaRose

Re: Variable Assignments and Answers Read From a File

by Sherman Wong -
Hello Gavin:

I thank you for the suggestion regarding the ability to read variable assignments from a file and will pass it on to the professor who asked about it.

I'd like to thank Dr. Brian Walton and Dr. Boyd Duffee for their remarks.

I agree with Dr. Walton about the necessity of having to read variable assignments from a file particularly in a mathematical setting. In brief, the reason for the use of a file is that the professor is in accountancy and wants to test his students on the use of Excel; he wants the solutions to be exactly the same as those from Excel.

As to Dr. Duffee's work on creating a method for the PG language that performs this task, I really believe that it would be beneficial. I vaguely recall a WeBWorK template dealing with systems of linear equations where the student is to balance a chemical equation and the solution was linked to the equation displayed. Although the balancing is an application of solving a linear system, the random assignment of values is probably very tricky since the choice of values depend on the elements used. This is one application where reading from a file both the variable assignments and the solutions would be very handy.

Once again I thank you all for your help.

Sherman
In reply to Sherman Wong

Re: Variable Assignments and Answers Read From a File

by D. Brian Walton -
Before going to all of the work of creating a file with parameter values, I would actually have a brief discussion about how the file would have been constructed in the first place.

I have a difficult time imagining the advantage of using a file with all possibilities listed. There is no flexibility if the size of the class needed to change. The burden of problem generation is set squarely on the individual rather than a computer. The only advantage I can see is having a 100% guarantee that every student sees a different problem.

I have to imagine that there was some type of algorithm that the colleague went through in order to create the table. If so, I would ask why a file of a set number of possible values is better than having the computer randomly generate values that use the same algorithm. If there were enough variation allowed in the random generation, the odds of two students seeing the same problem would be very low.

For example, I am imagining a scenario where a file might be useful, say a chemistry or geology lab where some object needs to be identified by using a series of diagnostics. The file would then contain the results of the various diagnostic tests, and the student would need to interpret the results to come to some conclusion. I would implement this by actually randomly choosing the final outcome, and then randomly computing diagnostic results that would be compatible with reaching that final conclusion.

Brian Walton
In reply to D. Brian Walton

Re: Variable Assignments and Answers Read From a File

by Boyd Duffee -
I may be working on what you're looking for in terms of objects. I've just finished a class file, BritishSportsCars.pl, that creates an instance of an object that knows its name, mass and time from 0 to 60 mph. I'm about to extend the same principle to create a class of isotopes for alpha decay and another for beta emission. The data could just come out of a csv file and is not fragile to updates as the object accessor methods are always $object->get_fieldname()

Is this something people would be interested in?

Boyd
In reply to Boyd Duffee

Objects for WebWork

by Boyd Duffee -
Every so often, I want to write a question based on real world data and end up creating hashes to hold data together. Since this is the basis for constructing objects in Perl, I wrote a macro that would read all of it's data and then pick one of the objects at random, a bit like how WebWork lets you pick a number at random. Realizing how I'd be changing the macro every time I wanted a different class of object, I then wrote a script that would parse a text file with the data and create the macro. I submit the results for your consideration.

Half of the file is documentation, which I hope explains what I was thinking. It is a touch fragile if the data contains the same characters as the field separators and has only been tested a few times, so YMMV.

let me know if it's somewhat useful,
Boyd
In reply to Boyd Duffee

Re: Objects for WebWork

by Boyd Duffee -
More people these days are comfortable using spreadsheets which have a tendency to produce CSV files, so I wrote a version of the script that handles CSV. Of course, it requires the perl module Text::CSV, but it should be a bit more robust than the previous text file version.

If you find bugs, there's an email address at the end of the file.

Boyd