JOIN
Get Time
forums   
Search | Watch Thread  |  My Post History  |  My Watches  |  User Settings
View: Flat (newest first)  | Threaded  | Tree
Previous Thread  |  Next Thread
3.1. Running Visualizer with Your Solution | Reply
Problem

You have coded a solution for a Marathon problem, and you want to test it locally using the provided
visualizer before submitting it to the server.

Solution

The solution you submit to the server contains only the implementation of the required class. To run it with the visualizer, you have to add the code which will interact with the visualizer, namely read the parameters generated by the visualizer from standard input, create an instance of the class, pass the parameters to the corresponding method of the class, recieve the results and write them to standard output.

In some problems the scheme is more complicated: the required methods of the class are called iteratively until a certain condition is satisfied. Since this condition is usually checked at visualizer side, it's enough for your code to read the parameters from stdin and write the results of processing them to stdout in an infinite loop. Once the visualizer has finished receiving data from your solution, it will halt the solution.

Some problems provide library methods available on the server, and using them locally requires additional code which imitates calling these methods and receiving their return via standard output and standard input of your solution.

The exact order of actions is usually described in pseudocode in the visualizer manual, but you have to implement them in language of your choice.

Discussion

To run the visualizer with your solution, you should usually run:
java -jar <jar name> -exec "<command>" -seed <seed>

Here <jar name> is the name of provided .jar file which contains the visualizer, <seed> is seed for test case generation, and <command> is the command which runs your solution. If your solution is written in Java and stored in file YourSol.java, you have to compile it to YourSol.class, and use "java YourSol" as <command>. For other languages, compile your solution into executable file and use its name as <command>.

Some visualizers provide additional options which tune the way the visualization is done, but these options depend on the specific problem and are listed in the visualizer manual. Usually it is possible to turn visualization off; for some game-based problems the visualizer might allow to play the game yourself, i.e., provide the data for it via visual interface.

There are a few things which should be stressed before and apart from showing them in the examples:
- flush streams each time you have finished writing data to them, otherwise the visualizer doesn't start reading from them and looks buzzed.
- when you read data from input stream, use a method which reads off end of line.
- if you're not comfortable with using visualizer yet, perform the data exchange between it and your solution exactly as described in visualizer manual. If you leave some unread data in standard input of your solution, the visualizer might be unable to read the contents of its standard output.

There are three basic ways of visualizer-solution interaction.

1. The visualizer "calls" the solution's method once.
In this case your interaction code has to read the parameters from stdin, process them, print the result to stdout and flush stdout. Example of such problem is BrokenClayTile. The pseudocode given in the visualizer description for this problem is typical:
    S = int(readLine())
    N = int(readLine())
    P = int(readLine())
    for (i=0; i<P; i++)
        pieces[i] = readLine()
    ret = reconstruct(S, N, pieces)
    for (i=0; i<S; i++)
        printLine(ret[i])
    flush(stdout)


2. The visualizer "calls" the solution's method several time.
This is typical for game-based or simulation-based problems, in which the task is done in several steps, and information necessary for choosing some step is known only after the previous step is completed. The number of calls is usually unknown beforehand and is limited either with a constant or with some condition which must be fulfilled in order
to stop the simulation. Thus, for example, in problem ChessPuzzle method click is called as long as there are valid moves left.

Since it's the visualizer who decides to stop simulation, for your interaction code it's enough to repeat the loop "read the parameters, process them, print the result" infinitely. Once the simulation is over, the visualizer will halt your running solution.

Usually this approach is combined with a single "call" to another method which gives initial parameters of simulation or parameters that stay the same during all simulation. This "call" should be handled as described in way 1.

The pseudocode for problem ChessPuzzle is as follows:
    K = int(readLine())
    R = int(readLine())
    for (i=0; i<R; i++)
        board[i] = readLine()
    printLine(start(K, board))
    flush(stdout)
    while (true)
        revealed = readLine()
        printLine(click(revealed))
        flush(stdout)


3. The visualizer "calls" the solution's method once and provides a "library method" which solution should "call" to get necessary information.
This interaction method is the trickiest to implement in the visualizer and is used by the writer only if other two methods are too unnatural for the problem. In this case, once the visualizer has provided the initial parameters, it waits for the solution's return. If it is a predefined constant (usually "?"), the visualizer interprets the next portion of data as the parameters of the library method to be called; otherwise it assumes that this is the final return of the solution.

Now let's have a closer look at how to write interaction code, using problem ReliefMap as an example. It provides library method Relief.measure(x,y) and thus uses way 3 of visualizer-solution interaction. The interaction pseudocode for the problem looks like this:
    double measure(x, y)
    {   printLine('?')
        printLine(x)
        printLine(y)
        flush(stdout)
        return double(readLine())
    }
    main
    {   H = int(readLine())
        for (i=0; i<H; i++)
            contourMap[i] = readLine()
        ret = getMap(contourMap)
        printLine('!')
        for (i=0; i<W*H; i++)
            printLine(ret[i])
        flush(stdout)
    }

(continued in next post)
Re: 3.1. Running Visualizer with Your Solution (response to post by Nickolas) | Reply
Edit: Added VB.NET code.
C++

/* ----- imitation of provided library class ----- */
#include <iostream>
using namespace std;
 
class Relief {
    public:
    static double measure(int x, int y) {
        cout<<"?"<<endl<<x<<endl<<y<<endl;
        cout.flush();
        double ret;
        cin>>ret;
        return ret;
    }
};
/* ----- the solution itself ----- */
#include <vector>
#include <string>
using namespace std;
 
class ReliefMap {
    public:
    vector<double> getMap(vector<string> contourMap) {
        /* your code here */
    }
};
/* ----- visualizer interaction code ----- */
int main(int argc, char* argv[])
{   vector<string> contourMap;
    int H;
    cin>>H;
    for (int i=0; i<H; i++)
    {   string t;
        cin>>t;
        contourMap.push_back(t);
    }
    ReliefMap rm;
    vector<double> ret = rm.getMap(contourMap);
    cout<<"!"<<endl;
    for (int i=0; i<ret.size(); i++)
        cout<<ret[i]<<endl;
    cout.flush();
    return 0;
}


Java

/* ----- imitation of provided library class ----- */
import java.io.*;
 
class Relief {
    public static double measure(int x, int y) {
      try {
        System.out.println("?\n"+x+"\n"+y);
        System.out.flush();
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        return Double.parseDouble(br.readLine());
      }
      catch (Exception e) {}
    }
}
/* ----- the solution itself ----- */
public class ReliefMap {
    public double[] getMap(String[] contourMap) {
        /* your code here */
    }
/* ----- visualizer interaction code - can be added to the solution class ----- */
    public static void main(String[] args) {
      try {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int H = Integer.parseInt(br.readLine());
        String[] contourMap = new String[H];
        for (int i=0; i<H; i++)
            contourMap[i] = br.readLine();
        ReliefMap rm = new ReliefMap();
        double[] ret = rm.getMap(contourMap);
        System.out.println("!");
        for (int i=0; i<ret.length; i++)
            System.out.println(ret[i]);
        System.out.flush();
      }
      catch (Exception e) {}
    }
}


C#

// ----- imitation of provided library class -----
using System;
 
public class Relief {
	public static double measure(int x, int y)
	{	Console.WriteLine("?{2}{0}{2}{1}", x, y, Environment.NewLine);
		Console.Out.Flush();
		return double.Parse(Console.ReadLine());
	}
}
// ----- the solution itself -----
public class ReliefMap {
	public double[] getMap(string[] contourMap) {
		// your code here
	}
// ----- visualizer interaction code - can be added to the solution class -----
	private static void Main()
	{	int lineCount = int.Parse(Console.ReadLine());
		string[] contourMap = new string[lineCount];
		for(int i = 0; i < lineCount; i++)
			contourMap[i] = Console.ReadLine();
		double[] result = new ReliefMap().getMap(contourMap);
		Console.WriteLine("!");
		foreach(double d in result)
			Console.WriteLine(d);
		Console.Out.Flush();
	}
}


Python

# ----- imitation of provided library class ----- 
import sys
class Relief:
    @staticmethod
    def measure(x, y):
        print '?'
        print x
        print y
        sys.stdout.flush()
        return float(sys.stdin.readline())     
 
# ----- the solution itself ----- 
class ReliefMap:
    def getMap(self, contourMap):
        # your code here
        pass
 
# ----- visualizer interaction code ----- 
H = int(sys.stdin.readline())
contourMap = []
for i in range(0, H):
    contourMap.append(sys.stdin.readline()[:-1])
rm = ReliefMap()
ret = rm.getMap(contourMap)
print '!'
for line in ret:
    print line
    sys.stdout.flush()



VB.NET

Module Module1
    ' ----- imitation of provided library class ----- 
    Public Class Relief
        Public Shared Function measure(ByVal x As Integer, ByVal y As Integer) As Double
            Try
                Console.WriteLine("?")
                Console.WriteLine(x.ToString())
                Console.WriteLine(y.ToString())
                Console.Out.Flush()
                Dim ret As Double
                ret = Val(Console.ReadLine())
                Return ret
            Catch
                Return 0
            End Try
        End Function
    End Class
    ' ----- the solution itself ----- 
    Public Class ReliefMap
        Public Function getMap(ByVal contourMap As String()) As Double()
            ' your code here
        End Function
    End Class
    ' ----- visualizer interaction code ----- 
    Sub Main()
        Dim H As Integer
        Try
            H = Val(Console.ReadLine())
        Catch
        End Try
        Dim contourMap(H) As String
        Try
            For i As Integer = 0 To H - 1
                contourMap(i) = Console.ReadLine()
            Next i
        Catch
        End Try
        Dim rm As ReliefMap
        rm = New ReliefMap
        Dim ret As Double()
        ret = rm.getMap(contourMap)
        For i As Integer = 0 To ret.Length() - 1
            Console.WriteLine(ret(i))
        Next i
        Console.Out.Flush()
    End Sub
End Module


Finally, note that even if your solution correctly interacts with the visualizer, the results of testing your solution locally and on the server might still differ:
- check for visualizer updates which can happen when the match has already started. Sometimes first version of visualizer might contain some bugs, which will be found and fixed in later versions.
- the visualizer doesn't implement the check for time limit violation. So a solution which finishes successfully locally can get "Time Limit Exceeded" when tested on the server.
- on the other hand, if your solution uses certain iterative technique and checks its runtime itself, the results can differ because on different systems different number of iterations is done.
- floating point calculations can cause minor differences in your solution's behaviour on different systems, and in problems like BounceOff this might cause major differences in results.
Re: 3.1. Running Visualizer with Your Solution (response to post by Nickolas) | Reply
Sorry, but I cannot understand how to run solution with visualizer.
I tried to run this:
java -jar Visualizer.jar -exec "C:\Users\Ivan\Documents\Visual Studio 2008\Projects\maraphon66(my_first)\maraphon66(my_first)\DigitsPattern.cpp" -seed 0

but, unfortunately, I got error :" Windows cannot find "java" ".
What I should do?
Re: 3.1. Running Visualizer with Your Solution (response to post by GoogleHireMe) | Reply
"For other languages, compile your solution into executable file and use its name as <command>."

I'll add specific examples for each language.
Re: 3.1. Running Visualizer with Your Solution (response to post by Nickolas) | Reply
I've understood my mistake, but I still have got this error:"Windows cannot find "java" ".

I try to run this :

java -jar Visualizer_new.jar -exec "C:\Users\Ivan\Documents\Visual Studio 2008\Projects\maraphon66(my_first)\run.exe" -seed 1

What is wrong now?
Re: 3.1. Running Visualizer with Your Solution (response to post by GoogleHireMe) | Reply
Do you actually have Java installed properly at your machine?

Yuo should better ask such questions in the forum of the match, since this thread is reference information, not troubleshooting one.
Re: 3.1. Running Visualizer with Your Solution (response to post by Nickolas) | Reply
I think YES, because, in the other way, I wouldn't be able to run Arena.
So, I think, it's troublesooting... maybe, something wrong with my Java?

Edit:
Sorry, I create new thread in forum. Thanks.
Re: 3.1. Running Visualizer with Your Solution (response to post by Nickolas) | Reply
The link to the problem BounceOff was wrong in the second post.
RSS