// Hybrid Simulation for Chance Measure
// Written by Microsoft Visual C++ 6.0
// Copyright by UTLab @ Tsinghua University
// http://orsc.edu.cn/UTLab


#include <vector>
#include <iostream>
#include <cmath>
#include "utlab.h"
using namespace std;

inline double max(double a, double b)
{
	return (a>b)?a:b;
}


inline double min(double a, double b)
{
	return (a<b)?a:b;
}

void main(){
	const int N = 5000;
	double sup1=0, sup2=0;
	vector<double> theta1(N,0),theta2(N,0),theta3(N,0),cr(N,0),su1(N,0),su2(N,0),z1(N,0),z2(N,0),z3(N,0),p(N,0);
	
	
	int i=0, j=0;
	//generate theta1, theta2,..., thetaN from the credibility space
	for ( i = 0 ; i < N ; i++){
		theta1[i]=myu(1,3);
		theta2[i]=myu(2,4);
		theta3[i]=myu(3,5);
		cr[i]=min(min(triangle(theta1[i],1,2,3) ,triangle(theta2[i],2,3,4) ),triangle(theta3[i],3,4,5) )/2;
	}

	//generate omg1, omg2,..., omgN from the probability space
	for ( i = 0 ; i < N ; i++){
		z1[i] = myn(0,1);
		z2[i] = myn(0,1);
		z3[i] = myn(0,1);
	}
	
	double temp=0,f=0;
	//for any fixed i in [0,N-1], i.e. fixed theta1 theta2 theta3, compute Pr{f(omega)<=0}
	for ( i = 0 ; i < N ; i++){
		//now theta[i] fixed
		temp=0;f=0;
		for ( j = 0 ; j < N ; j++){
			f=9-((theta1[i]+z1[j])*(theta1[i]+z1[j])+(theta2[i]+z2[j])*(theta2[i]+z2[j])+(theta3[i]+z3[j])*(theta3[i]+z3[j]));
			if (f<=0)
				temp++;
		}
		//p[i] solved.
		p[i]=temp/N;
		su1[i]=min(cr[i],p[i]);
		su2[i]=min(cr[i],1-p[i]);
	}

	//max of CrP
	for ( i = 0 ; i < N ; i++){
		sup1=max(sup1,su1[i]);
		sup2=max(sup2,su2[i]);
	}
	
	cout<<max(sup1,1-sup2);
}
